Open Hanks10100 opened 6 years ago
To achieve it, the scoped style sheets should be sent to native render engines before render. And the front-end frameworks no longer calculate the css rules referenced in class list.
All the following packages should be adjusted:
weex-vue-loader
weex-js-runtime
weex-vue-framework
weex-vue-loader
Generate scopeId
for each style sheet, and inject a code snippet to process them at the definition of each component.
if (typeof weex === 'object' && weex && weex.document) {
// process style sheets here
try {
weex.document.registerStyleSheets(scopeId, styleSheets)
} catch (e) {}
}
In practice, the weex.document
should implement this API:
weex.document.registerStyleSheets(scopeId: string, styleSheets: Array<StyleSheet>)
It will send a render directive like this:
callNative(instanceId, [{
module: 'dom',
method: 'registerStyleSheets',
args: [scopeId, styleSheets]
}])
classList
to native instead of classStyles
.Add a classList
attribute on the Weex Element, and no longer calculate the class styles in it.
@styleScope
attribute on each element which value equals the scopeId
.The scopeId
will be generated by weex-vue-loader
and passed to Vue component through options
.
<div class="btn btn-large"></div>
{
type: 'div',
classList: ['btn', 'btn-large'],
attr: {
'@styleScope': ['data-v-b3b891b6']
}
}
class
attribute directly.In particular, Do not get styles from classList
and call el.setStyles(styles)
any more, use el.setAttr('class', classList)
instead.
@styleScope
and classList
.It's not the specificity of CSS selectors.
Even Weex doesn't support complex selectors, the priority of styles should also be considered.
If an element contains both style
and classList
, the CSS rules in style
will override those calculated from classList
.
See this example:
<template>
<div class="foo vee bar"></div>
</template>
<style scoped>
.foo {
font-size: 50px;
}
.bar {
font-size: 100px;
color: green;
}
.vee {
color: blue;
}
</style>
The calculated style of <div>
should be:
div {
font-size: 100px;
color: blue;
}
The font-size
in .bar
override the font-size
in .foo
.
styleSheet
> order in classList
.The color
in .vee
override the color
in .bar
, even the bar
in written after the vee
in the class name.
Will be compiled to:
Currently, class names and the stylesheet are managed in front-end frameworks and
weex-js-runtime
. However, in this case, the result ofbox.classList
couldn't be got in front-end frameworks.The stylesheet should be managed in native render engines.