The lightning/alert module lets you create an alert modal within 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 will launch the alert modal, and call LightningAlert.open() with your desired attributes.
This example creates an alert modal with an error message and 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}
Component Styling
This component uses the Salesforce Lightning Design System (SLDS) prompt blueprint.
LightningAlert supports the following attributes:
message: Message text that displays in the alert.
label: Header text, also used as the aria-label. Default string is Alert.
variant: Two values, header and headerless. Default value is header.
theme: Color theme for the header. The theme attribute supports the following options from SLDS:
default: white
shade: gray
inverse: dark blue
alt-inverse: darker blue
success: green
info: gray-ish blue
warning: yellow
error: red
offline: black
If an invalid value is provided, LightningAlert uses the default theme. LightningAlert ignores the style attribute.
Testing Your Component's Alert
Code using LightningAlert can be tested by mocking the LightningAlert.open() method.
The example below uses a button to open an alert dialog with text that changes when the alert opens.
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 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".