liveBook · Manning

Shadow root

As explained in Vanilla Web

A shadow root is the encapsulated DOM subtree attached to a shadow host for implementing a web component's internal structure and presentation.

A custom element that renders into the page DOM shares that DOM’s styling and selectors. A shadow root gives the element an internal DOM tree behind a boundary, so page CSS does not accidentally style its internals and internal CSS does not style the page. The element carrying that tree is the shadow host. section 7.8 section 7.8.2

Attach and render

In a custom element constructor, call `this.attachShadow({ mode: 'open' })`. Render internal markup through `this.shadowRoot`, not `this.innerHTML`: writing to the host adds light-DOM nodes, which appear in the shadow tree only when projected through a `<slot>`. Continue using the host for public attributes, properties, and events. section 7.8.1

Make the boundary an explicit design choice:

  • Use `open` when code outside the component needs `host.shadowRoot`, including tests that query internal nodes. With `closed`, that property returns `null`; keep the root returned by `attachShadow()` if component code needs a reference. Closed mode does not provide additional style isolation or a security boundary. section 7.8.1 section 9.7.2
  • Style the tree inside the shadow root. Use `:host` to style the host, and expose deliberate styling interfaces such as CSS custom properties or `::part` when consumers need customization. section 7.8.3 section 7.13
  • Use slots when the host page must supply content. For slot tests, inspect assigned elements rather than inferring projection from outer HTML. section 7.8.1 section 9.7.2

A shadow root can also be declared in HTML with `<template shadowrootmode="open">`. The browser creates it while parsing, before the custom element class upgrades, so initial rendering need not wait for JavaScript. Use `shadowrootmode`, not the older `shadowroot` attribute. section 9.6.1

<product-card>
  <template shadowrootmode="open"> #1
    <link rel="stylesheet" href="product-card.css">

    <div class="row">
      <p class="title"><slot name="title">Untitled</slot></p>
      <p class="price"><slot name="price">$0</slot></p>
    </div>

    <p class="muted"><slot name="sku"></slot></p>
    <p><slot name="actions"></slot></p>
  </template>

  <span slot="title">USB-C Camera</span>
  <span slot="price">$199</span>
  <span slot="sku">SKU: CAM-UC-01</span>
  <button slot="actions" type="button">Add to cart</button>
</product-card>

The product-card markup shows a declarative root containing its stylesheet and named slots, with the content supplied by the host. section 9.6.1

How Vanilla Web covers it

How deep it goes
Explains attachment, open and closed access, rendering placement, styling, declarative creation, and testing; defers detailed slot behavior.
Examples
Contrasts a button styled by page CSS with an isolated component, then shows a declarative product card and slot assertions.
What it assumes
Assumes familiarity with HTML, CSS, JavaScript classes, and DOM operations while defining the shadow boundary and attachment API.
What you can do after
Build it from scratch with `attachShadow()` or declarative HTML, then render into and test the resulting tree.
How it connects
Connects custom elements to light DOM, slots, CSS custom properties, `:host`, `::part`, and DOM events.

Also explained in

Compare how 3 books explain shadow root →

Related concepts

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