Sub topic 14
Closing a Modal and Handling the Result
This chapter assumes you can write a basic Lightning Web Component, use @api properties, and work with Promises and async/await. It also assumes you have seen the modal template structure and the static open method from earlier chapters.
How a modal returns a value
A modal does not dispatch an event to hand back its answer. It calls this.close(result). The close method is inherited from LightningModal. It removes the modal from the page and resolves the Promise that open returned, using result as the resolved value.
The modal side
Each button calls close with a value that describes the user's choice.
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 result can be any value: a string, a boolean, or an object. Use an object when the modal collects more than one piece of data.
The opener side
open returns a Promise, so await it and branch on the result.
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
}
}
}Code after the await runs only once the modal has closed. Until then, the function is paused and the rest of the page stays responsive.
What the result is when the user dismisses the modal
Users can also close a modal with the X button or the Escape key. In that case no one calls this.close(...) with a value, so the Promise resolves with undefined. Write the opener so that anything other than your success value is treated as "do nothing":
if (result !== 'confirm') {
return;
}This check handles the explicit 'cancel' result and the dismissed case with one line. It is safer than checking for 'cancel' alone.
Common errors
- Checking only for the negative value. If you test
result === 'cancel'and proceed otherwise, a dismissed modal (undefined) would continue the action. Test for the positive value instead. - Forgetting that
closeis notreturn. Code afterthis.close(...)in the modal still runs. Addreturnif you do not want that. - Expecting a result from
disableClose. If you setdisableClosewhile work is in progress, the user cannot dismiss the modal, so you must callcloseyourself when the work finishes. - Not handling Promise rejection.
awaitonopenonly throws if opening fails. Wrap work that follows the modal intry/catch, as the final chapter does.
Exercise
A modal's Cancel button calls this.close('cancel'), and its Confirm button calls this.close('confirm'). The opener runs const result = await MyModal.open(); and then deletes the record whenever result !== 'cancel'. What goes wrong, and how do you fix it?
Show answer
If the user closes the modal with the X button or the Escape key, close is never called with a value, so result is undefined. Since undefined !== 'cancel' is true, the record is deleted even though the user did not confirm. Fix it by checking for the positive value: if (result === 'confirm') { /* delete */ }, or return early with if (result !== 'confirm') { return; }.