Skip to content

Commit 15b7ae2

Browse files
committed
fix(property/attribute): add property/attribute handling
1 parent fe1ce06 commit 15b7ae2

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

src/reconciler/host.ts

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ function isHostElement(
2222
);
2323
}
2424

25+
const CLASS_MAP = { className: "class", style: "style" };
26+
function getPropertyKind(
27+
type: ShadowHostElement["type"],
28+
key: string,
29+
): { type: "attribute"; key: string } | { type: "property"; key: string } {
30+
if (typeof type === "string") {
31+
if (key in CLASS_MAP === true) {
32+
return { type: "attribute", key: (CLASS_MAP as any)[key] };
33+
} else if (type === "svg" || type.startsWith("svg:")) {
34+
return { type: "attribute", key: "class" };
35+
} else {
36+
return { type: "property", key };
37+
}
38+
} else {
39+
return { type: "property", key: key };
40+
}
41+
}
42+
2543
export const hostReconcile: Reconciler = (opt) => {
2644
// Check if new shadow is of type dom-element
2745
if (isHostElement(opt.shadowElement)) {
@@ -125,25 +143,33 @@ export const hostReconcile: Reconciler = (opt) => {
125143
}
126144
} else {
127145
untracked(() => {
128-
if (propKey === "style") {
129-
(opt.shadowCache.node as any).setAttribute(
130-
"style",
131-
Object.entries(
132-
(opt.shadowElement as ShadowHostElement).props[propKey],
133-
)
134-
.map(([key, value]) => `${key}: ${value}`)
135-
.join(";"),
146+
const kind = getPropertyKind(
147+
(opt.shadowCache.value as ShadowHostElement).type,
148+
propKey,
149+
);
150+
151+
if (kind.type === "attribute") {
152+
(opt.shadowCache.node as Element).setAttribute(
153+
kind.key,
154+
kind.key === "style"
155+
? Object.entries(
156+
(opt.shadowElement as ShadowHostElement).props[propKey],
157+
)
158+
.map(([key, value]) => `${key}: ${value}`)
159+
.join(";")
160+
: (opt.shadowElement as ShadowHostElement).props[propKey],
136161
);
137-
} else {
138-
(opt.shadowCache.node as any)[propKey] = (
162+
} else if (kind.type === "property") {
163+
(opt.shadowCache.node as any)[kind.key] = (
139164
opt.shadowElement as ShadowHostElement
140165
).props[propKey];
141166
}
142167
});
143168
}
144169

145-
(opt.shadowCache.value as ShadowHostElement).props[propKey] =
146-
opt.shadowElement.props[propKey];
170+
(opt.shadowCache.value as ShadowHostElement).props[propKey] = (
171+
opt.shadowElement as ShadowHostElement
172+
).props[propKey];
147173
}
148174
}
149175

0 commit comments

Comments
 (0)