Sub topic 9
Passing slots through nested components
This chapter assumes you can write a component with default and named slots, fill them from a parent template with the slot attribute, and handle slotchange. Those topics are covered in earlier chapters. Here you will see how to pass slots through a wrapper component to a component nested inside it.
The problem
Suppose you build c-card with header, default, and footer slots. Later you build c-order-panel, a wrapper that uses c-card internally and adds its own behavior, such as a standard border or a loading state. Users of c-order-panel still want to supply a header, body, and footer.
A parent can only fill slots of the components it writes directly in its own template. The user of c-order-panel cannot reach the slots of the c-card inside it. The wrapper has to pass its slots through.
Forwarding with a slot inside a slot
The wrapper declares its own slots. It places each one inside the <c-card> tag and gives it a slot attribute that names the c-card slot it should fill.
<!-- orderPanel.html -->
<template>
<c-card>
<slot name="title" slot="header"></slot>
<slot></slot>
<slot name="actions" slot="footer"></slot>
</c-card>
</template>Each <slot> element here has two roles:
namedefines a slot onc-order-panel, the one its own users fill.slotassigns that<slot>element to a slot onc-card.
The unnamed <slot></slot> has no slot attribute, so it goes into the default slot of c-card. Its own name is empty, so it is the default slot of c-order-panel.
The final user writes this:
<!-- checkout.html -->
<template>
<c-order-panel>
<span slot="title">Order summary</span>
<p>Two items in your cart.</p>
<lightning-button slot="actions" label="Checkout"></lightning-button>
</c-order-panel>
</template>Nobody in this chain needs to know the full path. checkout knows only the slot names of c-order-panel. c-order-panel knows only the slot names of c-card. You can rename a slot in the wrapper, for example actions instead of footer, without touching c-card.
Who owns the content
Forwarding does not change ownership. The <span>, <p>, and <lightning-button> still belong to checkout. Three things follow:
- Data bindings and event handlers on that content run in
checkout's context, not inc-order-panelorc-card. - CSS from
checkoutstyles it. Styles fromc-order-panelandc-carddo not reach it. c-order-panelcan add its own elements next to the forwarded slots. Those elements are owned byc-order-panel.
Fallback content and forwarding
Forwarding has one surprising effect on fallback content. Take the header slot of c-card from the earlier chapter, which shows "Untitled" when empty. The <slot name="title" slot="header"> element in c-order-panel is itself a node assigned to that slot. So the slot is never empty, even when the user of c-order-panel supplies no title. The "Untitled" fallback never appears.
To keep a fallback, put it in the forwarding slot:
<!-- orderPanel.html (title only) -->
<template>
<c-card>
<slot name="title" slot="header">Untitled order</slot>
<slot></slot>
<slot name="actions" slot="footer"></slot>
</c-card>
</template>The text inside the forwarding <slot> shows only when the user of c-order-panel leaves title empty.
slotchange and assignedNodes with forwarding
The same effect changes how c-card detects whether a footer exists. In the earlier example, handleFooterChange calls slot.assignedNodes(). With forwarding, the only assigned node is the <slot> element from c-order-panel, so the length is always 1 and c-card always thinks it has a footer.
Pass flatten: true to resolve forwarding slots down to the real content:
// card.js
import { LightningElement } from 'lwc';
export default class Card extends LightningElement {
hasFooter = false;
handleFooterChange(event) {
const slot = event.target;
this.hasFooter = slot.assignedNodes({ flatten: true }).length > 0;
}
get footerClass() {
return this.hasFooter ? 'footer' : 'footer slds-hide';
}
}With flatten: true, a forwarding slot is replaced by the nodes assigned to it. If the forwarding slot has no assigned nodes, its fallback content is returned instead. If you give the forwarding slot fallback content, a footer would then count as present. Keep forwarding slots for footers empty when c-card uses this check.
When to forward
Forward slots when the wrapper adds real value, such as shared behavior, a fixed layout, or a simpler API. Avoid it when the wrapper only renames slots. Each layer adds one more place where a slot name can be misspelled, and a mismatch fails silently: content with an unknown slot name is simply not rendered.
Exercise
A c-card has a header slot with the fallback "Untitled". A wrapper uses <c-card><slot name="title" slot="header"></slot></c-card>. A user of the wrapper provides no title. What does the card show in its header, and what change makes "Untitled order" appear?
Show answer
The header is empty. The forwarding <slot> element is assigned to the card's header slot, so that slot is not empty and the card's "Untitled" fallback is never used. Put the fallback inside the forwarding slot, as in <slot name="title" slot="header">Untitled order</slot>. It then shows when the wrapper's user supplies no title.
Exercise
In c-card, slot.assignedNodes().length > 0 is always true when the footer slot is filled by a forwarding slot. How do you fix the check?
Show answer
Call slot.assignedNodes({ flatten: true }). This replaces the forwarding <slot> element with the nodes actually assigned to it, so the length is 0 when the wrapper's user supplies no footer content.