Course contentsShow
← Web Development
Lesson 2471 of 2,98244. Web ComponentsFree lesson

Event Retargeting in Shadow DOM

How events bubble through shadow boundaries and how event.target is retargeted.

Event Retargeting in Shadow DOM

You'll learn: How Shadow DOM automatically rewrites event targets to preserve encapsulation when events cross shadow boundaries.

What Is Event Retargeting?

When an event occurs inside a Shadow DOM and bubbles up to the light DOM (the regular document), the browser changes the event.target property to maintain encapsulation. This is called event retargeting.

If the original target was an element inside the shadow tree, listeners outside the shadow root would see the target as the host element itself—not the internal shadow element. This prevents external code from accidentally accessing your component's private implementation details.

How It Works

Imagine a <my-button> custom element with a shadow root containing a <button> element inside. When a user clicks the internal <button>:

  1. Inside the shadow root: event.target points to the actual <button> element
  2. Outside the shadow root (in the light DOM): event.target is retargeted to <my-button>—the shadow host

The event still bubbles normally through the DOM tree, but the target reference is adjusted at each shadow boundary to protect encapsulation.

Event Phases and Retargeting

During the capture and bubble phases, event.target changes depending on where your listener lives. Use event.composedPath() to see the full path the event travels, including shadow-internal elements—useful for debugging but respects closed shadow roots.

Key Takeaway: Event retargeting automatically replaces event.target with the shadow host when events cross shadow boundaries, keeping your component's internals hidden while allowing events to bubble naturally through the DOM tree.