Sub topic 4
Fallback content for empty slots
This chapter assumes you can build a basic Lightning web component, use a child component in a parent template, and write a template with a default slot and named slots. If those are new, read the chapters on the default slot and named slots first.
What fallback content is
A slot is a placeholder in the child's template. If the parent supplies nothing for it, the slot renders empty. Fallback content is whatever you write between the opening and closing <slot> tags. The browser shows it only when the parent assigns no content to that slot.
Here is the card component from the earlier chapters.
<!-- 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. The body and footer slots have no fallback, so they render nothing when empty.
Seeing it work
The first parent fills the header. The second parent leaves it out.
<!-- withHeader.html -->
<template>
<c-card>
<span slot="header">Order summary</span>
<p>Two items in your cart.</p>
</c-card>
</template><!-- withoutHeader.html -->
<template>
<c-card>
<p>Two items in your cart.</p>
</c-card>
</template>In the first case the header shows "Order summary". In the second it shows "Untitled". The parent does not need to know the fallback exists. The child chooses a sensible default, and the parent overrides it only when it has something better.
The diagram below shows the decision the browser makes for each slot.
What can go inside a fallback
Fallback is not limited to text. You can put any valid template content between the slot tags, including elements, base components, and expressions.
<!-- card.html (footer section) -->
<footer>
<slot name="footer">
<lightning-button label="Close" onclick={handleClose}></lightning-button>
</slot>
</footer>Who owns fallback content
The child component owns fallback content, because it is part of the child's template. This differs from slotted content, which the parent owns.
- Expressions such as
{title}resolve against the child's properties. - Handlers such as
onclick={handleClose}must exist in the child's class. - The child's CSS file styles the fallback.
When the parent supplies content, the fallback is not displayed, so its handlers and styles do not apply to the parent's content.
When a slot counts as filled
A slot is filled when the parent assigns at least one node to it. The check looks at whether a node was assigned, not at whether that node looks empty. This causes two common surprises.
- An empty element still fills the slot.
<span slot="header"></span>is assigned to the header slot, soUntitleddoes not appear and the header is blank. - A parent expression that renders nothing leaves the fallback visible only if the whole element is not rendered. Put the condition on the slotted element itself.
The second case looks like this.
<!-- conditionalHeader.html -->
<template>
<c-card>
<template lwc:if={hasTitle}>
<span slot="header">{title}</span>
</template>
<p>Two items in your cart.</p>
</c-card>
</template>// conditionalHeader.js
import { LightningElement } from 'lwc';
export default class ConditionalHeader extends LightningElement {
title = 'Order summary';
hasTitle = false;
}With hasTitle set to false, no node is assigned and the card shows Untitled. Set it to true and the span replaces the fallback. If you wrote <span slot="header">{title}</span> without the condition and title were an empty string, the span would still be assigned and the header would be blank.
Fallback and slotchange
Fallback content is not a slotted node. assignedNodes() on an empty slot returns an empty array, even though the fallback is visible. The slotchange handler from the earlier example therefore reports hasFooter as false when only fallback is showing, which is usually what you want.
If you need the fallback nodes too, pass the flatten option.
// card.js (excerpt)
handleFooterChange(event) {
const slot = event.target;
const assigned = slot.assignedNodes();
const visible = slot.assignedNodes({ flatten: true });
this.hasParentFooter = assigned.length > 0;
this.hasVisibleFooter = visible.length > 0;
}hasParentFooter tells you whether the parent supplied content. hasVisibleFooter also counts the fallback.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Fallback never appears | The parent renders an empty element with the matching `slot` attribute | Render the element conditionally, as in the `lwc:if` example |
| Fallback handler does nothing | The handler is defined in the parent class instead of the child | Define it in the child, since the child owns the fallback |
| Footer container shows when empty | Code checks the slot element itself, which always exists | Check `assignedNodes().length` instead |
Best practice
Use fallback for defaults that make the component usable alone, such as a placeholder title or a standard close button. Keep it short. If the default needs real logic, a public property with a default value may fit better, and a later chapter compares the two approaches.
Exercise
A parent writes <span slot="header">{title}</span> inside c-card, and title is an empty string. Does the header slot show Untitled? Why or why not, and how would you make it show Untitled?
Show answer
No. The span is still assigned to the header slot even though it renders no text, so the slot counts as filled and the fallback is hidden. Wrap the span in <template lwc:if={title}> so the element is not rendered when title is empty. Then no node is assigned and Untitled appears.
Exercise
A card shows its fallback "Close" button, which has an onclick={handleClose} handler. Which class must define handleClose, the card or the parent, and why?
Show answer
The card. Fallback content is part of the card's template, so the card owns its expressions and handlers. The parent's class is not consulted.