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: website/docs/concepts/html/events.md
+40-4Lines changed: 40 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -141,7 +141,6 @@ listens for `click` events.
141
141
In this section **target ([Event.target](https://developer.mozilla.org/en-US/docs/Web/API/Event/target))**
142
142
is always referring to the element at which the event was dispatched from.
143
143
144
-
145
144
This will **not** always be the element at which the `Callback` is placed.
146
145
:::
147
146
@@ -257,6 +256,7 @@ impl Component for Comp {
257
256
}
258
257
}
259
258
```
259
+
260
260
:::tip
261
261
Use `batch_callback` when you want to conditionally return a value from a `Callback`.
262
262
:::
@@ -274,7 +274,6 @@ At this point you might be thinking... when is the dangerous version ok to use?
274
274
is safe<sup>1</sup> as we've set the `Callback` on to an element with no children so the target can
275
275
only be that same element.
276
276
277
-
278
277
_<sup>1</sup> As safe as anything can be when JS land is involved._
279
278
280
279
### Using `TargetCast`
@@ -357,12 +356,12 @@ impl Component for Comp {
357
356
}
358
357
}
359
358
```
359
+
360
360
If you followed the advice above and read about `JsCast`, or know the trait, you can probably
361
361
see that `TargetCast::target_dyn_into` feels similar to `JsCast::dyn_into` but specifically
362
362
does the cast on the target of the event. `TargetCast::target_unchecked_into` is similar to
363
363
`JsCast::unchecked_into`, and as such all the same warnings above `JsCast` apply to `TargetCast`.
364
364
365
-
366
365
### Using `NodeRef`
367
366
368
367
[`NodeRef`](../components/refs.md) can be used instead of querying the event given to a `Callback`.
@@ -421,6 +420,7 @@ impl Component for Comp {
421
420
}
422
421
}
423
422
```
423
+
424
424
Using `NodeRef`, you can ignore the event and use the `NodeRef::cast` method to get an
425
425
`Option<HtmlInputElement>` - this is optional as calling `cast` before the `NodeRef` has been
426
426
set, or when the type doesn't match will return `None`.
@@ -491,6 +491,42 @@ impl Component for Comp {
491
491
Which approach you take depends on your component and your preferences, there is no _blessed_ way
492
492
per se.
493
493
494
+
## Event bubbling
495
+
496
+
:::caution
497
+
498
+
`event.stop_propagation()` will not work! Read further for solutions
499
+
500
+
:::
501
+
502
+
Yew is in early stages of optimizing and rewriting its events system.
503
+
Currently all events are managed by using 1 event listener on the top html element available to yew.
504
+
Then yew itself works through the event flow.
505
+
When you want to stop propagation there are the following two ways.
506
+
507
+
### set_cancel_bubble
508
+
509
+
To stop propagation, `set_cancel_bubble(true)` on `web_sys::Event` can be used.
510
+
511
+
```rust ,ignore
512
+
// In your event handler
513
+
letevent:Event=mouse_event.dyn_into().unwrap();
514
+
event.set_cancel_bubble(true);
515
+
```
516
+
517
+
### set_event_bubbling
518
+
519
+
To improve yew performance it is recommended to use `set_event_bubbling` before the start of the app to disable bubbling all together, though there are known issues with some in-house yew components like `yew_router::Link`.
520
+
521
+
```rust ,ignore
522
+
useyew::prelude::*;
523
+
524
+
fnmain() {
525
+
yew::set_event_bubbling(false);
526
+
yew::start_app::<app::App>();
527
+
}
528
+
```
529
+
494
530
## Manual event listener
495
531
496
532
You may want to listen to an event that is not supported by Yew's `html` macro, see the
@@ -693,4 +729,4 @@ component is about to be destroyed as the `EventListener` has a `drop` implement
693
729
which will remove the event listener from the element.
0 commit comments