Sub topic 16
Built-in alert, confirm, and prompt modals
This chapter assumes you can write async functions, know how LightningModal.open() returns a Promise (covered earlier), and can handle events in an LWC.
Three modals you don't have to build
Many dialogs only need a message and one answer. Salesforce ships three modules for these cases, so you don't need to write a LightningModal subclass:
| Module | Buttons | Promise resolves with |
|---|---|---|
| `lightning/alert` | OK | `undefined`, when the user closes it |
| `lightning/confirm` | OK, Cancel | `true` for OK, `false` for Cancel |
| `lightning/prompt` | OK, Cancel | the entered text for OK, `null` for Cancel |
Each module exports a class with a static open() method, like a custom modal. You pass a plain options object and await the result.
Options
message: the body text. It is plain text, not HTML.label: the heading text. Always set it, because it also names the dialog for screen readers.theme: a color theme such asdefault,success,info,warning,error, oroffline.variant: on confirm,headerlessremoves the header.defaultValue: on prompt, the text pre-filled in the input.
Example: all three in one component
import { LightningElement } from 'lwc';
import LightningAlert from 'lightning/alert';
import LightningConfirm from 'lightning/confirm';
import LightningPrompt from 'lightning/prompt';
export default class BuiltInModals extends LightningElement {
async showAlert() {
await LightningAlert.open({
message: 'Your session will expire in five minutes.',
theme: 'warning',
label: 'Session warning'
});
// Runs after the user clicks OK.
}
async askToContinue() {
const confirmed = await LightningConfirm.open({
message: 'Discard your unsaved changes?',
theme: 'warning',
label: 'Discard changes'
});
if (confirmed) {
// Discard the changes.
}
}
async askForName() {
const name = await LightningPrompt.open({
message: 'Enter a name for the new view.',
label: 'Name your view',
defaultValue: 'My view'
});
if (name === null) {
return; // The user cancelled.
}
// Use the entered name.
}
}The askToContinue method shows the main use of confirm. It replaces the custom ConfirmDeleteModal from the earlier delete flow when you don't need custom content or extra buttons.
Common mistakes
- Not checking the result.
LightningConfirm.open()always resolves, even on Cancel. Writingawait LightningConfirm.open(...)and then continuing runs your action either way. Test the returned value. - Treating an empty string as a cancel. A prompt returns
nullon Cancel. If the user clears the input and clicks OK, you get an empty string. Check for both cases if you need a non-empty value. - Expecting rich content. These modals can't hold links, lists, or extra fields. Use a
LightningModalsubclass when you need those. - Skipping
label. Without it the dialog has no useful accessible name.
Exercise
Exercise
A component calls await LightningConfirm.open({ message: 'Delete this record?', label: 'Delete' }); and then immediately calls deleteRecord(this.recordId). Users report that clicking Cancel still deletes the record. What is wrong, and how do you fix it?
Show answer
The code ignores the resolved value. The confirm Promise resolves with false on Cancel, but the code continues anyway. Store the result, as in const confirmed = await LightningConfirm.open(...), and call deleteRecord only when confirmed is true.