Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 39 ยท Shadow DOM

Open and Closed Shadow Roots

Compare open and closed shadow roots, learn what mode controls, how to keep a private reference, and use the delegatesFocus, clonable and serializable options.

When you call attachShadow, the one required option is mode. It decides whether code outside the component can reach into the shadow tree through the shadowRoot property. The choice is smaller than it sounds, and many developers pick the wrong default because they believe it is about security. This lesson explains what mode really does, plus the other options of attachShadow.

mode: "open"

With an open root, element.shadowRoot returns the ShadowRoot, so anyone can inspect or modify the internals.

JavaScript
const root = host.attachShadow({ mode: "open" });
host.shadowRoot === root;   // true

This is what almost every component library uses. It makes DevTools, tests, analytics scripts and accessibility tools able to see inside, and it lets you write this.shadowRoot.querySelector(...) without keeping your own reference.

mode: "closed"

With a closed root, element.shadowRoot is null from the outside. The tree still works, but only the code that holds the reference returned by attachShadow can use it.

Open versus closed

The closed host prints null for shadowRoot, yet both paragraphs are rendered normally. To use a closed root inside a class, you store the reference in a private field:

JavaScript
class SecretCard extends HTMLElement {
  #root;
  constructor() {
    super();
    this.#root = this.attachShadow({ mode: "closed" });
    this.#root.innerHTML = "<p>Hello</p>";
  }
  update(text) {
    this.#root.querySelector("p").textContent = text;
  }
}

Is closed more secure?

No. closed is obscurity, not security. Anyone who runs script on the page can get around it: they can patch Element.prototype.attachShadow before your code runs, intercept the returned root, or read the internals through accessibility APIs and event paths. Browsers themselves use closed-style internals for built-ins like <video> controls, but they enforce that at a deeper level than page JavaScript can.

Practical downsides of closed:

  • Testing and debugging become harder (DevTools still shows the tree, but code cannot).
  • Page-level helpers, such as autofill and extensions, cannot see inside.
  • Subclasses and mixins cannot reach the root without extra plumbing.
  • You must carry the reference yourself, which is easy to forget.

Recommendation: use "open" by default. Choose "closed" only when you want to signal that the internals are private and you accept the trade-offs.

Keeping your own reference

Even with an open root you can store the reference. It is slightly faster and works for either mode:

JavaScript
constructor() {
  super();
  this.root = this.attachShadow({ mode: "open" });
}

Other attachShadow options

Besides mode, three options are commonly useful.

delegatesFocus

When true, clicking non-focusable parts of the shadow tree focuses the first focusable element inside it, and the host matches :focus when any internal element has focus. It also makes host.focus() forward to that first focusable element.

delegatesFocus

Calling host.focus() puts the cursor in the input. Without delegatesFocus, the host itself is not focusable, and the call would do nothing. Note that document.activeElement reports the host (label-field) because of retargeting, while shadowRoot.activeElement reports the internal input.

clonable and serializable

  • clonable: true makes host.cloneNode() copy the shadow root too. By default, cloning a host loses the shadow tree.
  • serializable: true lets host.getHTML({ serializableShadowRoots: true }) include a declarative <template shadowrootmode> for this root, so it can be sent as HTML (see the Declarative Shadow DOM lesson).
JavaScript
this.attachShadow({ mode: "open", clonable: true, serializable: true });

Both options are supported in current browsers.

slotAssignment

slotAssignment: "manual" switches from name-based slotting to manual assignment with slot.assign(...). It is rare and covered in the slots chapter.

Quick comparison

open closed
el.shadowRoot The root null
DevTools and tests Easy Harder
Security None None (only obscurity)
Typical use Almost all components Signalling "private internals"

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does el.shadowRoot return for a closed shadow root?

    Closed roots are hidden from the shadowRoot property, but the code that holds the reference can still use it.
  2. 2. Is mode: 'closed' a real security boundary?

    Page scripts can patch attachShadow or use other paths. It discourages casual access only.
  3. 3. What does delegatesFocus: true do?

    It forwards focus into the shadow tree and makes the host match :focus when an internal element is focused.
  4. 4. Which mode is recommended for most components?

    Open roots are easier to debug and test, and closed mode adds no real security.

Summary

  • mode is "open" (exposes el.shadowRoot) or "closed" (returns null outside).
  • Closed is obscurity, not security; prefer open unless you have a reason.
  • Keep your own reference to the root if you want a fast, mode-independent handle.
  • delegatesFocus forwards focus into the shadow tree.
  • clonable and serializable let a shadow tree survive cloning and HTML serialization.

Next: Style Encapsulation