A modal dialog that takes focus, traps it, makes the page inert and returns focus on close, following the APG modal dialog pattern in each framework.
A window over the page: it takes focus, keeps it inside, makes the rest of the page inert and
stops it scrolling, and gives focus back when it closes. It follows the WAI-ARIA APG
modal dialog pattern. Non-modal floating
content is a Popover; a dialog the user must answer, never dismissed from
outside, is an AlertDialog.
Open: a press on the Trigger (Enter and Space, as any button). Focus moves to
initialFocus, else the first tabbable element of the popup, else the popup itself.
Inside: Tab and Shift+Tab wrap inside the popup; everything outside the portal is inert,
and the page does not scroll (its scrollbar gutter is kept, so nothing shifts).
Close: Escape (reason escape-key), a press outside the popup (outside-press), a Close
part (close-press) or the Trigger again (trigger-press). Focus moves to finalFocus, else
the trigger, else the element focused before it opened.
Change details: onOpenChange(open, details) is called before the state changes, with
the reason, the DOM event and the trigger; details.cancel() keeps the dialog as it is (it
keeps working, focus and inertness included). A close of the handle or of the actions has the
reason imperative-action and returns focus as none does.
Layers: the dialog is a layer of the nested overlay tree.
Escape closes the newest layer only; a press inside the dialog closes the overlays opened from
it, not the dialog; closing the dialog closes them all (reason none).
Nested dialogs: a Dialog whose Root is inside another dialog's Popup is nested: its parts
carry data-nested, the parent's data-nested-dialog-open, and the parent Popup's
--nested-dialogs counts the open dialogs below it (2 for a child with its own child open,
0 when none), so a recipe can scale or offset it. A nested Backdrop renders nothing unless
forceRender. A press on a nested dialog's Viewport outside its Popup closes that dialog only.
Detached triggers: Triggers anywhere in the page open the same Root through a handle, each
with a payload the content reads (detached triggers). Only
the active trigger reports the open state, and focus returns to it. The handle's
open(triggerId), openWithPayload(payload) and close() act from code (reason
imperative-action), isOpen reads the state; a call with no Root attached does nothing.
Animation and lifecycle: the Backdrop, the Viewport and the Popup carry
data-starting-style on their first frame and data-ending-style while closing, and unmount
when their animations end; onOpenChangeComplete(open) reports the end of each transition. With
the Portal's keepMounted, they stay in the page while closed, hidden and data-closed.
Controlled: open and onOpenChange, with useState. The actions: actionsRef={ref}, then
ref.current?.close(). Detached triggers: const handle = Dialog.createHandle<User>(),
<Dialog.Trigger handle={handle} payload={user}> anywhere, and the Root's children as a function
{({ payload }) => ...}.
Every part also takes what the primitive contract gives:
render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which
it forwards. The names below are React's; Vue binds them the same way (v-model:open,
@open-change), and Angular as inputs and outputs ([(open)], (openChangeDetails)).
every change, with its reason (trigger-press, close-press, escape-key, outside-press); called before the change, details.cancel() stops it
onOpenChangeComplete
(open: boolean) => void
once an opening or a closing has finished its transitions (not on the first render, nor for an exit a reopening interrupted)
actionsRef (react)
{ current: OverlayActions | null }
receives close() (reason imperative-action) and unmount() (ends an exit now); Vue exposes them on the Root's template ref, Angular as the root directive's methods
aria-haspopup="dialog", aria-expanded ("true" on the active trigger while open), aria-controls (the popup, on the active trigger while open), data-popup-open (the active trigger), data-disabled, an id
Backdrop
aria-hidden="true", data-open / data-closed, data-starting-style, data-ending-style, data-nested, data-nested-dialog-open; hidden while kept mounted and closed
Viewport
role="presentation", data-open / data-closed, data-starting-style, data-ending-style, data-nested, data-nested-dialog-open; hidden while kept mounted and closed
Popup
role="dialog", aria-modal="true", aria-labelledby, aria-describedby, tabindex="-1", data-open / data-closed, data-starting-style, data-ending-style, data-nested, data-nested-dialog-open, the inline --nested-dialogs: <n>; hidden while kept mounted and closed