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.