Sub topic 11
LightningModal Basics
This chapter assumes you can build a basic Lightning Web Component (HTML template, JavaScript class, metadata file). It also assumes you know @api, dispatchEvent, and Promises with async/await.
What LightningModal is
LightningModal is a base class from the lightning/modal module. A modal is a dialog that sits on top of the page and blocks interaction with it until the user responds. You don't place a modal in another component's template. You create a component that extends LightningModal, and a parent component opens it from JavaScript.
This differs from most LWC work. A normal component is rendered from a parent's template. A modal component is opened by calling a static method, and it hands a result back through a Promise.
The pieces of a modal
A modal has three parts:
- A component class that extends
LightningModal. - A template built from header, body, and footer components.
- A parent component that opens it and waits for the result.
The modal class below extends LightningModal instead of LightningElement. It receives data through @api properties and returns a result with this.close().
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 template uses three components as its top-level content:
<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 label value in the header comes from the options passed to open(). LightningModal provides it, so you don't declare it yourself.
Opening the modal
The parent imports the modal class and calls its static open() method. The call returns a Promise. That Promise resolves with whatever value the modal passes to close().
import { LightningElement } from 'lwc';
import ConfirmDeleteModal from 'c/confirmDeleteModal';
export default class ModalOpener extends LightningElement {
async handleDeleteClick() {
const result = await ConfirmDeleteModal.open({
size: 'small',
description: 'Confirm deletion of the selected record',
label: 'Confirm delete',
recordName: 'Acme Corp'
});
if (result === 'confirm') {
// continue with the delete
}
}
}The object passed to open() mixes two kinds of values. size, description, and label are options for the modal itself. Any other key, such as recordName, sets the modal's @api property of the same name.
Common errors
- Extending the wrong class. A modal that extends
LightningElementhas noclose()method and can't be opened withopen(). - Missing
description. Screen readers use it to announce the modal. Always provide it. - Ignoring the dismiss case. If the user closes the dialog with the X button or Escape, the Promise resolves with
undefined. Check for the exact value you expect, as the=== 'confirm'test does above. - Mixing up property names. A typo in a key such as
recordnamewon't setrecordName, and the modal renders blank text.
Exercise
A parent calls await MyModal.open({ size: 'small', label: 'Edit', accountName: 'Acme' }). The user presses Escape. What does await return, and which key sets an @api property on the modal?
Show answer
It returns undefined, because the modal was dismissed without calling close() with a value. The accountName key sets the modal's @api accountName property. size and label are modal options.