# Shadow DOM and events The idea behind shadow tree is to encapsulate internal implementation details of a component. Let's say, a click event happens inside a shadow DOM of `` 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. So, to keep the details encapsulated, the browser *retargets* the event. **Events that happen in shadow DOM have the host element as the target, when caught outside of the component.** Here's a simple example: ```html run autorun="no-epub" untrusted height=60 ``` If you click on the button, the messages are: 1. Inner target: `BUTTON` -- internal event handler gets the correct target, the element inside shadow DOM. 2. Outer target: `USER-CARD` -- document event handler gets shadow host as the target. 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 ``. **Retargeting does not occur if the event occurs on a slotted element, that physically lives in the light DOM.** For example, if a user clicks on `` in the example below, the event target is exactly this `span` element, for both shadow and light handlers: ```html run autorun="no-epub" untrusted height=60 *!* John Smith */!* ``` If a click happens on `"John Smith"`, for both inner and outer handlers the target is ``. That's an element from the light DOM, so no retargeting. On the other hand, if the click occurs on an element originating from shadow DOM, e.g. on `Name`, then, as it bubbles out of the shadow DOM, its `event.target` is reset to ``. ## Bubbling, event.composedPath() For purposes of event bubbling, flattened DOM is used. So, if we have a slotted element, and an event occurs somewhere inside it, then it bubbles up to the `` and upwards. 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. In the example above, the flattened DOM is: ```html #shadow-root
Name: John Smith
``` So, for a click on ``, a call to `event.composedPath()` returns an array: [`span`, `slot`, `div`, `shadow-root`, `user-card`, `body`, `html`, `document`, `window`]. That's exactly the parent chain from the target element in the flattened DOM, after the composition. ```warn header="Shadow tree details are only provided for `{mode:'open'}` trees" If the shadow tree was created with `{mode: 'closed'}`, then the composed path starts from the host: `user-card` and upwards. That's the similar principle as for other methods that work with shadow DOM. Internals of closed trees are completely hidden. ``` ## event.composed Most events successfully bubble through a shadow DOM boundary. There are few events that do not. This is governed by the `composed` event object property. If it's `true`, then the event does cross the boundary. Otherwise, it only can be caught from inside the shadow DOM. If you take a look at [UI Events specification](https://www.w3.org/TR/uievents), most events have `composed: true`: - `blur`, `focus`, `focusin`, `focusout`, - `click`, `dblclick`, - `mousedown`, `mouseup` `mousemove`, `mouseout`, `mouseover`, - `wheel`, - `beforeinput`, `input`, `keydown`, `keyup`. All touch events and pointer events also have `composed: true`. There are some events that have `composed: false` though: - `mouseenter`, `mouseleave` (they do not bubble at all), - `load`, `unload`, `abort`, `error`, - `select`, - `slotchange`. These events can be caught only on elements within the same DOM, where the event target resides. ## Custom events When we dispatch custom events, we need to set both `bubbles` and `composed` properties to `true` for it to bubble up and out of the component. For example, here we create `div#inner` in the shadow DOM of `div#outer` and trigger two events on it. Only the one with `composed: true` makes it outside to the document: ```html run untrusted height=0
``` ## Summary Events only cross shadow DOM boundaries if their `composed` flag is set to `true`. Built-in events mostly have `composed: true`, as described in the relevant specifications: - UI Events . - Touch Events . - Pointer Events . - ...And so on. Some built-in events that have `composed: false`: - `mouseenter`, `mouseleave` (also do not bubble), - `load`, `unload`, `abort`, `error`, - `select`, - `slotchange`. These events can be caught only on elements within the same DOM. If we dispatch a `CustomEvent`, then we should explicitly set `composed: true`. 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.