Skip to content

Instantly share code, notes, and snippets.

@hovissimo
Last active September 11, 2022 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hovissimo/6b9cc8245a24433b5fb962bdba16d59e to your computer and use it in GitHub Desktop.
Save hovissimo/6b9cc8245a24433b5fb962bdba16d59e to your computer and use it in GitHub Desktop.
Web component default slot content
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