Sub topic 2
The default slot
This chapter assumes you can build a Lightning web component with an HTML template, a JavaScript class, and a CSS file. It also assumes you can place a child component in a parent template and use @api properties. The previous chapter explained why components need a way to accept markup. This chapter shows the simplest way to do it.
What the default slot is
A slot is a placeholder in a child component's template. The parent fills it with markup. The default slot is the slot with no name attribute. Any child element in the parent's markup that does not ask for a named slot goes into it.
The child defines the layout, and the parent supplies the content. The child does not need to know what the content is, so one component can wrap many different kinds of content.
A child with one default slot
This panel component draws a bordered box with a heading. The <slot></slot> element marks where the parent's content appears.
<!-- panel.html -->
<template>
<section class="panel">
<h2 class="title">{title}</h2>
<div class="content">
<slot></slot>
</div>
</section>
</template>// panel.js
import { LightningElement, api } from 'lwc';
export default class Panel extends LightningElement {
@api title = '';
}The title is a public property because it is a single string. The content is a slot because it is arbitrary markup. Chapter 6 covers that choice in detail.
Filling the slot from a parent
The parent writes content between the opening and closing tags of <c-panel>. The <p> and <lightning-button> below go into the default slot.
<!-- cart.html -->
<template>
<c-panel title="Order summary">
<p>You have {itemCount} items in your cart.</p>
<lightning-button label="Checkout" onclick={handleCheckout}></lightning-button>
</c-panel>
</template>// cart.js
import { LightningElement } from 'lwc';
export default class Cart extends LightningElement {
itemCount = 2;
handleCheckout() {
console.log('Checking out', this.itemCount, 'items');
}
}Both {itemCount} and handleCheckout resolve against cart, not panel. The parent wrote that markup, so the parent owns it. The child only decides where it appears. Chapter 5 covers ownership in full.
The diagram shows where each piece of markup ends up.
How content is assigned
The framework treats everything inside <c-panel> as the component's slotted content. It sorts that content like this:
- Elements with a
slot="name"attribute go to the slot with that name. - All other elements, and any non-whitespace text, go to the default slot.
You can mix element types and put several elements in the slot. They appear in the same order as in the parent's template.
| Parent writes | Result |
|---|---|
| An element with no `slot` attribute | Rendered in the default slot |
| Several elements with no `slot` attribute | All rendered in the default slot, in source order |
| Plain text | Rendered in the default slot |
| An element with `slot="x"` when the child has no slot named `x` | Not rendered |
| Content inside a component whose template has no slot | Not rendered |
Common errors
Content disappears. If the child template has no <slot>, the parent's content is silently discarded. There is no error. When content does not show up, check the child's template first.
Two default slots. A template should contain only one unnamed slot. In the browser's shadow DOM model, only the first unnamed slot receives the content. A second one stays empty. If you need two regions, use named slots, which the next chapter covers.
Expecting the child's CSS to style the content. Slotted elements belong to the parent's template. A rule such as .content p { color: red; } in panel.css does not reach the paragraph, because that paragraph is not in the panel's template. Style the content in the parent's CSS. Use a CSS class on the slotted element, or style the wrapper in the child, such as the .content div.
Handlers in the wrong place. A handler on <lightning-button> must exist on the parent class, because the parent wrote that markup. Adding handleCheckout to panel.js does nothing.
Querying slotted content too early. Slotted elements are not children of the child's template. this.template.querySelector('p') in panel.js returns null for the paragraph above. Chapter 8 shows how to read slotted nodes from the child.
Summary of the model
- The default slot is
<slot></slot>with no name. - Unlabeled content between the child's tags in the parent goes into it.
- The parent owns the slotted markup, so bindings and handlers live in the parent.
- A child without a slot drops the content silently.
Exercise
A parent contains <c-panel title="Notes"><p class="note">{message}</p></c-panel>. The panel.html template has a <slot></slot> inside a div, but the paragraph does not appear, and panel.css has a rule .note { font-style: italic; } that has no effect. Where is message resolved, and why does the rule fail?
Show answer
message is resolved on the parent component's class, because the parent wrote the <p>. The .note rule in panel.css fails because the paragraph is part of the parent's template, so the child's styles do not apply to it. Put the rule in the parent's CSS file. If the paragraph does not render at all, check that panel.html really contains the slot inside its <template> and that the paragraph has no slot attribute naming a missing slot.