Sub topic 12
Modal template structure: header, body, and footer
This chapter covers how a LightningModal template is laid out. It assumes you know the basic LWC file structure, @api properties, and event handlers. Opening the modal and handling its result are covered in later chapters.
The three building blocks
A modal component extends LightningModal instead of LightningElement. Its template uses three base components as top-level content:
| Component | Purpose |
|---|---|
| `lightning-modal-header` | Shows the title through its `label` attribute. |
| `lightning-modal-body` | Holds the main content. It scrolls when the content is tall. |
| `lightning-modal-footer` | Holds the action buttons. |
The modal supplies the surrounding frame, the close button in the corner, and focus handling. You only provide the content inside these three components.
A working template
This template asks the user to confirm a delete.
<template>
<lightning-modal-header label={label}></lightning-modal-header>
<lightning-modal-body>
<p>Delete {recordName}? This cannot be undone.</p>
</lightning-modal-body>
<lightning-modal-footer>
<lightning-button label="Cancel" onclick={handleCancel}></lightning-button>
<lightning-button variant="destructive" label="Delete" onclick={handleConfirm}></lightning-button>
</lightning-modal-footer>
</template>The template reads {label} and {recordName}. Both come from the class.
import { api } from 'lwc';
import LightningModal from 'lightning/modal';
export default class ConfirmDeleteModal extends LightningModal {
@api recordName;
handleCancel() {
this.close('cancel');
}
handleConfirm() {
this.close('confirm');
}
}recordName is declared with @api, so the opener can set it. label is not declared here. LightningModal already provides an @api label property, which the opener fills through the label option. this.close() closes the modal and hands a value back to the opener. The chapter on closing modals explains that value.
Rules to remember
- Use the three components as direct, top-level content. Do not wrap them in another element.
- The header is optional in principle, but without it the modal has no visible title. Provide
labelso the title and the accessible name stay meaningful. - Put buttons in the footer, not in the body. Users expect actions at the bottom of a modal.
- You can omit the footer for a modal that has no actions beyond the close button.
Common mistakes
- Extending
LightningElementinstead ofLightningModal. The component then has noclose()method and cannot be opened as a modal. - Putting the buttons inside
lightning-modal-body. They then scroll with the content instead of staying fixed at the bottom. - Forgetting
@apion a property the opener passes in. The value is silently ignored.
Exercise
Why does ConfirmDeleteModal use @api recordName but no @api label, even though the template reads both?
Show answer
The opener passes recordName as a custom value, so the class must expose it with @api. label is already defined as an @api property by the LightningModal base class, and the opener sets it through the label option, so declaring it again is unnecessary and conflicts with the inherited one.
Exercise
Where should the Cancel and Delete buttons go in a modal template, and why?
Show answer
In lightning-modal-footer. The footer keeps the actions fixed at the bottom of the modal. Buttons in the body would scroll away with long content.