Skip to content

Commit ea35dd3

Browse files
fix(h): merge selector classes with a getter class prop instead of throwing
A component that forwards its props to a selector-class element (props => h("button.btn", props)) receives a reactive class as a getter. Merging it with the static classes spread the accessor descriptor and added a value, so Object.defineProperties threw "Invalid property descriptor. Cannot both specify accessors and a value or writable attribute". A hand-written get class() on h("div.card", ...) threw the same way. Drop get/set from the descriptor and read the getter through the props object, so the existing function path turns the merged class into a getter that stays reactive.
1 parent 4e1a83a commit ea35dd3

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/h": patch
3+
---
4+
5+
A getter `class` on a tag with static classes no longer throws "Invalid property descriptor". A component that forwards its props to a selector-class element (`props => h("button.btn", props)`) and is given a reactive `class` received it as a getter, and merging it with the static classes built a descriptor with both `get` and `value`. The getter is now read through the props object, so the merged class keeps the static classes and stays reactive.

‎packages/h/src/hyperscript.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,12 @@ function materialize(args: any[]): ExpandableNode | ExpandableNode[] {
159159
for (const k in d) {
160160
if (k === "class" && classes.length !== 0) {
161161
const staticClasses = classes;
162-
const cls = d[k].value;
162+
// An accessor `class` (e.g. a forwarded component prop) is read
163+
// through `l`, so the merged value becomes a getter that tracks it.
164+
const { get, set, ...desc } = d[k];
165+
const cls = get ? () => l[k] : desc.value;
163166
d[k] = {
164-
...d[k],
167+
...desc,
165168
value:
166169
typeof cls === "function" ? () => [...staticClasses, cls()] : [...staticClasses, l[k]]
167170
};

‎packages/h/test/hyperscript.spec.js‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,33 @@ describe("Test HyperScript", () => {
278278
expect(el.classList.contains("sel")).toBe(true);
279279
expect(el.classList.contains("opt")).toBe(true);
280280
});
281+
282+
test("a getter class is merged with selector classes and stays reactive", () => {
283+
// A component that forwards its props to a selector-class element
284+
// receives a zero-arity `class` prop as a getter.
285+
const [active, setActive] = createSignal(false);
286+
const Button = props => h("button.btn", props);
287+
const el = mount(() => h(Button, { class: () => (active() ? "on" : "off") }));
288+
expect(el.className).toBe("btn off");
289+
290+
setActive(true);
291+
flush();
292+
expect(el.className).toBe("btn on");
293+
});
294+
295+
test("a hand-written class getter is merged with selector classes", () => {
296+
const [cls, setCls] = createSignal("off");
297+
const el = mount(() =>
298+
h("div.card", {
299+
get class() {
300+
return cls();
301+
}
302+
})
303+
);
304+
expect(el.className).toBe("card off");
305+
306+
setCls("on");
307+
flush();
308+
expect(el.className).toBe("card on");
309+
});
281310
});

0 commit comments

Comments
 (0)