Last active
September 11, 2022 15:30
-
-
Save hovissimo/6b9cc8245a24433b5fb962bdba16d59e to your computer and use it in GitHub Desktop.
Web component default slot content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
customElements.define('my-span', | |
class extends HTMLElement { | |
constructor() { | |
super(); | |
// '<span><slot>default text</slot></span>' | |
const span = document.createElement('span') | |
const slot = document.createElement('slot') | |
slot.appendChild(document.createTextNode('default text')) | |
span.appendChild(slot) | |
const shadowRoot = this.attachShadow({mode: 'open'}) | |
shadowRoot.appendChild(span) | |
} | |
}, | |
) | |
const filledSpan = document.createElement('my-span') | |
filledSpan.appendChild(document.createTextNode('other text')) | |
const emptySpan = document.createElement('my-span') | |
document.body.appendChild(emptySpan) | |
document.body.appendChild(filledSpan) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment