Sub topic 3
Named slots
This chapter assumes you can build a basic Lightning web component, use a child component in a parent template, and you know how the default slot works. You will learn how to give a child component several insertion points and how a parent targets each one.
Why a default slot is not enough
A default slot accepts everything the parent puts between the child's tags. That works for a single region of content. Many layouts have several regions, such as a card with a header, a body, and a footer. If every piece of content landed in one slot, the child could not place each piece in the right spot.
A named slot is a <slot> element with a name attribute. The parent sends content to it by adding a matching slot attribute to an element.
Defining named slots in the child
The card component below has two named slots (header and footer) and one unnamed 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 child controls the structure: where each slot sits, which wrapper elements surround it, and which CSS classes those wrappers carry. The text "Untitled" inside the header slot is fallback content. The next chapter covers it. For now, note that it shows only when the parent supplies nothing for that slot.
Filling the slots from the parent
The parent adds slot="name" to each element that should go into a named slot. Any element without a slot attribute goes to the default slot.
<!-- 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 rendered result follows the child's layout, not the order in the parent:
- The
spanappears inside<header>. - The
pappears inside thedivwith classbody, because it has noslotattribute. - The
lightning-buttonappears inside<footer>.
Matching rules
The match between the slot attribute and the name attribute follows a few rules.
- The value is a plain string and must match the child's
nameexactly, including case.slot="Header"does not matchname="header". - The
slotattribute must be on a direct child of the child component's tag. If you wrap the element in adiv, thedivgoes to the default slot and theslotattribute on the inner element has no effect. - Several elements can use the same slot name. They are all placed in that slot, in the order they appear in the parent.
- If the parent uses a slot name the child does not define, that content is not rendered.
- Elements with no
slotattribute all go to the default slot, in parent order.
Here is a parent that sends two elements to one slot.
<!-- parent.html -->
<template>
<c-card>
<span slot="header">Order summary</span>
<lightning-badge slot="header" label="New"></lightning-badge>
<p>Two items in your cart.</p>
</c-card>
</template>Both the span and the badge appear inside the header, and the footer slot stays empty.
<!-- parent.html: this does not work as intended -->
<template>
<c-card>
<div>
<span slot="header">Order summary</span>
</div>
</c-card>
</template>In this second parent, the span is not a direct child of c-card, so it does not reach the header slot. The div goes into the default slot.
Design tips for named slots
Name slots after their role in the layout (header, footer, actions), not after what a particular parent puts in them. A parent could place a button, a link, or a menu in actions. Naming the slot checkout-button would make the card awkward to reuse.
Keep the number of slots small. Each one is part of the component's public contract, much like an @api property. Removing or renaming a slot later breaks every parent that uses it.
Leave the default slot for the main content. Parents then do not need an attribute for the most common case.
Slots in a table
| Parent element | Child slot | Result |
|---|---|---|
| `<span slot="header">` | `<slot name="header">` | Rendered in the header |
| `<p>` with no attribute | `<slot>` (default) | Rendered in the body |
| `<span slot="sidebar">` | none defined | Not rendered |
| `<div><span slot="footer"></span></div>` | `<slot name="footer">` | `div` goes to the default slot, `span` is not placed in the footer |
Exercise
A parent template contains <c-card><div><lightning-button slot="footer" label="Save"></lightning-button></div></c-card>. The card defines a footer slot named footer. Why does the button not appear in the footer, and how do you fix it?
Show answer
The slot attribute only works on a direct child of the c-card tag. Here the button is nested inside a div, so the div goes into the default slot and the slot attribute has no effect. Remove the wrapper div, or move slot="footer" onto the div itself so that the div is the direct child.
Exercise
A card has slots named header and footer. A parent adds two <p slot="header"> elements and one <p> with no attribute. Where does each paragraph go, and in what order?
Show answer
Both <p slot="header"> elements go into the header slot, in the order they appear in the parent. The paragraph without a slot attribute goes into the default slot, which the child places in the body.