Shadow DOM
Shadow DOM serves for encapsulation. It allows a component to have its very own “shadow” DOM tree, that can’t be accidentally accessed from the main document, may have local style rules, and more.
Built-in shadow DOM
Did you ever think how complex browser controls are created and styled? Such as :
The browser uses DOM/CSS internally to draw them. That DOM structure is normally hidden from us, but we can see it in developer tools. E.g. in Chrome, we need to enable in Dev Tools “Show user agent shadow DOM” option. Then looks like this: What you see under #shadow-root is called “shadow DOM”. We can’t get built-in shadow DOM elements by regular JavaScript calls or selectors. These are not regular children, but a powerful encapsulation technique. In the example above, we can see a useful attribute pseudo. It’s non-standard, exists for historical reasons. We can use it style subelements with CSS, like this: Once again, pseudo is a non-standard attribute. Chronologically, browsers first started to experiment with internal DOM structures to implement controls, and then, after time, shadow DOM was standardized to allow us, developers, to do the similar thing. Further on, we’ll use the modern shadow DOM standard, covered by DOM spec and other related specifications.
Shadow tree
A DOM element can have two types of DOM subtrees:
1. Light tree – a regular DOM subtree, made of HTML children. All subtrees that we’ve seen in previous chapters were “light”.
2. Shadow tree – a hidden DOM subtree, not reflected in HTML, hidden from prying eyes.
If an element has both, then the browser renders only the shadow tree. But we can setup a kind of composition between shadow and light trees as well. We’ll see the details later in the chapter info:slots-composition.
Shadow tree can be used in Custom Elements to hide component internals and apply component-local styles.
For example, this , can’t host shadow tree.
The mode option sets the encapsulation level. It must have any of two values:
- “open” – the shadow root is available as elem.shadowRoot.
Any code is able to access the shadow tree of elem.
- “closed” – elem.shadowRoot is always null.
We can only access the shadow DOM by the reference returned by attachShadow (and probably hidden inside a class). Browser-native shadow trees, such as , are closed. There’s no way to access them.
The shadow root, returned by attachShadow, is like an element: we can use innerHTML or DOM methods, such as append, to populate it.
The element with a shadow root is called a “shadow tree host”, and is available as the shadow root host property:
Encapsulation
Shadow DOM is strongly delimited from the main document: 1. Shadow DOM elements are not visible to querySelector from the light DOM. In particular, Shadow DOM elements may have ids that conflict with those in the light DOM. They must be unique only within the shadow tree. 2. Shadow DOM has own stylesheets. Style rules from the outer DOM don’t get applied. For example: 1. The style from the document does not affect the shadow tree. 2. …But the style from the inside works. 3. To get elements in shadow tree, we must query from inside the tree.
References
Compatibility: https://caniuse.com/#feat=shadowdomv1
Shadow DOM is mentioned in many other specifications, e.g. DOM Parsing specifies that shadow root has innerHTML.
Summary
Shadow DOM is a way to create a component-local DOM.
- shadowRoot = elem.attachShadow({mode: open|closed}) – creates shadow DOM for elem. If mode=“open”, then it’s accessible as elem.shadowRoot property.
- We can populate shadowRoot using innerHTML or other DOM methods. Shadow DOM elements:
- Have their own ids space,
- Invisible to JavaScript selectors from the main document, such as querySelector,
- Use styles only from the shadow tree, not from the main document. Shadow DOM, if exists, is rendered by the browser instead of so-called “light DOM” (regular children). In the chapter info:slots-composition we’ll see how to compose them.
<style>
/* make the slider track red */
input::-webkit-slider-runnable-track {
background: red;
}
</style>
<input type="range">
Follow the lesson from Microsoft Web-Dev-For-Beginners course