You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sites/svelte-5-preview/src/routes/docs/content/01-api/05-imports.md
+8-21Lines changed: 8 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -117,40 +117,27 @@ Svelte provides reactive `Map`, `Set`, `Date` and `URL` classes. These can be im
117
117
118
118
## `svelte/events`
119
119
120
-
Svelte provides a way of imperatively attaching DOM event listeners to elements using the `on` export from `svelte/events`. This can be used in place of
121
-
imperatively doing `element.addEventListener`, with that benefit that `on` will allow Svelte to co-ordinate the event through its own event delegation system.
120
+
Where possible, event handlers added with [attributes like `onclick`](/docs/event-handlers) use a technique called _event delegation_. It works by creating a single handler for each event type on the root DOM element, rather than creating a handler for each element, resulting in better performance and memory usage.
122
121
123
-
> Svelte 5 has its own internal event delegation system that it uses for certain types of events. Rather than creating hundreds or thousands of invidivual
124
-
> event handlers on each element, Svelte creates a single delegated event handler on the root DOM element and manages the bookkeeping itself. This can have a significant impact
125
-
> on performance and memory usage.
122
+
Delegated event handlers run after other event handlers. In other words, a handler added programmatically with [`addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) will run _before_ a handler added declaratively with `onclick`, regardless of their relative position in the DOM ([demo](/#H4sIAAAAAAAAE41Sy2rDMBD8lUUXJxDiu-sYeugt_YK6h8RaN6LyykgrQzH6965shxJooQc_RhrNzA6aVW8sBlW9zYouA6pKPY-jOij-GjMIE1pGwcFF3-WVOnTejNy01LIZRucZZnD06iIxJOi9G6BYjxVPmZQfiwzaTBkL2ti73R5ODcwLiftIHRtHcLuQtuhlc9tpuSyBbyZAuLloNfhIELBzpO8E-Q_O4tG6j13hIqO_y0BvPOpiv0bhtJ1Y3pLoeNH6ZULiswmMJLZFZ033WRzuAvstdMseOXqCh9SriMfBTfgPnZxg-aYM6_KnS6pFCK6GdJVHPc0C01JyfY0slUnHi-JpfgjwSzUycdgmfOjFEP3RS1qdhJ8dYMDFt1yNmxxU0jRyCwanTW9Qq4p9xPSevgHI3m43QAIAAA==)). It also means that calling `event.stopPropagation()` inside a declarative handler _won't_ prevent the programmatic handler (created inside an action, for example) from running.
126
123
127
-
```js
128
-
// @filename: index.ts
129
-
const element:Element=null as any;
130
-
// ---cut---
131
-
import { on } from'svelte/events';
132
-
133
-
on(element, 'click', () => {
134
-
console.log('element was clicked');
135
-
});
136
-
```
137
-
138
-
Additionally, `on` returns a function that easily allows for removal of the attached event handler:
124
+
To preserve the relative order, use `on` rather than `addEventListener` ([demo](/#H4sIAAAAAAAAE3VRy26DMBD8lZUvECkqdwpI_YB-QdJDgpfGqlkjex2pQv73rnmoStQeMB52dnZmmdVgLAZVn2ZFlxFVrd6mSR0Vf08ZhDtaRsHBRd_nL03ovZm4O9OZzTg5zzCDo3cXiSHB4N0IxdpWvD6RnuoV3pE4rLT8WGTQ5p6xoE20LA_QdjAvJB4i9WxE6nYhbdFLcaucuaqAbyZAuLloNfhIELB3pHeC3IOz-GLdZ1m4yOh3GRiMR10cViucto7l9MjRk9gvxdsRit6a_qs47q1rT8qvpvpdDjXChqshXWdT7SwwLVtrrpElnAguSu38EPCPEOItbF4eEhiifxKkdZLw8wQYcZlbrYO7bFTcdPJbR6fNYFCrmn3E9JF-AJZOg9MRAgAA)):
139
125
140
126
```js
141
127
// @filename: index.ts
142
128
const element:Element=null as any;
143
129
// ---cut---
144
130
import { on } from'svelte/events';
145
131
146
-
constremove=on(element, 'click', () => {
132
+
constoff=on(element, 'click', () => {
147
133
console.log('element was clicked');
148
134
});
149
-
// ...
150
-
remove();
135
+
136
+
// later, if we need to remove the event listener:
137
+
off();
151
138
```
152
139
153
-
> Note: `on` also accepts 4th optional argument for defining the options for the event handler. This matches that of the options argument (`EventListenerOptions`) for `addEventListener`.
140
+
`on` also accepts an optional fourth argument which matches the options argument for `addEventListener`.
0 commit comments