liveBook · Manning

Closed mode

As explained in Web Components in Action

Closed mode is a Shadow DOM configuration in which a host element does not expose its shadow root through the standard shadowRoot property.

Closed mode creates a Shadow DOM boundary whose shadow root is not exposed through the host element’s standard `shadowRoot` property. It helps block ordinary outside access to a component’s internal DOM, but it is aimed at accidental intrusion rather than strong protection against malicious users. section 8.2 section 8.4

Create a closed shadow root

Use the returned root immediately instead of reading it back from the host: `connectedCallback() { const root = this.attachShadow({ mode: 'closed' }); root.innerHTML = '<div class="inside-component">My Component</div>'; }` A local variable preserves access for component setup while leaving the standard `shadowRoot` property unavailable to outside code. Storing that root on a public property would undermine the boundary. section 8.2

Closed mode is therefore an option for DOM encapsulation, not a complete security barrier. An open shadow root is presented as the more practical convention when controlled inspection or emergency access matters. section 8.4

How Web Components in Action covers it

How deep it goes
Explains configuration, local-root access, and practical limitations, but stops short of treating closed mode as robust security.
Examples
Demonstrates closed-root creation, local root storage, public root exposure, and attempted outside DOM queries.
What it assumes
Assumes familiarity with custom elements, connectedCallback, DOM trees, and the Shadow DOM’s host-root boundary.
What you can do after
Apply closed mode by retaining the returned shadow root locally and evaluating its limits as an encapsulation technique.
How it connects
Connects Shadow DOM with shadow roots, encapsulation, custom elements, and connectedCallback.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.