Sub topic 3
Named slots
This chapter assumes you can build a basic Lightning web component (HTML template, JavaScript class, CSS file), use a child component in a parent template, and mark public properties with @api. You should also know the default slot from the previous chapter, which collects whatever the parent places inside the child's tags.
A default slot is enough when a component has one place for outside content. Many components have several: a card with a header, a body and a footer, or a page layout with a sidebar and a main area. Named slots solve this. The child declares several labeled openings, and the parent routes each piece of markup to the one it wants.
What a named slot is
A named slot is a <slot> element with a name attribute. The parent targets it by putting a slot attribute with the same value on an element.
- In the child,
<slot name="header"></slot>declares an opening calledheader. - In the parent,
<span slot="header">Order summary</span>sends that span into the opening.
The two attributes have different jobs. name is on the child's <slot> element and declares the opening. slot is on the parent's element and picks the opening. The values must match exactly, including case. By convention, slot names are lowercase and short.
A complete example
Here is the card component from this tutorial. It has two named slots and one default slot.
<!-- card.html -->
<template>
<article class="card">
<header>
<slot name="header">Untitled</slot>
</header>
<div class="body">
<slot></slot>
</div>
<footer>
<slot name="footer"></slot>
</footer>
</article>
</template>The header slot contains the text "Untitled". That is fallback content, which appears only when the parent supplies nothing for the slot. A later chapter covers it in detail.
The parent fills the slots like this:
<!-- parent.html -->
<template>
<c-card>
<span slot="header">Order summary</span>
<p>Two items in your cart.</p>
<lightning-button slot="footer" label="Checkout"></lightning-button>
</c-card>
</template>The browser output is structurally equivalent to this:
<article class="card">
<header><span>Order summary</span></header>
<div class="body"><p>Two items in your cart.</p></div>
<footer><lightning-button label="Checkout"></lightning-button></footer>
</article>The <p> has no slot attribute, so it goes to the unnamed slot. The <span> and the <lightning-button> carry slot values, so they go to header and footer.
The routing rules
The browser assigns each child of the component tag to a slot. These rules cover most cases.
- An element with a
slotattribute goes to the slot whosenamehas the same value. - An element with no
slotattribute goes to the default (unnamed) slot. - Text written directly inside the component tag, with no wrapping element, also goes to the default slot. A text node cannot carry a
slotattribute, so it can never reach a named slot. Wrap the text in an element, such as a<span>, to route it. - If the
slotvalue matches no slot in the child, the element is not rendered. No error appears. - If content would go to the default slot but the child has no default slot, that content is not rendered either.
Rules 4 and 5 are the most common source of "my content vanished" bugs. The next table shows what happens for several parent snippets against the card above.
| Parent markup | Where it lands |
|---|---|
| `<span slot="header">Title</span>` | `header` slot |
| `<p>Body text</p>` | Default slot |
| `Plain text` (no element) | Default slot |
| `<span slot="Header">Title</span>` | Nowhere. Case differs, so no slot matches. |
| `<span slot="subtitle">Sub</span>` | Nowhere. The card has no `subtitle` slot. |
Only direct children count
The slot attribute works only on elements that are direct children of the component tag. This markup does not do what it appears to do:
<template>
<c-card>
<div>
<span slot="header">Order summary</span>
</div>
</c-card>
</template>The <div> is the direct child, and it has no slot attribute. It goes to the default slot with the span inside it. The span's slot="header" is ignored because its parent is the <div>, not the card. Move the attribute to the <div> if you want the whole div in the header.
Several elements in one slot
A slot can receive any number of elements. Give each element the same slot value:
<!-- parent.html -->
<template>
<c-card>
<span slot="header">Order summary</span>
<p>Two items in your cart.</p>
<lightning-button slot="footer" label="Cancel"></lightning-button>
<lightning-button slot="footer" variant="brand" label="Checkout"></lightning-button>
</c-card>
</template>Both buttons appear inside the card's <footer>. They keep the order in which they were written in the parent. The order in which attributes or slots appear has no effect on this.
The order of elements in the parent's markup does not have to follow the child's layout. In the next example, the footer content is written before the header content, and the card still renders header first, body, then footer, because the child's template decides the layout.
<template>
<c-card>
<lightning-button slot="footer" label="Checkout"></lightning-button>
<p>Two items in your cart.</p>
<span slot="header">Order summary</span>
</c-card>
</template>This is the main benefit of named slots. The parent supplies content in any order, and the child controls where each piece appears.
If you want to give several elements a single wrapper, you can group them yourself:
<template>
<c-card>
<div slot="footer" class="actions">
<lightning-button label="Cancel"></lightning-button>
<lightning-button variant="brand" label="Checkout"></lightning-button>
</div>
</c-card>
</template>Use separate elements with the same slot value when the child may want to treat each one separately, for example by styling or counting them. Use a wrapper when the parent wants to control the layout of the group.
Rules for slot names in the child
Keep these constraints in mind when you write the child's template:
- Write the
nameas a plain string literal, such asname="footer". A slot name is not a place for a template expression likename={slotName}. - Use one slot per name. If two slots shared a name, it would be unclear where the parent's content should go.
- Use at most one unnamed slot, which is the default slot.
- Pick names that describe the role of the content (
header,footer,actions,sidebar), not its look (left,blue). A role name stays valid if the layout changes.
You can place a <slot> anywhere in the template, including inside conditional blocks, as long as the name stays a literal. A slot inside a conditional block exists only while the block is rendered. Content assigned to it is not displayed when the block is hidden.
<!-- panel.html -->
<template>
<section class="panel">
<template lwc:if={showHeader}>
<header>
<slot name="header"></slot>
</header>
</template>
<slot></slot>
</section>
</template>// panel.js
import { LightningElement, api } from 'lwc';
export default class Panel extends LightningElement {
@api showHeader = false;
}When showHeader is false, anything the parent marks with slot="header" is not shown. Make sure the user can tell why. A silent no-op can look like a bug.
Reacting to what the parent supplied
A child often needs to know whether a named slot received anything. For example, it may want to hide an empty footer container so it does not leave padding behind. Add an onslotchange handler to the slot and read the slot's assigned nodes:
<!-- card.html (footer section) -->
<footer class={footerClass}>
<slot name="footer" onslotchange={handleFooterChange}></slot>
</footer>// card.js
import { LightningElement } from 'lwc';
export default class Card extends LightningElement {
hasFooter = false;
handleFooterChange(event) {
const slot = event.target;
this.hasFooter = slot.assignedNodes().length > 0;
}
get footerClass() {
return this.hasFooter ? 'footer' : 'footer slds-hide';
}
}The slotchange event fires when the nodes assigned to a slot change. assignedNodes() returns the nodes currently assigned to it. The slds-hide class comes from the Salesforce Lightning Design System and hides the element. Later chapters cover slotchange and assignedNodes() in more depth, including what happens with fallback content.
Notice what makes this possible. Each named slot reports its own assignments, so the child can track the header, body and footer independently. With one default slot, the child could only ask a single question about all the content.
Common mistakes with named slots
| Symptom | Likely cause | Fix |
|---|---|---|
| Header content does not appear | Slot name misspelled, or different case between `slot` and `name` | Make the two values identical |
| Content appears in the body instead of the footer | `slot` attribute is on a nested element, not a direct child | Move the attribute to the direct child |
| Content appears nowhere | The child has no matching slot, or no default slot | Add the slot to the child, or fix the name |
| Text lands in the body | Bare text cannot take a `slot` attribute | Wrap the text in an element and put `slot` on it |
| Footer empty but still takes space | The container is always rendered | Handle `slotchange` and hide the container |
A word on ownership
Slotted elements are written in the parent's template, so the parent owns them. Their data bindings and event handlers refer to the parent's JavaScript class. In the examples above, label="Checkout" and any onclick handler on the button belong to the parent. The card only decides where the button appears. The ownership chapter covers the consequences for styling and event handling.
When named slots are the right tool
Use named slots when the parent should supply markup, such as a button, an icon with a label, or a formatted block. Use a public property when the parent only needs to supply a plain value, such as a title string. A later chapter compares the two approaches in detail. For now, a quick check is whether the content could be anything other than text. If it could, a slot gives the parent the freedom it needs.
Summary
- A named slot is a
<slot name="x">in the child. The parent targets it withslot="x"on a direct child element. - Elements without a
slotattribute, and bare text, go to the default slot. - Names must match exactly. A mismatch fails silently and the content is not rendered.
- Many elements can share one slot name. They appear in the order they were written in the parent.
- The child decides the layout, so the order of the parent's markup does not matter.
- Handle
slotchangeon a named slot when the child needs to react to whether content was supplied.
Exercise
A parent writes the markup below inside <c-card>. The card has a default slot and named slots header and footer. For each of the three elements, say which slot it lands in, and explain why the heading text does not appear in the card's header.
<div class="intro"><h2 slot="header">Welcome</h2></div>
<p slot="sidebar">Tips</p>
Thanks for visiting.
Show answer
The <div class="intro"> has no slot attribute, so it goes to the default slot, and the <h2> goes with it as a child of the div. The slot="header" on the <h2> is ignored because the <h2> is not a direct child of <c-card>, so "Welcome" renders in the body, not the header. The fix is to move slot="header" onto the <div>, or to place the <h2> directly inside <c-card>. The <p slot="sidebar"> matches no slot in the card, because the card has no sidebar slot, so it is not rendered at all. The bare text "Thanks for visiting." has no element to carry a slot attribute, so it goes to the default slot.
Exercise
Write the child template and the parent template for a component c-alert. The child has a named slot icon, a default slot for the message, and a named slot actions. The parent should put two buttons, "Dismiss" and "Details", in the actions slot, in that order. Then add a handler to the child that sets a hasActions field to true only when the actions slot has content.
Show answer
Child template:
<template>
<div class="alert">
<span class="icon"><slot name="icon"></slot></span>
<div class="message"><slot></slot></div>
<div class={actionsClass}>
<slot name="actions" onslotchange={handleActionsChange}></slot>
</div>
</div>
</template>
Parent template:
<template>
<c-alert>
<lightning-icon slot="icon" icon-name="utility:warning" size="x-small"></lightning-icon>
<span>Your session is about to expire.</span>
<lightning-button slot="actions" label="Dismiss"></lightning-button>
<lightning-button slot="actions" label="Details"></lightning-button>
</c-alert>
</template>
Child JavaScript:
import { LightningElement } from 'lwc';
export default class Alert extends LightningElement {
hasActions = false;
handleActionsChange(event) {
this.hasActions = event.target.assignedNodes().length > 0;
}
get actionsClass() {
return this.hasActions ? 'actions' : 'actions slds-hide';
}
}
Both buttons use slot="actions" and appear in the order written in the parent. The message span has no slot attribute, so it goes to the default slot.