Add add_css_class method to DynamicElement

pull/5481/head
Alfred Ringstad 6 years ago
parent f001107711
commit 4fcc68188d

@ -14,6 +14,7 @@ import Animation from './Animation';
import Action from './Action'; import Action from './Action';
import { string_literal } from '../utils/stringify'; import { string_literal } from '../utils/stringify';
import { Literal } from 'estree'; import { Literal } from 'estree';
import Text from './Text';
export default class DynamicElement extends Node { export default class DynamicElement extends Node {
type: 'DynamicElement'; type: 'DynamicElement';
@ -30,6 +31,7 @@ export default class DynamicElement extends Node {
animation?: Animation = null; animation?: Animation = null;
children: INode[]; children: INode[];
scope: TemplateScope; scope: TemplateScope;
needs_manual_style_scoping: boolean;
constructor(component: Component, parent, scope, info) { constructor(component: Component, parent, scope, info) {
super(component, parent, scope, info); super(component, parent, scope, info);
@ -96,4 +98,37 @@ export default class DynamicElement extends Node {
this.children = map_children(component, this, this.scope, info.children); this.children = map_children(component, this, this.scope, info.children);
} }
add_css_class() {
if (this.attributes.some(attr => attr.is_spread)) {
this.needs_manual_style_scoping = true;
return;
}
const { id } = this.component.stylesheet;
const class_attribute = this.attributes.find(a => a.name === 'class');
if (class_attribute && !class_attribute.is_true) {
if (class_attribute.chunks.length === 1 && class_attribute.chunks[0].type === 'Text') {
(class_attribute.chunks[0] as Text).data += ` ${id}`;
} else {
(class_attribute.chunks as Node[]).push(
new Text(this.component, this, this.scope, {
type: 'Text',
data: ` ${id}`,
synthetic: true
} as any)
);
}
} else {
this.attributes.push(
new Attribute(this.component, this, this.scope, {
type: 'Attribute',
name: 'class',
value: [{ type: 'Text', data: id, synthetic: true }]
} as any)
);
}
}
} }

@ -50,9 +50,9 @@ export default function (
const snippet = expression ? expression.node : x`#ctx.${name}`; // TODO is this right? const snippet = expression ? expression.node : x`#ctx.${name}`; // TODO is this right?
return x`${snippet} ? "${name}" : ""`; return x`${snippet} ? "${name}" : ""`;
}); });
// if (node.needs_manual_style_scoping) { if (node.needs_manual_style_scoping) {
// class_expression_list.push(x`"${node.component.stylesheet.id}"`); class_expression_list.push(x`"${node.component.stylesheet.id}"`);
// } }
const class_expression = const class_expression =
class_expression_list.length > 0 && class_expression_list.length > 0 &&
class_expression_list.reduce((lhs, rhs) => x`${lhs} + ' ' + ${rhs}`); class_expression_list.reduce((lhs, rhs) => x`${lhs} + ' ' + ${rhs}`);

Loading…
Cancel
Save