The lightning/prompt module provides the LightningPrompt component to create a prompt modal within your component. Use LightningPrompt on a component to ask the user to provide information before they continue.
Use LightningPrompt.open() instead of the native window.prompt() for a more consistent user experience. They have similar functions, but LightningPrompt.open() works in cross-origin iframes, where the .prompt() method is no longer supported in Chrome and Safari. Unlike window.prompt(), LightningPrompt.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 prompt has closed.
Note
Design
lightning/prompt 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.
Import LightningPrompt from the lightning/prompt module in the component that opens the prompt modal, and call LightningPrompt.open() with your desired attributes.
This example creates a prompt modal with a header, message, and two buttons. If you enter text and click OK in the prompt, the .open() function returns a promise that resolves to the input value. If you click Cancel, the function returns a promise that resolves to null.
1import{LightningElement}from "lwc";2import LightningPrompt from "lightning/prompt";34export default class MyApp extends LightningElement{5 handlePromptClick(){6 LightningPrompt.open({7 message: "this is the prompt message",8 //theme defaults to "default"9 label: "Please Respond", // this is the header text10 defaultValue: "initial input value", //this is optional11}).then((result)=>{12 //Prompt has been closed13 //result is input text if OK clicked14 //and null if cancel was clicked15});16}17}
LightningPrompt supports these attributes:
message: Message text that displays in the prompt.
defaultValue: Optional. Initial leading text for the input text box.
label: Header text, also used as the aria-label. Default string is Prompt.
1import LightningPrompt from 'lightning/prompt';2jest.mock('lightning/prompt');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 string representing input text if testing when user clicks "OK"12 // Pass null if testing when user clicks "Cancel"13 LightningPrompt.open = jest.fn().mockResolvedValue('test value');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('test value');26 // Open triggered once27 expect(LightningPrompt.open.mock.calls).toHaveLength(1);28})
Attributes
Name
Description
Type
Default
Required
default-value
Default value for input.
string
label
Value to use for header text in "header" variant or aria-label in "headerless" variant.
string
Prompt (Localized Value)
message
Text to display in the prompt.
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 prompt. Valid values are "header" and "headerless".