Skip to content

Commit 666143c

Browse files
committed
update docs for accuracy
1 parent ed5a2c7 commit 666143c

3 files changed

Lines changed: 38 additions & 16 deletions

File tree

src/routes/reference/jsx-attributes/on.mdx

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ description: >-
2424

2525
## Value
2626

27-
- **Type:** event handler function or `EventListenerObject & AddEventListenerOptions`
27+
- **Type:** event handler function or object with `handleEvent` and optional `AddEventListenerOptions`
2828

2929
Listener passed to `addEventListener`.
3030

@@ -33,6 +33,7 @@ Listener passed to `addEventListener`.
3333
- `on:name={handler}` attaches a listener for the event named `name`.
3434
- The listener is attached directly to the element instead of using Solid's delegated event system.
3535
- Event names keep the text after `on:` exactly as written.
36+
- Use `on:*` when you need exact event casing, such as case-sensitive custom events.
3637
- When the value changes, Solid removes the previous direct listener and adds the new one.
3738
- Listener options such as `once`, `passive`, `capture`, and `signal` can be provided by passing an object that implements `handleEvent`.
3839
- `oncapture:*` remains available as a deprecated capture-specific form.
@@ -42,26 +43,43 @@ Listener passed to `addEventListener`.
4243
### Basic usage
4344

4445
```tsx
45-
<div on:DOMContentLoaded={(e) => console.log("Welcome!", e)} />
46+
const [message, setMessage] = createSignal("Waiting");
47+
let el;
48+
49+
<>
50+
<button
51+
onClick={() =>
52+
el.dispatchEvent(new CustomEvent("MyEvent", { detail: "Hello" }))
53+
}
54+
>
55+
Dispatch event
56+
</button>
57+
58+
<div on:MyEvent={(e) => setMessage(e.detail)} ref={el} />
59+
<p>{message()}</p>
60+
</>;
4661
```
4762

4863
### Listener options
4964

5065
```tsx
66+
const [count, setCount] = createSignal(0);
67+
5168
const handler = {
52-
handleEvent(e) {
53-
console.log(e);
69+
handleEvent() {
70+
setCount((count) => count + 1);
5471
},
5572
once: true,
56-
passive: false,
57-
capture: true,
5873
};
5974

60-
<div on:wheel={handler} />;
75+
<>
76+
<button on:click={handler}>Click me</button>
77+
<p>Clicked {count()} time(s)</p>
78+
</>;
6179
```
6280

6381
:::note
64-
The object-listener form was added in Solid 1.9.0.
82+
The object-listener form was added in Solid 1.9.0. It is useful when you need options such as `once`, `passive`, `capture`, or `signal`.
6583
:::
6684

6785
## Related

src/routes/reference/jsx-attributes/style.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ description: >-
2424

2525
## Value
2626

27-
- **Type:** `string | Record<string, string | undefined | null>`
27+
- **Type:** `string | CSSProperties`
2828

2929
CSS string or style object.
3030

3131
## Behavior
3232

3333
- String values are written as inline CSS text.
34-
- Object values are applied property by property with `element.style.setProperty`, so keys should use lower-case, dash-separated CSS property names. CSS custom properties can use keys such as `--my-color`.
34+
- Object values are applied property by property with `element.style.setProperty`, so keys should use lower-case, dash-separated CSS property names instead of camelCase. CSS custom properties can use keys such as `--my-color`.
3535
- Nullish values in a style object remove that property, and falsy overall `style` values remove the `style` attribute.
36+
- In SSR output, object values are serialized into the emitted `style` attribute.
3637

3738
## Examples
3839

src/routes/reference/jsx-attributes/use.mdx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ description: >-
2525
```ts
2626
type Accessor<T> = () => T;
2727

28-
function directive(element: Element, accessor: Accessor<any>): void;
28+
function directive(element: Element, accessor?: Accessor<any>): void;
2929
```
3030

3131
## Value
3232

3333
- **Type:** directive argument
3434

35-
Value exposed to the directive through an accessor.
36-
When no explicit value is passed, the accessor returns `true`.
35+
The directive reads the value through an accessor. Without an explicit value, the accessor returns `true`.
3736

3837
## Behavior
3938

40-
- The compiler lowers `use:name={value}` to a directive helper call with the element and an accessor for `value`. Without an explicit value, the accessor returns `true`.
41-
- The directive runs during rendering before the element is connected to the DOM.
39+
- `use:name={value}` passes the element and an accessor for `value` to the directive.
40+
- Without an explicit value, the accessor returns `true`.
41+
- The directive runs during rendering in the current owner, before the element is connected to the DOM, so it can create effects and register cleanup.
4242
- `use:*` works only on native elements, including custom elements, and is not forwarded through user-defined components.
4343

4444
## Examples
@@ -48,8 +48,11 @@ When no explicit value is passed, the accessor returns `true`.
4848
```tsx
4949
function model(element, value) {
5050
const [field, setField] = value();
51+
const onInput = ({ currentTarget }) => setField(currentTarget.value);
52+
5153
createRenderEffect(() => (element.value = field()));
52-
element.addEventListener("input", ({ target }) => setField(target.value));
54+
element.addEventListener("input", onInput);
55+
onCleanup(() => element.removeEventListener("input", onInput));
5356
}
5457

5558
const [name, setName] = createSignal("");

0 commit comments

Comments
 (0)