Events that Cross Shadow DOM Boundary

Web ComponentsShadow DOMEvents

The events that do cross the shadow boundary are:

category

events

focus

blur, focus, focusin, focusout

mouse

click, dblclick, mousedown, mouseenter, mousemove, ...

wheel

wheel

input

beforeinput, input

keyboard

keydown, keyup

composition

compositionstart, compositionupdate, compositionend

drag

dragstart, drag, dragend, drop, ...

Built-in Events

Most (built-in) events successfully bubble through a shadow DOM boundary (with event.composed == true).

There are few events that do not:

  • 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

如果要讓 CustomEvent 跨過 shadow DOM boundary,必須要設定: {bubbles: true, composed: true} ⭐️

If composed: false (default), consumers won't be able to listen for the event outside of your shadow root.

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:

  • dispatch it on the shadow host

  • set composed: false.

Then it’s out of the component shadow DOM, but won’t bubble up to higher-level DOM.

Last updated