Shadow DOM refers to the ability of the browser to include a subtree of DOM elements into the rendering of a document, but not into the main document DOM tree.
Document
<html>
<head>
<body>
<title>
Text: "My title"
<div>
<p>
Text: "My content"
<custom-tag>
<button>
Text: "Click me"
class CustomTag extends HTMLElement {
constructor () {
super();
// This enables the ShadowDOM ability for a custom element
const shadowRoot = this.attachShadow({mode: 'open'});
// This adds HTML code to the element
shadowRoot.innerHTML = `<button>Click me, baby!</button>`;
}
}
// The customElements API is used
/* more info here:
https://developers.google.com/web/fundamentals/web-components/customelements
*/
customElements.define('custom-tag', CustomTag);<!DOCTYPE html>
<html>
<head>...</head>
<body>
<custom-tag></custom-tag>
</body>
</html>class CustomTag extends HTMLElement {
constructor () {
super();
const shadowRoot = this.attachShadow({mode: 'open'});
shadowRoot.innerHTML = `<button>Click me, baby!</button>`;
}
// This method is called when the element is inserted into the DOM
connectedCallback () {
// this.shadowRoot is the root of the DOM subtree
this.shadowRoot.querySelector('button').addEventListener('click', () => {
alert('Hello, world!');
});
}
}
customElements.define('custom-tag', CustomTag);<body>
<!-- This won't work, Shadow DOM encapsulates the CSS for components -->
<!-- The parent document can't edit the components' styling -->
<style>
button {
background: blue;
color: white;
padding: 10px;
border-radius: 5px;
border: none;
font-size: 14px;
}
</style>
<custom-tag></custom-tag>
</body>class CustomTag extends HTMLElement {
constructor () {
super();
const shadowRoot = this.attachShadow({mode: 'open'});
// We'll add some CSS by hand
shadowRoot.innerHTML = `
<style>
button {
background: blue;
color: white;
padding: 10px;
border-radius: 5px;
border: none;
font-size: 14px;
}
</style>
<button>Click me, baby!</button>`;
}
connectedCallback () {
this.shadowRoot.querySelector('button').addEventListener('click', () => {
alert('Hello, world!');
});
}
}
customElements.define('custom-tag', CustomTag);