Prompt
lightning-prompt
Create a prompt modal within your component that asks the user to provide information before they continue.
For Use In
Lightning Experience, Experience Builder Sites, Lightning Out (Beta), Standalone Lightning App
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.
| SLDS 1 | SLDS 2 | |
|---|---|---|
| Design | Prompt | Prompt |
| For Use In | Lightning Experience, Experience Builder sites, Lightning Out (Beta), Standalone Lightning app | Lightning Experience |
Usage
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.
1<!-- c/myApp.html -->
2<template>
3 <lightning-button onclick={handlePromptClick} label="Open Prompt Modal">
4 </lightning-button>
5</template>1import { LightningElement } from "lwc";
2import LightningPrompt from "lightning/prompt";
3
4export 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 text
10 defaultValue: "initial input value", //this is optional
11 }).then((result) => {
12 //Prompt has been closed
13 //result is input text if OK clicked
14 //and null if cancel was clicked
15 });
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 thearia-label. Default string isPrompt.variant: See the Variants section.theme: See the Themes section.
Component Styling
Use a combination of variants, themes, and utility classes to customize your prompts.
Variants
Specify the variant attribute with one of these values.
headeris the default variant, which displays the prompt with header textheaderlessdisplays the prompt without header text
Themes
Specify the theme attribute for the prompt header with one of these color options from SLDS.
default: whiteshade: grayinverse: dark bluealt-inverse: darker bluesuccess: greeninfo: gray-ish bluewarning: yellowerror: redoffline: black
If an invalid value is provided, LightningPrompt uses the default theme. LightningPrompt ignores the style attribute.
Utility Classes
To apply additional styling, use the SLDS utility classes with the class attribute.
Test Your Prompt
Code using LightningPrompt can be tested by mocking the LightningPrompt.open() method.
This example uses a button to open a prompt dialog and sets the result in a template.
1<button data-button onclick={handleClick}>Open Prompt</button>
2<div data-result>{result}</div>1import LightningPrompt from 'lightning/prompt';
2jest.mock('lightning/prompt');
3
4test(() => {
5 // Create and appendChild(element)
6
7 const buttonEle = element.shadowRoot.querySelector('[data-button]');
8 const resultEle = element.shadowRoot.querySelector('[data-result]');
9
10 // 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 value
15 expect(resultEle.textContent).toBe('unknown');
16 // Click modal open button
17 buttonEle.click();
18
19 // Click handler render cycle
20 await Promise.resolve();
21 // Render cycle triggered by tracked value {result}
22 await Promise.resolve();
23
24 // Verify result is set in the template
25 expect(resultEle.textContent).toBe('test value');
26 // Open triggered once
27 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". | string | header |