Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 39 ยท Templates and Slots

slotchange, assignedNodes and Manual Slot Assignment

React to slotted content with the slotchange event, read slot.assignedNodes and assignedElements, handle late-arriving children, and use manual slot assignment.

Slots decide where children are drawn, but a component often needs to know about its children: count the tabs, build a menu from headings, show an empty state. The platform provides the slotchange event and a handful of methods for that. This lesson also solves the timing problem from earlier lessons: when are the children actually there?

The slotchange event

A slotchange event fires on the <slot> element when the list of nodes assigned to it changes: the first assignment, children added, removed or reassigned (for example by changing a child's slot attribute). It does not fire when a slotted node's content changes (text edits inside a slotted <p>); use a MutationObserver for that.

slotchange bubbles but is not composed, so it never leaves the shadow tree: listen on the slot or on the shadow root.

Counting slotted items

The counter updates itself as items come and go, and it also fires once at startup for the initial children, so you do not need separate initial code. That startup firing is the cure for the "children not parsed yet" problem: do your child-dependent setup in a slotchange handler instead of connectedCallback.

assignedNodes and assignedElements

These slot methods return what is currently assigned:

Call Returns
slot.assignedNodes() Elements and text nodes
slot.assignedElements() Elements only
slot.assignedNodes({ flatten: true }) Also expands nested slots and, when nothing is assigned, the fallback content
slot.assignedElements({ flatten: true }) Same, elements only

flatten: true matters when the slot you are inspecting is itself slotted into another component. Without it, you would see only the inner <slot> element as the assigned node.

Building a menu from children

A very common pattern is reading slotted headings and building navigation or tabs from them.

A tab list built from slotted panels

Because the tab bar is built in slotchange, it works no matter when the panels arrive: parsed HTML, appended later by a script, or generated by a framework. (A full accessible tab component also needs aria-controls, roving tabindex and arrow-key handling; the accessibility chapter returns to this.)

Observing deeper changes

slotchange reports structural changes only. For text changes inside slotted nodes, observe them:

JavaScript
const observer = new MutationObserver(() => this.#refresh());
observer.observe(this, { childList: true, subtree: true, characterData: true });

Connect the observer in connectedCallback and disconnect() it in disconnectedCallback.

Manual slot assignment

By default, assignment is named: the browser matches slot attributes to slot names. attachShadow({ slotAssignment: "manual" }) turns this off and lets your code decide with slot.assign(...nodes). This is useful when the page author should not have to write slot attributes (for example, a virtual list that decides which items to show).

JavaScript
const root = this.attachShadow({ mode: "open", slotAssignment: "manual" });
root.innerHTML = `<slot id="a"></slot><slot id="b"></slot>`;
const [first, ...rest] = this.children;
root.getElementById("a").assign(first);
root.getElementById("b").assign(...rest);

Rules: only direct children of the host can be assigned, each at most to one slot, and assign replaces the slot's earlier manual assignment. Manual assignment works in current browsers, but is much rarer than named slots, so you can skip it until you need it. (This snippet is read-only here.)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. On which element does the slotchange event fire?

    slotchange is dispatched at the slot whose assigned nodes changed.
  2. 2. Which situation does NOT trigger slotchange?

    slotchange reports changes to the list of assigned nodes, not changes inside them. Use a MutationObserver for those.
  3. 3. What does assignedNodes({ flatten: true }) add?

    Flattening resolves slots that are themselves slotted and reports fallback content when the slot is empty.
  4. 4. Why build child-dependent UI in slotchange instead of connectedCallback?

    slotchange runs whenever assignment changes, including the initial one, so it covers late-arriving children.

Summary

  • slotchange fires on a <slot> when its assigned nodes change, including the first assignment.
  • assignedNodes, assignedElements and flatten: true read the current assignment.
  • Build child-dependent UI (counts, tabs, menus) in a slotchange handler.
  • Use a MutationObserver for text or attribute changes inside slotted content.
  • slotAssignment: "manual" with slot.assign() gives code full control of assignment.

Next: Styling Slotted Content