Events that Cross Shadow DOM Boundary

Web Components โŸฉ Shadow DOM โŸฉ Events โŸฉ

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

Was this helpful?