Sub topic 2
Fallback Content for Empty Slots
Assume you know how a parent uses a child component and how @api properties work. This chapter focuses on what happens when a parent does not provide slot content, and which component owns that content.
Fallback content
Fallback content is markup inside a slot element. The browser displays it only when the parent supplies no matching content.
<!-- card.html -->
<template>
<article class="card">
<header>
<slot name="header">Untitled</slot>
</header>
<div class="body">
<slot>No details provided.</slot>
</div>
<footer>
<slot name="footer"></slot>
</footer>
</article>
</template>The parent can fill some slots and leave others empty:
<!-- parent.html -->
<template>
<c-card>
<span slot="header">Order summary</span>
<p>Two items are in your cart.</p>
</c-card>
</template>The header displays Order summary, and the default slot displays the paragraph. The footer has no fallback content, so its slot is empty.
Ownership of slotted content
The parent owns the nodes it passes into a slot. The child controls the surrounding layout, but it does not take ownership of the supplied markup.
| Concern | Owner | Practical result |
|---|---|---|
| Markup and data binding | Parent | Expressions use the parent’s data |
| Event handlers | Parent | Handlers declared on parent markup remain there |
| Styling slotted nodes | Parent | Put styles for supplied nodes in the parent |
| Slot wrapper layout | Child | The child can style its own container |
For example, a button passed through a footer slot keeps the parent’s data and event handler:
<c-card>
<lightning-button
slot="footer"
label={buttonLabel}
onclick={handleCheckout}>
</lightning-button>
</c-card>The child can style the <footer> wrapper, but should not depend on reaching into the slot to rewrite the button. This ownership boundary keeps components reusable.
Exercise
If a parent leaves <slot name="header">Untitled</slot> empty, what appears, and who controls that text?
Show answer
Untitled appears. The child controls it because it is fallback content in the child template.