Skip to content

Commit 574fab5

Browse files
committed
Update "Shadow DOM and events" article
1 parent 5e280a3 commit 574fab5

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

8-web-components/7-shadow-dom-events/article.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
The idea behind shadow tree is to encapsulate internal implementation details of a component.
44

5-
Let's say, a click event happens inside a shadow DOM of `<user-card>` component. But scripts in the main document have no idea about the shadow DOM internals, especially if the component comes from a 3rd-party library or so.
5+
Let's say, a click event happens inside a shadow DOM of `<user-card>` component. But scripts in the main document have no idea about the shadow DOM internals, especially if the component comes from a 3rd-party library.
66

7-
So, to keep things simple, the browser *retargets* the event.
7+
So, to keep the details encapsulated, the browser *retargets* the event.
88

99
**Events that happen in shadow DOM have the host element as the target, when caught outside of the component.**
1010

@@ -35,11 +35,11 @@ If you click on the button, the messages are:
3535
1. Inner target: `BUTTON` -- internal event handler gets the correct target, the element inside shadow DOM.
3636
2. Outer target: `USER-CARD` -- document event handler gets shadow host as the target.
3737

38-
Event retargeting is a great thing to have, because the outer document doesn't have no know about component internals. From its point of view, the event happened on `<user-card>`.
38+
Event retargeting is a great thing to have, because the outer document doesn't have to know about component internals. From its point of view, the event happened on `<user-card>`.
3939

4040
**Retargeting does not occur if the event occurs on a slotted element, that physically lives in the light DOM.**
4141

42-
For example, if a user clicks on `<span slot="username">` in the example below, the event target is exactly this element, for both shadow and light handlers:
42+
For example, if a user clicks on `<span slot="username">` in the example below, the event target is exactly this `span` element, for both shadow and light handlers:
4343

4444
```html run autorun="no-epub" untrusted height=60
4545
<user-card id="userCard">
@@ -75,7 +75,7 @@ For purposes of event bubbling, flattened DOM is used.
7575

7676
So, if we have a slotted element, and an event occurs somewhere inside it, then it bubbles up to the `<slot>` and upwards.
7777

78-
The full path to the original event target, with all the shadow root elements, can be obtained using `event.composedPath()`. As we can see from the name of the method, that path is taken after the composition.
78+
The full path to the original event target, with all the shadow elements, can be obtained using `event.composedPath()`. As we can see from the name of the method, that path is taken after the composition.
7979

8080
In the example above, the flattened DOM is:
8181

@@ -119,7 +119,7 @@ All touch events and pointer events also have `composed: true`.
119119
120120
There are some events that have `composed: false` though:
121121
122-
- `mouseenter`, `mouseleave` (they also do not bubble),
122+
- `mouseenter`, `mouseleave` (they do not bubble at all),
123123
- `load`, `unload`, `abort`, `error`,
124124
- `select`,
125125
- `slotchange`.
@@ -189,4 +189,4 @@ These events can be caught only on elements within the same DOM.
189189

190190
If we dispatch a `CustomEvent`, then we should explicitly set `composed: true`.
191191

192-
Please note that in case of nested components, composed events bubble through all shadow DOM boundaries. So, if an event is intended only for the immediate enclosing component, we can also dispatch it on the shadow host. Then it's out of the shadow DOM already.
192+
Please note that in case of nested components, one shadow DOM may be nested into another. In that case composed events bubble through all shadow DOM boundaries. So, if an event is intended only for the immediate enclosing component, we can also dispatch it on the shadow host and set `composed: false`. Then it's out of the component shadow DOM, but won't bubble up to higher-level DOM.

0 commit comments

Comments
 (0)