Skip to content

Commit f0283b5

Browse files
committed
fix #28; support CSS custom properties
1 parent eba29e8 commit f0283b5

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

src/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ function hypertext(render, postprocess) {
555555
} else if (typeof subvalue === "function") {
556556
node[key] = subvalue;
557557
} else if (key === "style" && isObjectLiteral(subvalue)) {
558-
Object.assign(node[key], subvalue);
558+
setStyles(node[key], subvalue);
559559
} else {
560560
setAttribute(node, key, subvalue === true ? "" : subvalue);
561561
}
@@ -566,7 +566,7 @@ function hypertext(render, postprocess) {
566566
if (typeof value === "function") {
567567
node[name] = value;
568568
} else { // style
569-
Object.assign(node[name], value);
569+
setStyles(node[name], value);
570570
}
571571
}
572572
}
@@ -663,3 +663,12 @@ function removeAttribute(node, name) {
663663
}
664664
node.removeAttribute(name);
665665
}
666+
667+
// We can’t use Object.assign because custom properties…
668+
function setStyles(style, values) {
669+
for (const name in values) {
670+
const value = values[name];
671+
if (name.startsWith("--")) style.setProperty(name, value);
672+
else style[name] = value;
673+
}
674+
}

test/snapshots.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ export function interpolatedStyleObject() {
6868
return html`<span style=${{background: "yellow"}}>It’s all yellow!</span>`;
6969
}
7070

71+
export function interpolatedStyleObjectWithCustomProperty() {
72+
return html`<span style=${{"--custom-property": "yellow"}}>It’s all yellow!</span>`;
73+
}
74+
7175
export function interpolatedStyleString() {
7276
return html`<span style="background: ${"yellow; font-style: italic"};">It’s yellow (and italic).</span>`;
7377
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span style="--custom-property: yellow;">It’s all yellow!</span>

0 commit comments

Comments
 (0)