Skip to content

Commit 9358ef8

Browse files
ryansolidClaude via Cursorcursoragent
committed
fix(html): component props named on/only/once get getters like any dynamic prop (#3728)
The getter-wrapping exemption for `on*` names applied to components as well as native elements, so `<${Loading} on=${() => key()}>` passed a function that never changed. Components now exempt only camelCase `onXxx` handlers (and `ref`); native elements are unchanged. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 6be8486 commit 9358ef8

4 files changed

Lines changed: 111 additions & 11 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/html": patch
3+
---
4+
5+
Zero-argument functions passed to component props named `on`, `only`, `once`, etc. are now wrapped in getters like any other dynamic prop (#3728). The `on*` exemption on components now covers only `onXxx` handler names (an uppercase letter after `on`), so `<${Loading} on=${() => key()}>` re-keys the boundary when `key()` changes. Native elements are unchanged: every `on*` prop is still passed through as an event handler. A lowercase `onclick` passed to a component is now wrapped in a getter; name it `onClick` or give the handler a parameter.

‎packages/html/README.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ There are a few differences from Solid's JSX that are important to note.
9090
html`<div id=${() => props.id}>${() => firstName() + lastName()}</div>`
9191
```
9292

93-
2. Events on components require explicit event in the arguments
93+
2. Zero-argument functions passed to component props become getters
9494

95-
Solid's Tagged Template Literals automatically wraps functions passed to props of components with no arguments in getters so you need to provide one to prevent this. The same applies to render props like in the `<For>` component.
95+
Solid's Tagged Template Literals automatically wrap functions with no arguments passed to props of components in getters, so a render prop like the `<For>` child needs a declared argument to stay a function. Event handlers named `onXxx` (an uppercase letter after `on`) are exempt; any other prop, including `on` on `<Loading>`, `only`, or a lowercase `onclick`, is wrapped.
9696

9797
```js
98-
// good
99-
html`<${Button} onClick=${(e) => console.log("Hi")} />`;
100-
101-
// bad
98+
// handler: passed as-is
10299
html`<${Button} onClick=${() => console.log("Hi")} />`;
100+
101+
// getter: `props.on` reads `key()`
102+
html`<${Loading} on=${() => key()}>...<//>`;
103103
```
104104

105105
4. All refs are callback form

‎packages/html/src/tagged-jsx.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function createHtml() {
233233
own[prop.name] = prop.value;
234234
break;
235235
case EXPRESSION_PROP:
236-
applyGetter(own, prop.name, values[prop.value]);
236+
applyGetter(own, prop.name, values[prop.value], node.type === COMPONENT_NODE);
237237
break;
238238
case SPREAD_PROP:
239239
const spreadValue = values[prop.value];
@@ -258,12 +258,23 @@ function createHtml() {
258258
return mergeProps(...sources) as Record<string, any>;
259259
};
260260

261-
const applyGetter = (props: Record<string, any>, name: string, value: any) => {
261+
// Elements: every `on*` prop is an event to `spread`. Components have no
262+
// event namespace, so only the `onXxx` handler convention is exempt —
263+
// `on`, `only`, `once` are ordinary props.
264+
const isHandlerName = (name: string, component: boolean) =>
265+
component ? /^on[A-Z]/.test(name) : name.startsWith("on");
266+
267+
const applyGetter = (
268+
props: Record<string, any>,
269+
name: string,
270+
value: any,
271+
component: boolean
272+
) => {
262273
if (
263274
typeof value === "function" &&
264275
value.length === 0 &&
265276
name !== "ref" &&
266-
!name.startsWith("on")
277+
!isHandlerName(name, component)
267278
) {
268279
Object.defineProperty(props, name, {
269280
get() {

‎packages/html/test/tagged-jsx.test.ts‎

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { createRoot, createSignal, flush, createMemo } from "solid-js";
1+
import { createRoot, createSignal, flush, createMemo, Loading } from "solid-js";
22
import htmlTag from "../src/tagged-jsx.js";
3-
import { expect, it, describe, beforeEach } from "vitest";
3+
import { expect, it, describe, beforeEach, vi } from "vitest";
44
import { insert, render, registerElementClaim } from "@solidjs/web";
55

66
const For = (props: any) => {
@@ -429,6 +429,90 @@ describe("Tagged JSX Integration Tests", () => {
429429
expect(container.querySelector("li")?.textContent).toBe("Item 1");
430430
dispose();
431431
}));
432+
433+
it("wraps zero-arg functions in getters for non-handler `on*` props (#3728)", () => {
434+
const [value, setValue] = createSignal(1);
435+
const handler = () => "handled";
436+
const ref = () => {};
437+
let props!: any;
438+
const Probe = (p: any) => {
439+
props = p;
440+
return null;
441+
};
442+
const dispose = createRoot(d => {
443+
html`<${Probe}
444+
on=${() => value()}
445+
only=${() => value() * 2}
446+
once=${() => value() * 3}
447+
onClick=${handler}
448+
ref=${ref}
449+
/>`;
450+
return d;
451+
});
452+
453+
expect(props.on).toBe(1);
454+
expect(props.only).toBe(2);
455+
expect(props.once).toBe(3);
456+
expect(props.onClick).toBe(handler);
457+
expect(props.ref).toBe(ref);
458+
459+
setValue(2);
460+
flush();
461+
expect(props.on).toBe(2);
462+
expect(props.only).toBe(4);
463+
expect(props.once).toBe(6);
464+
dispose();
465+
});
466+
467+
it("keeps zero-arg `onXxx` handlers on native elements as events", () => {
468+
let clicks = 0;
469+
const container = document.createElement("div");
470+
document.body.append(container);
471+
const dispose = render(
472+
() =>
473+
html`<div>
474+
<button onClick=${() => clicks++}>a</button>
475+
<button onclick="void 0">b</button>
476+
</div>`,
477+
container
478+
);
479+
const [a, b] = container.querySelectorAll("button");
480+
expect(clicks).toBe(0);
481+
a.dispatchEvent(new MouseEvent("click", { bubbles: true }));
482+
expect(clicks).toBe(1);
483+
expect(b.getAttribute("onclick")).toBe("void 0");
484+
dispose();
485+
});
486+
487+
it("re-keys <Loading> from a reactive `on` getter (#3728)", async () => {
488+
vi.useFakeTimers();
489+
try {
490+
const delay = <T>(ms: number, value: T) => new Promise<T>(r => setTimeout(r, ms, value));
491+
const [key, setKey] = createSignal(0);
492+
const container = document.createElement("div");
493+
const dispose = render(() => {
494+
const data = createMemo(() => delay(100, key()));
495+
return html.define({ Loading })`<${Loading} on=${() => key()} fallback="loading">
496+
<span>${() => data()}</span>
497+
<//>`;
498+
}, container);
499+
flush();
500+
expect(container.textContent!.trim()).toBe("loading");
501+
await vi.advanceTimersByTimeAsync(100);
502+
flush();
503+
expect(container.textContent!.trim()).toBe("0");
504+
505+
setKey(1);
506+
flush();
507+
expect(container.textContent!.trim()).toBe("loading");
508+
await vi.advanceTimersByTimeAsync(100);
509+
flush();
510+
expect(container.textContent!.trim()).toBe("1");
511+
dispose();
512+
} finally {
513+
vi.useRealTimers();
514+
}
515+
});
432516
});
433517

434518
describe("Special Elements and Namespaces", () => {

0 commit comments

Comments
 (0)