Thursday 4 August 2022

Create Notifications with LightningAlert, LightningConfirm, and LightningPrompt Modules

The Web Hypertext Application Technology Working Group (WHATWG) updated the HTML specification in early 2021 to remove support for the alert(), confirm(), and prompt() native APIs when used in a third-party context. So, Browsers like Chrome and Safari are now planning to end support for cross-origin use of the window.alert(), window.confirm(), and window.prompt() native APIs.

To address this issue in any LWC or Aura component, Salesforce has introduced LightningAlert, LightningConfirm, and LightningPrompt in the Summer'22 release. See Summer'22 release notes Click here.

So now to generate notifications from your Lightning web components, use the new modules LightningAlert, LightningConfirm, and LightningPrompt instead of native APIs.

The below examples show how to use LightningAlert, LightningConfirm, and LightningPrompt in Lightning web components.


Let's look at the example, There are 3 buttons in LWC to display the notification Alert, Confirm and Prompt. 

Below is a code and output for LightningAlert, LightningConfirm, and LightningPrompt notifications.

In this example, a modal with a header, message, and two buttons are created. If the user enters text and clicks "OK" in the prompt, the .open() function returns a promise that resolves to the input value; however, if the user clicks “Cancel,” the promise resolves to null. 


If you have any questions you can reach out to our Salesforce Consulting team here.

No comments:

Post a Comment