Sub topic 3
Opening a Modal and Passing Options and Properties
Assume you know basic LWC structure, @api, event dispatching, and JavaScript async/await. LightningModal creates a modal component that opens asynchronously and returns a Promise.
Open a modal
A modal template uses a header, body, and footer. The modal class extends LightningModal. Values passed to open() become @api properties.
<template>
<lightning-modal-header label={label}></lightning-modal-header>
<lightning-modal-body>
<p>Delete {recordName}?</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>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 parent imports the modal component and calls its static open() method. Options such as size, label, and description control the modal. Other values, such as recordName, populate the modal's public properties.
import { LightningElement } from 'lwc';
import ConfirmDeleteModal from 'c/confirmDeleteModal';
export default class AccountActions extends LightningElement {
async handleDelete() {
const result = await ConfirmDeleteModal.open({
size: 'small',
label: 'Confirm delete',
description: 'Confirm deletion of this account',
recordName: 'Acme Corp'
});
if (result === 'confirm') {
// Continue with the operation.
}
}
}Close the modal and handle events
this.close(value) resolves the Promise returned by open(). Use the value to distinguish confirmation from cancellation. A modal can also dispatch a custom event for intermediate information, such as a selected option. The opener can provide an on<eventname> handler in the open() options.
Built-in modal utilities cover simpler interactions:
import LightningAlert from 'lightning/alert';
import LightningConfirm from 'lightning/confirm';
import LightningPrompt from 'lightning/prompt';
async function askUser() {
await LightningAlert.open({
message: 'The record was saved.',
theme: 'success',
label: 'Saved'
});
const confirmed = await LightningConfirm.open({
message: 'Continue?',
variant: 'headerless',
label: 'Confirm'
});
if (confirmed) {
const value = await LightningPrompt.open({
message: 'Enter a name',
defaultValue: '',
label: 'Name'
});
return value;
}
return null;
}Confirm, save, toast, and navigate
A common flow waits for confirmation, saves the record, shows feedback, and navigates.
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { deleteRecord } from 'lightning/uiRecordApi';
import ConfirmDeleteModal from 'c/confirmDeleteModal';
export default class DeleteAccount extends NavigationMixin(LightningElement) {
@api recordId;
async handleDelete() {
const result = await ConfirmDeleteModal.open({
size: 'small',
label: 'Confirm delete',
recordName: 'this account'
});
if (result !== 'confirm') return;
try {
await deleteRecord(this.recordId);
this.dispatchEvent(new ShowToastEvent({
title: 'Deleted',
message: 'The account was deleted.',
variant: 'success'
}));
this[NavigationMixin.Navigate]({
type: 'standard__objectPage',
attributes: { objectApiName: 'Account', actionName: 'home' }
});
} catch (error) {
this.dispatchEvent(new ShowToastEvent({
title: 'Delete failed',
message: error.body?.message || error.message,
variant: 'error',
mode: 'sticky'
}));
}
}
}Exercise
Which value should the modal pass to close() after the user confirms?
Show answer
A value such as 'confirm', which the opener checks after awaiting open().