Sub topic 12
Modal template structure: header, body, and footer
A modal built on LightningModal has a three-part layout. This chapter covers what each part does, which attributes control it, and the mistakes that break the layout. It assumes you can write a basic LWC (HTML template, JavaScript class, metadata file), use @api properties and dispatchEvent, and read async/await code. The previous chapter introduced LightningModal. The next chapters cover how to open a modal and how to return a result from it.
How a modal component differs from a normal component
A modal is an LWC bundle like any other, with three files: an HTML template, a JavaScript class, and a metadata file. Two things set it apart:
- The class extends
LightningModal(imported fromlightning/modal) instead ofLightningElement. - The template is built from three special components that give the modal its look and behavior:
lightning-modal-header,lightning-modal-body, andlightning-modal-footer.
You do not write the overlay, the backdrop, the focus trap, or the close button yourself. The platform provides them when the modal opens.
The three components
| Component | Purpose | Required? | Key attributes |
|---|---|---|---|
| `lightning-modal-header` | Title area at the top, with the built-in close (X) button | Optional | `label`, `tagline` |
| `lightning-modal-body` | Main content. It scrolls when the content is tall | Yes | None needed |
| `lightning-modal-footer` | Action area at the bottom, usually buttons | Optional | None needed |
Use all three for a dialog that asks the user to decide something. A modal that only displays information can leave out the footer. A modal without a header has no visible title, so give it a description (see below) to keep it accessible.
A complete example
This template is for a delete confirmation dialog.
<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 class that goes with it:
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');
}
}The metadata file follows the usual pattern. A modal is opened from code, so it does not need to be exposed to App Builder.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<isExposed>false</isExposed>
</LightningComponentBundle>
</parameter>Three details in this example are worth noting:
- The template uses
{label}but the class never declares alabelproperty.LightningModalalready provideslabel,size, anddescriptionas public properties. The opener sets them when it callsopen(). You only declare properties that are specific to your modal, such asrecordName. {recordName}works like any other@apiproperty. The opener passes the value in the sameopen()call.- The class calls
this.close(...)with a value. That value is what the opener's Promise resolves with. The next chapters cover that flow.
The header
The header renders the title and the close (X) button. Set the title with label. Binding it to {label} lets the opener choose the text, which makes the modal reusable. A fixed string such as label="Confirm delete" also works when the title never changes.
The optional tagline attribute adds a short line of secondary text under the title. Use it for context, such as the name of the record the modal affects.
<lightning-modal-header label={label} tagline="Account: Acme Corp"></lightning-modal-header>If you omit lightning-modal-header, the modal still opens, but it has no visible title. Do this only for a deliberate design reason.
The body
lightning-modal-body holds everything the user reads or fills in. Put text, a form, a lightning-record-edit-form, or a list inside it. The body is the part that scrolls when its content is taller than the available space, while the header and footer stay in place.
You can nest ordinary components inside the body, including your own child components. The body is where most of the modal's logic lives. For a form, you read the field values in the class, validate them, and pass a result to this.close().
The footer
lightning-modal-footer holds the action buttons. The footer lays out its children and keeps them pinned to the bottom of the modal. Put the primary action last (rightmost), as in the example above. Wire each button to a handler that calls this.close() with a different result, or that does work first and closes afterward.
A footer is not required to contain buttons, but if the user must make a choice, the choices belong here and not in the body. This keeps the layout consistent with other Salesforce dialogs.
Related properties that affect the layout
These properties are set by the opener, but they change how your template looks, so you should know them while designing the structure.
sizeissmall,medium,large, orfull. The default ismedium. A confirmation dialog suitssmall. A form with many fields suitslarge.descriptionis a text description for screen readers. Always set it. It is how assistive technology announces what the dialog is for.labelfeeds the header title, as shown above.disableClosestops the user from dismissing the modal with the X button or the Escape key. Use it briefly, for example while a save is running, and set it back tofalseafterward. Leaving it on traps the user.
Common mistakes
- Missing body. Content placed outside
lightning-modal-bodydoes not get the modal's padding and scroll behavior. Always wrap your main content in the body component. - Extending the wrong class. If the class extends
LightningElement, the modal cannot be opened withopen()andthis.close()does not exist. The class must extendLightningModal. - Wrapping the three components in another element. Keep the header, body, and footer as direct children of
<template>. A wrapperdivaround them can break the layout. - Forgetting accessibility. A modal with no
descriptionand no header label gives screen reader users no clue what it is. Set both when you can. - Putting the modal in a page layout. A
LightningModalcomponent is not placed in a page's template. It is opened from another component's JavaScript with the staticopen()method.
Summary of the structure rules
- Extend
LightningModal, notLightningElement. - Use
lightning-modal-bodyfor the main content. Addlightning-modal-headerfor a title andlightning-modal-footerfor actions. - Keep the three components as direct children of
<template>. - Use the inherited
label,size, anddescription. Add@apiproperties only for your own data. - Call
this.close(result)to dismiss the modal and send a value back to the opener.
Exercise
A teammate's modal opens, but the title is missing and the text runs to the edges with no padding. The template is <template><p>Are you sure?</p><lightning-button label="OK" onclick={handleOk}></lightning-button></template>. Rewrite the template so it has a proper title, padded content, and a footer button. Which property should the opener also set so screen readers can describe the dialog?
Show answer
Use this template: <template><lightning-modal-header label={label}></lightning-modal-header><lightning-modal-body><p>Are you sure?</p></lightning-modal-body><lightning-modal-footer><lightning-button label="OK" onclick={handleOk}></lightning-button></lightning-modal-footer></template>. The header provides the title, the body provides padding and scrolling, and the footer holds the action button. The opener should also pass description in the open() options. It should pass label too, because the header binds to it.
Exercise
Why can the template of ConfirmDeleteModal use {label} even though the class declares only @api recordName?
Show answer
LightningModal already provides label, size, and description as public properties. The opener sets them through the options passed to open(), so the subclass does not need to declare them.