Lesson 5 of 9 · 23m
Templates and Slots: Composition Without a Framework
Inert template markup, efficient cloning, named slot assignment, fallback content, and the slotchange timing details that matter.
Templates and slots are the platform's composition primitives. They look small next to JSX until the same component must be consumed by server-rendered pages and two frameworks. Then the absence of a runtime abstraction becomes the advantage.
<template> is inert markup
<template id="user-card-template">
<article class="card">
<img part="avatar" alt="">
<slot name="name">Unknown user</slot>
<slot></slot>
</article>
</template>Template content does not render, fetch images, run scripts, or appear in document queries. Clone it when needed:
const template = document.getElementById("user-card-template");
this.shadowRoot.append(template.content.cloneNode(true));Cloning keeps static structure separate from dynamic values. In self-contained components, define the template once per class rather than depending on a template element in the outer document.
Slots are named outlets
<user-card>
<strong slot="name">Ada Lovelace</strong>
<p>First programmer</p>
</user-card>The <strong> is assigned to the name slot; the paragraph goes to the default slot. Neither node moves into the shadow tree — projection is visual composition, not DOM reparenting. Consumers therefore keep control of their own content and event listeners.
Fallback content is the empty state
<slot name="empty"><p>No results yet.</p></slot>Fallback renders only when nothing is assigned. It is excellent for empty states and documentation-like defaults, but it is not conditional rendering. Whitespace text nodes can count as assignment, so use assignedElements() when your logic cares about actual elements.
React to assignment with slotchange
connectedCallback() {
const slot = this.shadowRoot.querySelector("slot[name='name']");
slot.addEventListener("slotchange", () => {
this.toggleAttribute("has-name", slot.assignedElements().length > 0);
});
}slotchange reports assignment changes, not every mutation deep inside assigned nodes. If a consumer edits text within its own slotted subtree, normal rendering already handles it; only add logic when presence or absence changes semantics.
Composition rules people trip over
- A node can be assigned to at most one slot in one shadow tree.
- Nested slots use flattened assignment; they do not magically forward a grandparent's API.
display: contentsis often needed on wrappers so layout applies cleanly to slotted children.- Observe light DOM with
MutationObserveronly when whole assigned nodes are added or removed frequently.
Field notes
- Prefer slots for anything users recognize as content; HTML is already the composition language.
- Do not encode behavioral data in child markup when a typed property would be clearer.
- Renaming a slot silently drops consumer content and is more breaking than most visual changes.
- Use
assignedNodes({ flatten: true })when working through nested slot composition.
Was this lesson helpful?