SfSkills Domains Docs GitHub

Home / lwc / lwc-lightning-modal

lwc-lightning-modal

LightningModal base class (Winter '23+): extending LightningModal, open() static method, modal headers/bodies/footers, close() with result, size variants, accessibility. NOT for lightning-dialog legacy patterns (deprecated) — use lwc/lwc-modal-and-overlay.

Open the skill folder on GitHub


LWC Lightning Modal

Activate when building a modal dialog in LWC — confirmation prompts, inline forms, multi-step wizards. LightningModal (GA Winter '23) replaces the earlier ad-hoc modal patterns and handles focus trap, accessibility, and lifecycle consistently.

Before Starting

Core Concepts

Defining a modal component

import LightningModal from 'lightning/modal';
export default class MyConfirm extends LightningModal {
    @api label;
    handleOk() { this.close('ok'); }
    handleCancel() { this.close('cancel'); }
}

Template uses <lightning-modal-header>, <lightning-modal-body>, <lightning-modal-footer>.

Opening the modal

import MyConfirm from 'c/myConfirm';
async handleOpen() {
    const result = await MyConfirm.open({
        label: 'Confirm Delete',
        size: 'small',
        description: 'Confirm deletion'
    });
    if (result === 'ok') { ... }
}

open() returns a Promise that resolves when this.close(value) is called.

Sizes

small, medium, large, full — set via the size option.

Accessibility

Header slot becomes the accessible label automatically. Focus trapping is built-in. First focusable element gets focus on open.

Passing data in / out

Common Patterns

Pattern: Confirmation dialog

const ok = await ConfirmModal.open({ label: 'Delete record?' });
if (ok === 'confirm') { /* delete */ }

Pattern: Inline form modal

Modal hosts a lightning-record-edit-form; on save, call this.close(recordId). Parent refreshes its data.

Pattern: Multi-step wizard

Maintain step state inside the modal class; each step is a different rendered fragment. Final step calls this.close(allCollectedData).

Decision Guidance

NeedMechanism
Confirmation or promptLightningModal
Inline-embedded panelRegular LWC with SLDS panel classes
Full-screen takeoverLightningModal size='full'
Non-modal popoverSLDS popover utility CSS
Legacy dialog codeMigrate to LightningModal
  1. Create the modal component extending LightningModal.
  2. Define @api inputs for data passed in.
  3. Template uses modal header / body / footer slots.
  4. Implement close paths with meaningful result values.
  5. Parent calls await MyModal.open(options) and handles result.
  6. Test keyboard navigation (Tab trap, Esc closes).
  7. Test screen reader announces modal label.

Review Checklist

Salesforce-Specific Gotchas

  1. Modal component must not render in parent template. Use open() only.
  2. Dismissing via Esc or outside-click resolves with undefined. Handle undefined as "cancelled."
  3. API not available before Winter '23. Older orgs need alternative patterns.

Output Artifacts

ArtifactDescription
Modal LWCSubclass extending LightningModal
Parent invocation helperawait MyModal.open(...) wrapper
A11y test checklistKeyboard + screen-reader verification

Package files