Skip to content

Commit b3f39c8

Browse files
authored
add docs for handleEvent object syntax (#866)
1 parent 793a9ba commit b3f39c8

File tree

7 files changed

+66
-27
lines changed

7 files changed

+66
-27
lines changed

src/middleware/legacy-routes-redirect.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ const LEGACY_ROUTES = {
6161
"/references/api-reference/special-jsx-attributes/classList": "/reference/jsx-attributes/classlist",
6262
"/references/api-reference/special-jsx-attributes/innerHTML-or-textContent": "/reference/jsx-attributes/innerhtml-or-textcontent",
6363
"/references/api-reference/special-jsx-attributes/on_": "/reference/jsx-attributes/on_",
64-
"/references/api-reference/special-jsx-attributes/on_-and-oncapture_": "/reference/jsx-attributes/on-and-oncapture",
64+
"/references/api-reference/special-jsx-attributes/on_-and-oncapture_": "/reference/jsx-attributes/on",
65+
6566
"/references/api-reference/special-jsx-attributes/once": "/reference/jsx-attributes/once",
6667
"/references/api-reference/special-jsx-attributes/prop_": "/reference/jsx-attributes/prop",
6768
"/references/api-reference/special-jsx-attributes/ref": "/reference/jsx-attributes/ref",
@@ -102,7 +103,10 @@ const LEGACY_ROUTES = {
102103
"/references/concepts/reactivity/tracking": "/concepts/intro-to-reactivity#subscribers",
103104
"/references/concepts/ssr/async-ssr": "/guides/fetching-data",
104105
"/references/concepts/ssr/simple-client-fetching-ssr": "/guides/fetching-data",
105-
"/references/concepts/state-management/context": "/guides/complex-state-management#state-sharing"
106+
"/references/concepts/state-management/context": "/guides/complex-state-management#state-sharing",
107+
108+
// solid-docs-next moves/new location for old pages/solid api updates
109+
"/reference/jsx-attributes/on-and-oncapture": "/reference/jsx-attributes/on",
106110

107111
} as const;
108112

src/routes/concepts/components/event-handlers.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Event handlers are functions that are called in response to specific events occu
77

88
Solid provides two ways to add event listeners to the browser:
99

10-
- [`on:__`](/reference/jsx-attributes/on-and-oncapture): adds an event listener to the `element`. This is also known as a _native event_.
10+
- [`on:__`](/reference/jsx-attributes/on): adds an event listener to the `element`. This is also known as a _native event_.
1111
- [`on__`](/reference/jsx-attributes/on_): adds an event listener to the `document` and dispatches it to the `element`. This can be referred to as a _delegated event_.
1212

1313
Delegated events flow through the [_component tree_](/concepts/components/basics#component-trees), and save some resources by performing better on commonly used events.
@@ -85,7 +85,7 @@ This is especially useful when working with a large number of elements, such as
8585
Supported events such as `click`, `input` and `keydown` are just a few examples that are optimized in this way.
8686
To view the full list see the [references below](#list-of-delegated-events).
8787

88-
If you need to attach an event listener to an element that is not supported by Solid's event delegation, such as a custom event in a [custom element](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements), you can use the [`on:__`](/reference/jsx-attributes/on-and-oncapture) form.
88+
If you need to attach an event listener to an element that is not supported by Solid's event delegation, such as a custom event in a [custom element](https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements), you can use the [`on:__`](/reference/jsx-attributes/on) form.
8989

9090
```tsx
9191
<div on:customEvent={handleCustomEvent} />

src/routes/configuration/typescript.mdx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ The following alternative also works when using `Show`:
660660

661661
### Custom event handlers
662662

663-
To handle custom events in Solid, you can use the attributes `on:___` and `oncapture:___`.
663+
To handle custom events in Solid, you can use the attribute `on:___`.
664664
Typing these events requires an extension of Solid's JSX namespace.
665665

666666
```tsx
@@ -678,29 +678,43 @@ declare module "solid-js" {
678678
interface CustomEvents {
679679
Name: NameEvent; // Matches `on:Name`
680680
}
681-
interface CustomCaptureEvents {
682-
Name: NameEvent; // Matches `oncapture:Name`
683-
}
684681
}
685682
}
686683

687684
// Usage
688685
<div on:Name={(event) => console.log("name is", event.detail.name)} />;
689686
```
690687

688+
<Callout>New in v1.9.0</Callout>
689+
690+
It is now possible to use the intersection `EventListenerObject & AddEventListenerOptions` to provide listener options as follows:
691+
692+
```tsx
693+
import type { JSX } from "solid-js"
694+
695+
const handler: JSX.EventHandlerWithOptions<HTMLDivElement, Event> = {
696+
once: true,
697+
handleEvent: (event) => {
698+
console.log("will fire only once");
699+
},
700+
}
701+
702+
// Usage
703+
<div on:click={handler} />;
704+
```
705+
691706
<Callout>
692707
**Note**:
693708
By default, using native events like `mousemove` with the `on` prefix — for example, `<div on:mousemove={e => {}} />` — will trigger a TypeScript error.
694709
This occurs because these native events are not part of Solid's custom event type definitions.
695-
To solve this, `CustomEvents` and `CustomCaptureEvents` interfaces can be extended to include events from the `HTMLElementEventMap`:
710+
To solve this, the `CustomEvents` interface can be extended to include events from the `HTMLElementEventMap`:
696711

697712
To include all native events:
698713

699714
```ts
700715
declare module "solid-js" {
701716
namespace JSX {
702717
interface CustomEvents extends HTMLElementEventMap {}
703-
interface CustomCaptureEvents extends HTMLElementEventMap {}
704718
}
705719
}
706720
```

src/routes/reference/jsx-attributes/data.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"bool.mdx",
77
"innerhtml-or-textcontent.mdx",
88
"on_.mdx",
9-
"on-and-oncapture.mdx",
9+
"on.mdx",
1010
"once.mdx",
1111
"prop.mdx",
1212
"ref.mdx",

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

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: on:*
3+
order: 4
4+
---
5+
6+
For events with capital letters, listener options, or if you need to attach event handlers directly to a DOM element instead of optimized delegating via the document, use `on:*` in place of `on*`.
7+
8+
```tsx
9+
<div on:DOMContentLoaded={(e) => console.log("Welcome!")} />
10+
```
11+
12+
This directly attaches an event handler (via [`addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener)) to the `div`.
13+
14+
<Callout>New in v1.9.0</Callout>
15+
16+
An aditional special syntax that allows full control of [`capture`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture), [`passive`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#passive), [`once`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#once) and [`signal`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#signal) is an intersection or combination of `EventListenerObject` & `AddEventListenerOptions`, as follows:
17+
18+
```tsx
19+
const handler = {
20+
handleEvent(e) {
21+
console.log(e)
22+
},
23+
once:true,
24+
passive:false,
25+
capture:true
26+
}
27+
28+
<div on:wheel={handler} />
29+
30+
// or inline
31+
32+
<div on:click={{passive:true, handleEvent(e) => console.log("Weeeee!")}} />
33+
```
34+
35+
This new syntax replaces the now deprecated `oncapture:` and it's future proof for any posible new event listener options.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Event handlers in Solid typically take the form of `onclick` or `onClick` depend
1111

1212
Conceptually, this example attaches a `click` event listener (via [`addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener)) to the `div`. However, Solid actually handles common UI events that bubble and are composed (such as `click`) at the document level, and then synthetically implements delegation (capturing and bubbling). This improves performance for these common events by reducing the number of event handlers.
1313

14-
Note that `onClick` handles the event `click`; in general, event names get mapped to lower case. If you need to work with event names containing capital letters, see [`on:`](/reference/jsx-attributes/on-and-oncapture) which attaches event handlers directly (also avoiding fancy delegation via document).
14+
Note that `onClick` handles the event `click`; in general, event names get mapped to lower case. If you need to work with event names containing capital letters, or use listener options such once, passive, capture see [`on:`](/reference/jsx-attributes/on) which attaches event handlers directly (also avoiding fancy delegation via document).
1515

1616
Solid also supports passing a two-element array to the event handler to bind a value to the first argument of the event handler. This doesn't use `bind` or create an additional closure, so it is a highly optimized way of delegating events.
1717

@@ -22,7 +22,7 @@ function handler(itemId, e) {
2222

2323
<ul>
2424
<For each={state.list}>{(item) => <li onClick={[handler, item.id]} />}</For>
25-
</ul>
25+
</ul>;
2626
```
2727

2828
Events are never rebound and the bindings are not reactive, as it is expensive to attach and detach listeners. Since event handlers are called like any other function each time an event fires, there is no need for reactivity; shortcut your handler if desired.

0 commit comments

Comments
 (0)