Use the lightning/alert module to create and open an alert modal from your component. Use LightningAlert on your components to communicate a state that affects the entire system, not just a feature or page.
Use LightningAlert.open() instead of the native window.alert() for a more consistent user experience. They have similar functions, but LightningAlert.open() works in cross-origin iframes, where the .alert() method is no longer supported in Chrome and Safari. Unlike window.alert(), LightningAlert.open() doesn’t halt execution on the page, it returns a Promise. Use async/await or .then() for any code you want to execute after the alert has closed.
Import LightningAlert from the lightning/alert module in the component that you use to open the alert modal, and call LightningAlert.open() with your attributes.
This example creates an alert modal with an error message and an OK button. The .open() function returns a promise that resolves when you click OK.
1import{LightningElement}from "lwc";2import LightningAlert from "lightning/alert";34export default class MyApp extends LightningElement{5 async handleAlertClick(){6 await LightningAlert.open({7 message: "this is the alert message",8 theme: "error", // a red theme intended for error states9 label: "Error!", // this is the header text10});11 //Alert has been closed12}13}
Design
This module implements the prompt blueprint in the Salesforce Lightning Design System (SLDS). The prompt adapts to SLDS 1 or SLDS 2 styling based on the org’s theme or the container app that you use.
Use the keyboard interaction guidelines on the SLDS site. When the dialog opens, focus moves to the header text on the dialog, which has tabindex="-1".
To close the dialog, you can press the Esc key. Alternatively, you can tab to the OK button and press Enter.
lightning/alert observes these roles, states, and properties.
The section element has role="alertdialog" with an aria-description, which uses the value you provide for the message attribute.
The aria-labelledby value on the section is set to the ID of the header text. Provide a header text by using the label attribute.
Attributes
Name
Description
Type
Default
Required
label
Value to use for header text in "header" variant or aria-label in "headerless" variant.
string
Alert (Localized Value)
message
Text to display in the alert.
string
theme
Theme to use when variant is "header". Valid values are "default", "shade", "inverse", "alt-inverse", "success", "success", "info", "warning", "error", and "offline".
string
default
variant
Variant to use for alert. Valid values are "header" and "headerless".