Matias & Alex
<div
[style.width]="myWidth"
[class.active]=
"myActiveClass"
[style]="myStyles"
[class]="myClasses">
...
</div>
if (rf & 1) {
element(0, 'div');
}
if (rf & 2) {
styleProp('width',
ctx.myWidth);
classProp('active',
ctx.myActiveClass);
styleMap(ctx.myStyles);
classMap(ctx.myClasses);
}
<div
[style.width]="myWidth"
[class.active]=
"myActiveClass"
[style]="myStyles"
[class]="myClasses">
...
</div>
// create
ElementNode, // <div>
// update
PropertyNode, // style.width
PropertyNode, // class.active
PropertyNode, // style
PropertyName // class
// [style.width]
property = {
kind: NodeKind.Property,
name: 'style.width',
expression: o.Expression,
}
interface CreateEmitter {
emit(node: CreateNode):
o.Statement|null;
}
interface UpdateEmitter {
emit(node: UpdateNode):
o.Statement|null;
}
// [style.width]
property = {
kind: NodeKind.Property,
name: 'style.width',
expression: o.Expression,
}
// [style.width]
property = {
kind: NodeKind.StyleProp,
name: 'width',
expression: o.Expression,
}
export class StyleTransform
implements Transform {
visit(node: Node, list: List): Node {
// [style] or [style.prop]
if (node.kind === NodeKind.Property
&& isStyleProp(node.name)) {
node = updateToStyleBinding(node);
}
return node;
}
}
function updateToStyleBinding(
node: PropertyNode) {
if (node.name === 'style') {
return {...} as StyleMap;
} else {
return {...} as StyleProp;
}
}
property = {
kind: NodeKind.StyleProp,
name: 'propName', // width
expression: o.Expression,
}
property = {
kind: NodeKind.StyleMap,
expression: o.Expression,
}
property = {
kind: NodeKind.ClassProp,
name: 'className', // activeClass
expression: o.Expression,
}
property = {
kind: NodeKind.ClassMap,
expression: o.Expression,
}