Lesson 4 of 9 · 26m
Shadow DOM Mechanics: Encapsulation, Retargeting, and Focus
What shadow roots actually isolate, how events are retargeted across boundaries, and the focus behavior that surprises even experienced teams.
Shadow DOM is not a security boundary and it is not total isolation. It is a scoped DOM tree with composition, style, focus, and event rules. Once those rules are clear, behavior that looks random — wrong targets, missing focus, impossible selectors — becomes predictable.
A shadow root is a second tree
class SearchBox extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" }).innerHTML = `
<input part="input" type="search" aria-label="Search">
<button part="button">Search</button>`;
}
}Document selectors do not descend into this tree, and shadow styles do not leak outward by default. "closed" mode merely hides the convenient shadowRoot property; it is not meaningful protection. Prefer open roots unless you understand the testing and integration cost of closing them.
Events are retargeted, not lost
host.addEventListener("click", (event) => {
console.log(event.target); // <search-box>
console.log(event.composedPath()); // button, shadowRoot, host, ...
});Retargeting preserves encapsulation for listeners outside the root. composed controls whether the event crosses the boundary; bubbles controls whether it climbs afterward. Plain focus is not composed, which is why it often appears to vanish. Use focusin or dispatch a semantic custom event when hosts need notification.
Slots compose rendering without moving nodes
Light DOM children are assigned to slots visually, but they remain in the light DOM. Therefore this fails:
shadowRoot.querySelector("slot").querySelector("span"); // nullUse slot.assignedNodes() or slot.assignedElements(), usually after slotchange. Host-page listeners and styles still apply because ownership never moved. This distinction is the core mental model.
Focus crosses boundaries in composed order
document.activeElement may report the host while actual focus is on an internal input. If your element wraps a control, delegate focus deliberately:
focus(options) {
this.shadowRoot.querySelector("input")?.focus(options);
}Keyboard traversal follows the composed tree, not a flat querySelectorAll list. Avoid putting focusable controls both outside and inside the same logical control unless you have tested the sequence with a keyboard.
Isolation has deliberate holes
Inherited properties still inherit: color, font-family, and CSS custom properties cross the boundary. That is the correct collaboration model. Establish broad typography outside, expose precise hooks inside, and don't try to make the component immune to the page it belongs to.
Field notes
event.targetdepends on where the listener is attached; useevent.composedPath()[0]when debugging the origin.- Labels using
forcannot cross shadow boundaries. Keep label/control pairs in one tree or design ARIA deliberately. - Closed roots complicate tests, browser extensions, and accessibility tooling while adding little real protection.
- Shadow DOM does not sanitize anything. Never interpolate untrusted strings into
innerHTML.
Was this lesson helpful?