Skip to content

Commit fa8ffbe

Browse files
committed
event options
1 parent c8e7222 commit fa8ffbe

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

2-ui/2-events/01-introduction-browser-events/article.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Web-standard developers understood that long ago and suggested an alternative wa
216216
The syntax to add a handler:
217217

218218
```js
219-
element.addEventListener(event, handler[, phase]);
219+
element.addEventListener(event, handler[, options]);
220220
```
221221

222222
`event`
@@ -225,15 +225,17 @@ element.addEventListener(event, handler[, phase]);
225225
`handler`
226226
: The handler function.
227227

228-
`phase`
229-
: An optional argument, the "phase" for the handler to work. To be covered later. Usually we don't use it.
228+
`options`
229+
: An additional optional object with properties:
230+
- `once`: if `true`, then the listener is automatically removed after it triggers.
231+
- `capture`: the phrase where to handle the event, to be covered later in the chapter <info:bubbling-and-capturing>. For historical reasons, `options` can also be `false/true`, that's the same as `{capture: false/true}`.
232+
- `passive`: if `true`, then the handler will not `preventDefault()`, we'll cover that later in <info:default-browser-action>.
230233

231-
To remove the handler, use `removeEventListener`:
232234

235+
To remove the handler, use `removeEventListener`:
233236

234237
```js
235-
// exactly the same arguments as addEventListener
236-
element.removeEventListener(event, handler[, phase]);
238+
element.removeEventListener(event, handler[, options]);
237239
```
238240

239241
````warn header="Removal requires the same function"

2-ui/2-events/02-bubbling-and-capturing/article.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ That is: for a click on `<td>` the event first goes through the ancestors chain
136136

137137
Handlers added using `on<event>`-property or using HTML attributes or using `addEventListener(event, handler)` don't know anything about capturing, they only run on the 2nd and 3rd phases.
138138

139-
To catch an event on the capturing phase, we need to set the 3rd argument of `addEventListener` to `true`.
139+
To catch an event on the capturing phase, we need to set the event options of `addEventListener` to `{capture: true}` (or just `true`).
140140

141-
There are two possible values for that optional last argument:
141+
There are two possible values `capture` option:
142142

143143
- If it's `false` (default), then the handler is set on the bubbling phase.
144144
- If it's `true`, then the handler is set on the capturing phase.
@@ -182,12 +182,16 @@ Please note that `P` shows up two times: at the end of capturing and at the star
182182

183183
There's a property `event.eventPhase` that tells us the number of the phase on which the event was caught. But it's rarely used, because we usually know it in the handler.
184184

185+
```smart header="To remove the handler, `removeEventListener` needs the same phase"
186+
If we `addEventListener(..., true)`, then we should mention the same phase in `removeEventListener(..., true)` to correctly remove the handler.
187+
```
188+
185189
## Summary
186190
187191
The event handling process:
188192
189193
- When an event happens -- the most nested element where it happens gets labeled as the "target element" (`event.target`).
190-
- Then the event first moves from the document root down to the `event.target`, calling handlers assigned with `addEventListener(...., true)` on the way.
194+
- Then the event first moves from the document root down to the `event.target`, calling handlers assigned with `addEventListener(...., true)` on the way (`true` is a shorthand for `{capture: true}`).
191195
- Then the event moves from `event.target` up to the root, calling handlers assigned using `on<event>` and `addEventListener` without the 3rd argument or with the 3rd argument `false`.
192196
193197
Each handler can access `event` object properties:

2-ui/2-events/04-default-browser-action/article.md

+16
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ But if you click the second one, there's no focus.
9393

9494
That's because the browser action is canceled on `mousedown`. The focusing is still possible if we use another way to enter the input. For instance, the `key:Tab` key to switch from the 1st input into the 2nd. But not with the mouse click any more.
9595

96+
## The "passive" handler option
97+
98+
The optional `passive: true` option of `addEventListener` signals the browser that the handler is not going to call `preventDefault()`.
99+
100+
Why that may be needed?
101+
102+
There are some events like `touchmove` on mobile devices (when the user moves their finger across the screen), that cause scrolling by default, but that scrolling can be prevented using `preventDefault()` in the handler.
103+
104+
So when the browser detects such event, it has first to process all handlers, and then if `preventDefault` is not called anywhere, it can proceed with scrolling. That may cause unnecessary delays and "jitters" in the UI.
105+
106+
The `passive: true` options tells the browser that the handler is not going to cancel scrolling. Then browser scrolls immediately providing a maximally fluent experience, and the event is handled by the way.
107+
108+
For some browsers (Firefox, Chrome), `passive` is `true` by default for `touchstart` and `touchmove` events.
109+
96110

97111
## event.defaultPrevented
98112

@@ -215,6 +229,8 @@ All the default actions can be prevented if we want to handle the event exclusiv
215229

216230
To prevent a default action -- use either `event.preventDefault()` or `return false`. The second method works only for handlers assigned with `on<event>`.
217231

232+
The `passive: true` option of `addEventListener` tells the browser that the action is not going to be prevented. That's useful for some mobile events, like `touchstart` and `touchmove`, to tell the browser that it should not wait for all handlers to finish before scrolling.
233+
218234
If the default action was prevented, the value of `event.defaultPrevented` becomes `true`, otherwise it's `false`.
219235

220236
```warn header="Stay semantic, don't abuse"

0 commit comments

Comments
 (0)