Example 1 — Default Slot Assignment
A direct text child of a host fills the unnamed <slot>.
class CtfTextSlotHost extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" }).innerHTML = "<slot></slot>";
}
}
customElements.define("ctf-text-slot-host", CtfTextSlotHost);
const host = document.createElement("ctf-text-slot-host");
const text = new Text("Slotted text");
host.appendChild(text);
document.body.appendChild(host);
const slot = text.assignedSlot;
console.log(slot !== null); // true
console.log(slot.localName); // "slot"
console.log(slot === host.shadowRoot.querySelector("slot")); // true How It Works
- Build a host. The custom element attaches an open shadow root with a default
<slot>. - Append text.
new Text("Slotted text")becomes a light-DOM child of the host. - Browser assigns. Unnamed light-DOM children go to the unnamed slot.
- Read the property.
text.assignedSlotis that same<slot>node.

