# How do you create custom modal dialog windows in LWC using LightningModal?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Modals, LightningModal

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-you-create-custom-modal-dialog-windows-in-lwc-using-lightningmodal

## Answer

**Basics:** Before `LightningModal` (GA from API 58.0, Spring '23), developers hand-built SLDS modals. Now a modal is itself a component class that you open from code, so you do not place it in the opener's template. Sibling modules `lightning/alert`, `lightning/confirm` and `lightning/prompt` provide simple alert, confirm and prompt dialogs.

**How it works:** Create `myModal` extending `LightningModal`. Template uses `<lightning-modal-header label="...">`, `<lightning-modal-body>` and `<lightning-modal-footer>`. In the opener: `const result = await MyModal.open({ size: 'medium', description: 'Accessible description', disableClose: false, recordId: this.recordId })`. Values passed in `open()` map to the modal's `@api` properties. Inside the modal call `this.close('okay')`, and the awaited promise resolves with that value.

**Advanced / deep dive:** `size` accepts small, medium, large and full; `description` is read by screen readers and should always be set; `disableClose` prevents closing with Escape or the X button (use for forced decisions, but then you must offer a way to close). The modal can emit events to the opener by passing an `onevent` handler property in `open()`, and the opener receives them when the modal dispatches a `CustomEvent` (for example `onselect`). Focus is managed and trapped by the framework, and header/footer slots give consistent styling. Because modals are opened programmatically they are easier to unit test by mocking the module's `open`. Limitations: support in all containers (for example some Experience Cloud templates) should be verified, and in screen actions you already have a modal, so use the action chrome instead.

**Code / config example:**
```javascript
// confirmDialog.js
import LightningModal from 'lightning/modal';
import { api } from 'lwc';
export default class ConfirmDialog extends LightningModal {
    @api message;
    handleYes() { this.close('yes'); }
    handleNo()  { this.close('no'); }
}
// confirmDialog.html
// <template>
//   <lightning-modal-header label="Confirm"></lightning-modal-header>
//   <lightning-modal-body>{message}</lightning-modal-body>
//   <lightning-modal-footer>
//     <lightning-button label="No" onclick={handleNo}></lightning-button>
//     <lightning-button variant="brand" label="Yes" onclick={handleYes}></lightning-button>
//   </lightning-modal-footer>
// </template>

// opener.js
import ConfirmDialog from 'c/confirmDialog';
async handleDelete() {
    const result = await ConfirmDialog.open({ size: 'small', description: 'Delete confirmation', message: 'Delete this record?' });
    if (result === 'yes') { /* proceed */ }
}
```

**Hidden gotchas & must-remember points:**
- `open()` is static and returns a Promise resolving on close.
- You do not include the modal in the opener's template.
- Always set `description` for accessibility.
- `disableClose` blocks Escape and the X button.
- Use `lightning/confirm` or `lightning/alert` for simple cases.
- If the user closes via Escape or X, the promise resolves with `undefined`.

**Best practice:** Return explicit results from `close()` and treat `undefined` as a cancel.

---
Source: ForceChallenges · last updated 2026-11-02
