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
: 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>.
Copy file name to clipboardExpand all lines: 2-ui/2-events/02-bubbling-and-capturing/article.md
+7-3
Original file line number
Diff line number
Diff line change
@@ -136,9 +136,9 @@ That is: for a click on `<td>` the event first goes through the ancestors chain
136
136
137
137
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.
138
138
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`).
140
140
141
-
There are two possible values for that optional last argument:
141
+
There are two possible values `capture` option:
142
142
143
143
- If it's `false` (default), then the handler is set on the bubbling phase.
144
144
- 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
182
182
183
183
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.
184
184
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
+
185
189
## Summary
186
190
187
191
The event handling process:
188
192
189
193
- 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}`).
191
195
- 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`.
192
196
193
197
Each handler can access `event` object properties:
Copy file name to clipboardExpand all lines: 2-ui/2-events/04-default-browser-action/article.md
+16
Original file line number
Diff line number
Diff line change
@@ -93,6 +93,20 @@ But if you click the second one, there's no focus.
93
93
94
94
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.
95
95
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
+
96
110
97
111
## event.defaultPrevented
98
112
@@ -215,6 +229,8 @@ All the default actions can be prevented if we want to handle the event exclusiv
215
229
216
230
To prevent a default action -- use either `event.preventDefault()` or `return false`. The second method works only for handlers assigned with `on<event>`.
217
231
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
+
218
234
If the default action was prevented, the value of `event.defaultPrevented` becomes `true`, otherwise it's `false`.
0 commit comments