Slots and light-DOM composition
Quick answer
A slot in a shadow tree provides a placement point for light-DOM children of its host. It lets users supply content without rebuilding component internals.
How it works
Use <slot name="label">fallback</slot> and a direct light-DOM child with slot="label". A default slot receives slottable children without a nonempty slot name. Slot fallback is shown when nothing is assigned. assignedNodes() returns assigned nodes (including text); assignedElements() returns assigned element nodes. The flatten option includes flattened nested slot assignments or fallback as defined by the DOM algorithms. slotchange signals changes in assigned nodes, not arbitrary edits inside an assigned descendant. Keep native labels and meaningful text when composing.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const host=document.createElement("div");const root=host.attachShadow({mode:"open"});root.innerHTML="<slot name=label>fallback</slot>";const span=document.createElement("span");span.slot="label";span.textContent="title";host.append(span);document.body.append(host);const slot=root.querySelector("slot");console.log(slot.assignedElements()[0].textContent,span.parentNode===host);})()Expected console output:
title trueThe node is assigned without moving its light-DOM parent.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const host=document.createElement("div");const root=host.attachShadow({mode:"open"});root.innerHTML="<slot>fallback</slot>";host.append(document.createTextNode("hello"));document.body.append(host);const slot=root.querySelector("slot");console.log(slot.assignedNodes()[0].nodeType,slot.assignedElements().length);})()Expected console output:
3 0A text node is assigned, but it is not an element.
Common mistakes
Do not expect assigned light-DOM nodes to move into the shadow root. Do not rely on slotchange to track every text mutation.
Practice
Build a named and default slot, append a named span, then verify its parent and assigned slot.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Slots and light-DOM composition — Knowledge Check
Apply the concept and reason about local example output.
What does a default slot receive?
Which method includes assigned text nodes?
Does changing text inside an already assigned element necessarily fire slotchange?
What is the expected console output of Slots and light-DOM composition example 1?
(async () => {const host=document.createElement("div");const root=host.attachShadow({mode:"open"});root.innerHTML="<slot name=label>fallback</slot>";const span=document.createElement("span");span.slot="label";span.textContent="title";host.append(span);document.body.append(host);const slot=root.querySelector("slot");console.log(slot.assignedElements()[0].textContent,span.parentNode===host);})()What is the expected console output of Slots and light-DOM composition example 2?
(async () => {const host=document.createElement("div");const root=host.attachShadow({mode:"open"});root.innerHTML="<slot>fallback</slot>";host.append(document.createTextNode("hello"));document.body.append(host);const slot=root.querySelector("slot");console.log(slot.assignedNodes()[0].nodeType,slot.assignedElements().length);})()