Skip to content

Commit c0fbb24

Browse files
committed
tweaks
1 parent b639581 commit c0fbb24

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

documentation/docs/99-legacy/10-legacy-on.md

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@
22
title: on:
33
---
44

5-
```svelte
6-
<!--- copy: false --->
7-
on:eventname={handler}
8-
```
9-
10-
```svelte
11-
<!--- copy: false --->
12-
on:eventname|modifiers={handler}
13-
```
5+
In runes mode, event handlers are just like any other attribute or prop.
146

15-
Use the `on:` directive to listen to DOM events.
7+
In legacy mode, we use the `on:` directive:
168

179
```svelte
1810
<!--- file: App.svelte --->
@@ -30,15 +22,15 @@ Use the `on:` directive to listen to DOM events.
3022
</button>
3123
```
3224

33-
Handlers can be declared inline with no performance penalty. As with attributes, directive values may be quoted for the sake of syntax highlighters.
25+
Handlers can be declared inline with no performance penalty:
3426

3527
```svelte
3628
<button on:click={() => (count += 1)}>
3729
count: {count}
3830
</button>
3931
```
4032

41-
Add _modifiers_ to DOM events with the `|` character.
33+
Add _modifiers_ to element event handlers with the `|` character.
4234

4335
```svelte
4436
<form on:submit|preventDefault={handleSubmit}>
@@ -64,66 +56,81 @@ Modifiers can be chained together, e.g. `on:click|once|capture={...}`.
6456
If the `on:` directive is used without a value, the component will _forward_ the event, meaning that a consumer of the component can listen for it.
6557

6658
```svelte
67-
<button on:click> The component itself will emit the click event </button>
59+
<button on:click>
60+
The component itself will emit the click event
61+
</button>
6862
```
6963

7064
It's possible to have multiple event listeners for the same event:
7165

7266
```svelte
7367
<!--- file: App.svelte --->
7468
<script>
75-
let counter = 0;
69+
let count = 0;
70+
7671
function increment() {
77-
counter = counter + 1;
72+
count += 1;
7873
}
7974
8075
/** @param {MouseEvent} event */
81-
function track(event) {
82-
trackEvent(event);
76+
function log(event) {
77+
console.log(event);
8378
}
8479
</script>
8580
86-
<button on:click={increment} on:click={track}>Click me!</button>
81+
<button on:click={increment} on:click={log}>
82+
clicks: {count}
83+
</button>
8784
```
8885

89-
> [!NOTE]
90-
> In Svelte 5+, use event attributes instead
91-
> ```svelte
92-
> <button onclick={() => alert('clicked')}>click me</button>
93-
> ```
94-
9586
## Component events
9687

97-
Component events created with [`createEventDispatcher`](svelte#createEventDispatcher) create a `CustomEvent`. These events do not bubble. The detail argument corresponds to the `CustomEvent.detail` property and can contain any type of data.
88+
Components can dispatch events by creating a _dispatcher_ when they are initialised:
9889

9990
```svelte
91+
<!--- file: Stepper.svelte -->
10092
<script>
10193
import { createEventDispatcher } from 'svelte';
102-
10394
const dispatch = createEventDispatcher();
10495
</script>
10596
106-
<button on:click={() => dispatch('notify', 'detail value')}>Fire Event</button>
97+
<button on:click={() => dispatch('decrement')}>decrement</button>
98+
<button on:click={() => dispatch('increment')}>increment</button>
10799
```
108100

109-
Events dispatched from child components can be listened to in their parent. Any data provided when the event was dispatched is available on the `detail` property of the event object.
101+
`dispatch` creates a [`CustomEvent`](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent). If a second argument is provided, it becomes the `detail` property of the event object.
102+
103+
A consumer of this component can listen for the dispatched events:
110104

111105
```svelte
112106
<script>
113-
function callbackFunction(event) {
114-
console.log(`Notify fired! Detail: ${event.detail}`);
115-
}
107+
import Stepper from './Stepper.svelte';
108+
109+
let n = 0;
116110
</script>
117111
118-
<Child on:notify={callbackFunction} />
112+
<Stepper
113+
on:decrement={() => n -= 1}
114+
on:increment={() => n += 1}
115+
/>
116+
117+
<p>n: {n}</p>
119118
```
120119

120+
Component events do not bubble — a parent component can only listen for events on its immediate children.
121+
122+
Other than `once`, modifiers are not valid on component event handlers.
123+
121124
> [!NOTE]
122-
> If you're planning on migrating to Svelte 5, use callback props instead. This will make upgrading easier as `createEventDispatcher` is deprecated
125+
> If you're planning an eventual migration to Svelte 5, use callback props instead. This will make upgrading easier as `createEventDispatcher` is deprecated:
126+
>
123127
> ```svelte
128+
> <!--- file: Stepper.svelte --->
124129
> <script>
125-
> export let notify;
130+
> export let decrement;
131+
> export let increment;
126132
> </script>
127-
>
128-
> <button on:click={() => notify('detail value')}>Fire Event</button>
133+
>
134+
> <button on:click={decrement}>decrement</button>
135+
> <button on:click={increment}>increment</button>
129136
> ```

0 commit comments

Comments
 (0)