Skip to content

Instantly share code, notes, and snippets.

@kristoferjoseph
Created August 23, 2022 20:47
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 kristoferjoseph/004c850b3778a5571596d053724bcd74 to your computer and use it in GitHub Desktop.
Save kristoferjoseph/004c850b3778a5571596d053724bcd74 to your computer and use it in GitHub Desktop.
Nested Custom Element Slots
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<template id="my-p-template">
<p>
<slot></slot>
</p>
</template>
<template id="my-rad-p-template">
<slot name="emoji">🙌</slot>
<my-p>
<slot></slot>
</my-p>
</template>
<my-p>
My P
</my-p>
<my-rad-p>
<span slot="emoji">✨</span>
My Rad P
</my-rad-p>
<my-rad-p>
<my-rad-p>
<span slot="emoji">1️⃣</span>
Second
<my-rad-p>
<span slot="emoji">2️⃣</span>
Third
</my-rad-p>
</my-rad-p>
</my-rad-p>
<script type="module">
class MyP extends HTMLElement {
constructor() {
super()
const template = document.getElementById('my-p-template')
this.attachShadow({ mode: 'open' })
.appendChild(template.content.cloneNode(true))
}
}
customElements.define('my-p', MyP)
class MyRadP extends HTMLElement {
constructor() {
super()
const template = document.getElementById('my-rad-p-template')
this.attachShadow({ mode: 'open' })
.appendChild(template.content.cloneNode(true))
}
}
customElements.define('my-rad-p', MyRadP)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment