Sub topic 13
Composing components with slots
This chapter assumes you know how @api properties pass data into a child, and that you know the basics of shadow DOM and the <slot> element. Properties pass values. Slots pass markup.
What a slot does
A slot is a placeholder in a child component's template. The parent puts elements between the child's tags, and the browser renders them where the <slot> sits. The child decides the layout. The parent decides the content.
A component with named slots
The child declares one default slot and two named slots. The header slot has fallback content.
<!-- cardPanel.html -->
<template>
<section>
<header><slot name="header">Untitled</slot></header>
<div><slot></slot></div>
<footer><slot name="footer"></slot></footer>
</section>
</template>// cardPanel.js
import { LightningElement } from 'lwc';
export default class CardPanel extends LightningElement {}The parent targets a named slot with the slot attribute. Elements without it go to the default slot.
<!-- orderPage.html -->
<template>
<c-card-panel>
<h2 slot="header">Orders</h2>
<p>Three orders are open.</p>
<button slot="footer" onclick={handleRefresh}>Refresh</button>
</c-card-panel>
</template>// orderPage.js
import { LightningElement } from 'lwc';
export default class OrderPage extends LightningElement {
handleRefresh() {
console.log('Refresh requested');
}
}The <button> belongs to orderPage, not to cardPanel. Its {handleRefresh} binding resolves against the parent's class.
Rules to remember
- Fallback content inside a
<slot>renders only when the parent supplies nothing for that slot. - Several elements can share one named slot. They appear in the order the parent wrote them.
- Slotted elements stay in the parent's tree. They are not moved into the child's shadow root.
- The parent's CSS styles slotted content, because the parent owns it. The child's CSS cannot reach it directly.
Reacting to slot content
A slot fires a standard slotchange event when its assigned nodes change. Attach it with onslotchange.
<slot onslotchange={handleSlotChange}></slot>handleSlotChange(event) {
const nodes = event.target.assignedNodes();
this.hasBody = nodes.length > 0;
}Common errors
- Querying slotted content with
this.template.querySelector. That searches only the child's own shadow tree. Usethis.querySelectorto find elements the parent slotted in. - Misspelling a slot name. The element then matches no named slot and is not rendered. The browser reports no error.
- Expecting the child to style slotted markup. Put those styles in the parent, or expose a property that changes behavior.
Exercise
A parent writes <c-card-panel><span slot="title">Hi</span></c-card-panel>, but cardPanel.html declares only header and footer slots. What does the user see, and why?
Show answer
The span is not rendered. The slot name title matches no slot in the child's template, so the browser has nowhere to place the element. The mismatch produces no error. The fix is to rename the slot attribute or add a <slot name="title"> to the child.