Sub topic 3
Shadow DOM: encapsulating markup and styles
This chapter assumes you know basic HTML and CSS, the browser DOM, and how to define a custom element with a JavaScript class. It covers how shadow DOM isolates a component's internals, and what that isolation changes in practice.
What shadow DOM is
Shadow DOM is a browser standard that lets an element own a separate, hidden DOM tree. The element that owns the tree is the host. The hidden tree hangs off a shadow root. Markup and styles inside the shadow root are kept apart from the rest of the page.
You create a shadow root by calling attachShadow on an element. The first example below uses the plain web component from the topic plan.
class HelloGreeting extends HTMLElement {
connectedCallback() {
const root = this.attachShadow({ mode: 'open' });
const name = this.getAttribute('name') ?? 'World';
root.innerHTML = `<p>Hello, ${name}!</p>`;
}
}
customElements.define('hello-greeting', HelloGreeting);With mode: 'open', outside code can reach the shadow root through element.shadowRoot. With mode: 'closed', shadowRoot returns null. Closed mode discourages access but does not make the component secure.
What gets encapsulated
Two things are isolated.
- Markup.
document.querySelector('p')does not find the<p>inside the shadow root. You must query the host'sshadowRootinstead. - Styles. A selector in the page's stylesheet does not match elements inside the shadow tree. A style written inside the shadow tree does not leak out.
Some things cross the boundary on purpose. Inherited CSS properties such as color and font-family flow from the host into the shadow tree. CSS custom properties (for example --brand-color) also pass through, which makes them the standard way to let a page theme a component.
Events and the boundary
Events fired inside a shadow tree bubble up to the host, but the browser retargets them. A listener on the page sees the host as event.target, not the inner element. This hides the component's internal structure. An event only escapes the shadow tree if it is composed. Most native UI events, such as click, are composed. A CustomEvent is not composed unless you set composed: true.
How this relates to LWC
An LWC component is a custom element with a shadow root. The framework attaches the shadow root for you, so you never call attachShadow yourself. Your template renders into it, and the CSS file in the bundle applies only inside it.
Be aware of two common errors:
- Querying inner elements from the page or from a parent with
document.querySelector. It returnsnullbecause of the boundary. - Expecting a parent's stylesheet to restyle a child component. It cannot reach inside. Expose a CSS custom property instead.
Exercise
A page stylesheet contains p { color: red; }. A hello-greeting element on that page has an open shadow root containing a <p>. Will the paragraph inside the shadow root turn red? What about color: red set on the hello-greeting host itself?
Show answer
No for the first rule, because page selectors do not match elements inside a shadow tree. Yes for the second, because color is an inherited property, so the value set on the host flows into the shadow tree, unless a rule inside the shadow tree sets its own color.