The lightning/confirm module creates a confirm modal within your component. Use LightningConfirm on your component to ask the user to respond before they continue.
Use LightningConfirm.open() instead of the native window.confirm() for a more consistent user experience. They have similar functions, but LightningConfirm.open() works in cross-origin iframes, where the .confirm() method is no longer supported in Chrome and Safari. Unlike window.confirm(), LightningConfirm.open() doesn’t halt execution on the page, it returns a Promise. Use async/await or .then() for any code you want to run after the confirm has closed.
Import LightningConfirm from the lightning/confirm module in the component that opens the confirm modal, and call LightningConfirm.open() with your attributes.
This example creates a headerless confirm modal with two buttons, OK and Cancel. The .open() function returns a promise that resolves to true when you click OK and false when you click Cancel.
1import{LightningElement}from "lwc";2import LightningConfirm from "lightning/confirm";34export default class MyApp extends LightningElement{5 async handleConfirmClick(){6 const result = await LightningConfirm.open({7 message: "this is the prompt message",8 variant: "headerless",9 label: "this is the aria-label value",10 // setting theme would have no effect11});12 //Confirm has been closed13 //result is true if OK was clicked14 //and false if cancel was clicked15}16}
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.
1import LightningConfirm from 'lightning/confirm';2jest.mock('lightning/confirm');34test(()=>{5 // Create and appendChild(element)67 const buttonEle = element.shadowRoot.querySelector('[data-button]');8 const resultEle = element.shadowRoot.querySelector('[data-result]');910 // Mock .open()11 // Pass true if testing when user clicks "OK"12 // Pass false if testing when user clicks "Cancel"13 LightningConfirm.open = jest.fn().mockResolvedValue(true);14 // Initial value15 expect(resultEle.textContent).toBe('unknown');16 // Click modal open button17 buttonEle.click();1819 // Click handler render cycle20 await Promise.resolve();21 // Render cycle triggered by tracked value {result}22 await Promise.resolve();2324 // Verify result is set in the template25 expect(resultEle.textContent).toBe('true');26 // Open triggered once27 expect(LightningConfirm.open.mock.calls).toHaveLength(1);28})
Attributes
Name
Description
Type
Default
Required
label
Value to use for header text in "header" variant or aria-label in "headerless" variant.
string
Confirm (Localized Value)
message
Text to display in the confirm modal.
string
theme
Theme to use when variant is "header". Valid values are "default", "shade", "inverse", "alt-inverse", "success", "info", "warning", "error", and "offline".
string
default
variant
Variant to use for the confirm modal. Valid values are "header" and "headerless".