Sub topic 1
Why compose components with slots
Prerequisites
This chapter assumes you already know:
- The three files of a Lightning web component: an HTML template, a JavaScript class, and an optional CSS file.
- How to place a child component in a parent template, for example
<c-card></c-card>. - How to expose a public property on a child with
@api. - Basic HTML and DOM event handling.
Composition: building screens from parts
Composition means building a component out of smaller components. A parent template contains child components, and each child handles one job. Lightning web components (LWC) rely on this pattern.
Public properties cover many cases. The parent sets @api values on the child, and the child renders them. This works well when the data is simple, such as a string, a number, or a record.
It works poorly when the parent needs to supply markup. Consider a card component that draws a border, a padding, and a header area. Different screens want different things inside it: a paragraph, a form, a button, or a list. A property cannot carry that content in a clean way.
The limit of properties
Here is a card that takes its body as a property.
<!-- card.html -->
<template>
<article class="card">
<p>{bodyText}</p>
</article>
</template>// card.js
import { LightningElement, api } from 'lwc';
export default class Card extends LightningElement {
@api bodyText;
}This card can show plain text and nothing else. To add a link, a bold word, or a button, you would need more properties, such as bodyHtml, showButton, buttonLabel, and buttonVariant. The list keeps growing, and the child ends up knowing about every use case.
Slots: the parent supplies markup
A slot is a placeholder in a child's template. The parent puts markup between the child's opening and closing tags, and the browser renders that markup at the slot's position. The child controls the layout. The parent controls the content.
The same card, rewritten with a slot:
<!-- card.html -->
<template>
<article class="card">
<slot></slot>
</article>
</template><!-- parent.html -->
<template>
<c-card>
<p>Two items in your cart.</p>
<lightning-button label="Checkout"></lightning-button>
</c-card>
</template>The card now accepts a paragraph and a button without any new properties. It does not need to know what it will hold. The next chapter covers this unnamed slot in detail, and later chapters add named slots and fallback content.
How the content reaches the slot
The parent writes the content, but the child decides where it appears. The diagram shows the flow.
Two points follow from this flow, and later chapters build on both:
- The markup stays owned by the parent. Its data bindings, event handlers, and most of its styling come from the parent's component, not the child's.
- The child never receives the content as a value. It only provides a place for it, so it cannot read it the way it reads an
@apiproperty.
Slots or properties: a first rule of thumb
Chapter 6 compares the two approaches in depth. For now, use this starting rule.
| You need to pass | Use |
|---|---|
| A string, number, boolean, or object the child will render in its own way | A public property |
| Markup of any shape, or other components, that the child only places | A slot |
If the child must make decisions based on the value, such as formatting a date or validating a number, use a property. If the child only provides a frame and a position, use a slot.
What composition with slots gives you
- Reuse. One card, modal, or layout component serves many screens.
- A small public API. The child needs no property for every content variation.
- Clear responsibility. The child owns structure. The parent owns content and behavior.
A quick check on the pattern
When you build a new component, ask one question: does this part of the template depend on content that callers will want to change freely? If so, put a <slot> there. If the part is fixed, keep it in the template.
Exercise
A teammate builds a c-panel component with the properties titleText, bodyText, showSaveButton, and saveLabel. Requests keep arriving for new panel contents, such as a form or a list. Which change reduces the property list, and what does the panel still do after the change?
Show answer
Replace bodyText, showSaveButton, and saveLabel with a <slot></slot> in the panel template, so the parent passes the body markup and any button directly. The panel keeps its frame (border, padding, and layout) and places whatever the parent supplies. A short titleText property can stay, because the panel only renders a plain string.
Exercise
In the slot version of c-card, the parent writes <lightning-button label="Checkout"></lightning-button> between the card tags. Which component owns that button, and where does the button appear in the page?
Show answer
The parent owns the button. Its bindings and handlers come from the parent's JavaScript class. It appears inside the card, at the position of the card's <slot> element.