Sub topic 13
Opening a modal and passing options and properties
This chapter assumes you can write a basic LWC with @api properties, and that you are comfortable with Promises and async/await. It also assumes you have seen the modal component structure (header, body, footer) from the previous chapter.
How a modal is opened
A modal is not placed in a parent's template. Instead, the parent imports the modal class and calls its static open() method. The call returns a Promise. That Promise resolves when the modal closes, with whatever value the modal passed to this.close().
Options and properties
The object you pass to open() holds two kinds of values.
- Options are read by the modal framework. They control how the modal looks and how it is announced to assistive technology.
- Properties are any other keys. Each key is assigned to the
@apiproperty of the same name on the modal.
| Key | Kind | Purpose |
|---|---|---|
| `size` | Option | Modal width: `small`, `medium`, `large`, or `full`. The default is `medium`. |
| `description` | Option | Accessible description that screen readers announce when the modal opens. |
| `label` | Option | Accessible label for the modal. The modal's own header can use it too. |
| `disableClose` | Option | When `true`, the user cannot dismiss the modal with the Escape key or the close button. The modal must close itself. |
| `recordName` | Property | Any key you declare with `@api` in the modal class. |
A complete example
The modal declares recordName with @api.
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 opener passes both options and the property in one object.
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
}
}
}Here size, description, and label are options. recordName is copied onto the modal's @api recordName, so the modal's template can show it as {recordName}.
Common errors
- Property is
undefinedin the modal. The key inopen()must match the@apiproperty name exactly, including case. A property without@apiis not set. - Nothing happens after
await. The Promise resolves only when the modal callsclose(). If no button calls it, the code afterawaitnever runs. - Treating a dismissed modal as confirmed. If the user closes the modal with Escape or the X button, the Promise resolves with
undefined. Compare against the exact value you expect, as the example does withresult === 'confirm'. - Missing
description. Without it, screen reader users get no summary of the modal's purpose. Set it every time.
Exercise
You call MyModal.open({ size: 'large', accountId: '001xx000003DGb2' }), but this.accountId is undefined inside the modal. Name two likely causes.
Show answer
The modal class does not declare @api accountId, or the declared name differs in case or spelling from the key passed to open(). Only keys that match an @api property are assigned. Here size is an option and is handled by the framework.
Exercise
Why can code that checks if (result) be a poor way to detect confirmation?
Show answer
A modal that the user dismisses with Escape or the close button resolves its Promise with undefined, but a modal can also close with other truthy values such as 'cancel'. A truthy check would treat 'cancel' as a confirmation. Compare against the exact expected value, for example result === 'confirm'.