Element-defined Content
// ⭐️ content of <template> is only parsed once❗️
let tmpl = document.createElement('template');
tmpl.innerHTML = `
<style>:host { ... }</style>
<b>I'm in shadow dom!</b>
<slot></slot>
`;
customElements.define('custom-element', class extends HTMLElement {
constructor() {
// always call super() first in the constructor.
super();
// Attach a shadow root to the element.
let root = this.attachShadow({mode: 'open'});
// ⭐️ clone <template> content
root.appendChild(tmpl.content.cloneNode(true));
}
});import <template>
Last updated