Skip to content

GhostDialog ​

GhostDialog renders a modal or non-modal dialog overlay with accessible focus trapping, scroll locking, and background isolation. Import GhostDialog, GhostDialogTrigger, GhostDialogOverlay, GhostDialogContent, GhostDialogTitle, GhostDialogDescription, and GhostDialogClose from @ghostvue/vue for headless use, or from @ghostvue/themes for the default elevation-4 surface and scrim backdrop.

Profile editor

Profile card with an edit dialog: validated fields, a scrolling form on short screens, and real save and cancel outcomes.

AL

Ada Lovelace

Research engineer

Work email
ada@analytical-engines.example
Bio
Writes the notes that turn the Analytical Engine from a calculator into a general-purpose machine. Currently documenting loops and conditional branching for the next release.

The profile card opens an edit dialog that works on a draft copy. Save changes validates the name and email, writes the draft back to the card, and closes; focus returns to Edit profile. If a field is invalid, the dialog stays open and focus moves to the first field that needs attention. Cancel, Escape, and a backdrop click close without saving and the card stays as it was. Focus enters on the first field, the default when no initialFocus is set. The form keeps a 36rem width, the name row stacks when it gets narrow, and on short screens the whole dialog scrolls so the actions stay reachable.

While the modal is open, Tab stays inside, the background is inert, and body scroll is locked. The overlay and surface render through GhostPortal by default and unmount after their exit motion.

Themed use and long content ​

Long content

Release notes on the elevation-4 surface that scroll inside the dialog when taller than the viewport.

The themed surface caps its width and height to the viewport, minus the --ghost-space-inset-lg inset, and scrolls when the content is taller. It stays centered in both left-to-right and right-to-left documents.

Importing from @ghostvue/themes loads default.css, styling .ghost-dialog-overlay with var(--ghost-scrim) and .ghost-dialog-content with --ghost-elevation-4-*, border, hairline width, radius, inset padding, typography, and focus-ring --ghost-* tokens. Composing :as="GhostButton" delegates trigger and close styling cleanly.

Nested dialogs ​

Nested

A confirmation stacked on a share dialog; Escape dismisses only the innermost layer and focus returns to the outer dialog.

Opening a dialog from inside another dialog stacks their dismissable layers, trapped focus scopes, inert sibling sets, and scroll locks cleanly. Pressing Escape dismisses only the innermost open dialog and restores focus and isolation back to the outer dialog.

Revoking the link disables the Revoke link button that opened the confirmation, so focus cannot return to it. The confirmation passes returnFocus to send focus to Done instead. While the link is active, returnFocus returns null and the default target is kept.

Alert dialogs ​

Alert dialog

Destructive confirmation with role="alertdialog", outside-click dismissal disabled, and a status after confirming.

Passing role="alertdialog" on GhostDialogContent disables outside-click dismissal by default so destructive confirmations cannot be dismissed accidentally by clicking the backdrop, and emits a development warning (GHOST003) if no description is provided. The result is announced in a polite role="status" next to the trigger.

Non-modal and controlled use ​

  • Set :modal="false" on GhostDialog to disable aria-modal, GhostDialogOverlay, scroll lock, background inert, and Tab trapping. Non-modal dialogs open at mount follow GhostPopover's openedAtMount rule so initial render does not steal focus on page load, whereas modal dialogs always move focus inside on open.
  • Use v-model:open for controlled state, or omit open with defaultOpen for uncontrolled state.

Advanced: headless composition ​

Basic use ​

Basic Headless

Unstyled modal dialog with overlay, trapped Tab focus, scroll lock, and inert background.

Import the same dialog parts from @ghostvue/vue when your application owns the scrim, surface, and focus styles. The modal still traps focus, isolates the background, and restores focus on close.

API ​

See the generated component references for props, emits, slots, exposed members, styling hooks, CSS variables, and executable examples.

PropTypeDefaultDescription
defaultOpenbooleanfalseInitial open state when open is omitted.
modalbooleantrueEnables modal behavior (trapped focus, scroll lock, inert background, overlay).
openboolean—Controlled open state. Omit for uncontrolled mode.
SlotSlot propsDescription
defaultclose: () => void, open: booleanTrigger, overlay, content, title, description, close parts, and optional custom UI reading the scoped state.

@ghostvue/themes adds no props, events, or slots to the headless GhostDialog API, which it forwards.

GhostDialog (root) ​

Emits update:open with (open: boolean). The default slot receives { open: boolean, close: () => void }.

GhostDialogTrigger ​

Sets type="button" (when as="button"), aria-haspopup="dialog", aria-expanded, aria-controls, data-part="trigger", and data-state="open" | "closed". Exposes { element, focus, blur }.

GhostDialogOverlay ​

Renders only when both open and modal are true. Sets aria-hidden="true", data-part="overlay", and data-state="open". Exposes { element, focus, blur }.

GhostDialogContent ​

Sets role, aria-modal="true" (when modal=true), tabindex="-1", data-part="content", data-state="open", aria-labelledby (linking GhostDialogTitle), and aria-describedby (linking GhostDialogDescription). Exposes { element, focus, blur }.

Three optional props control focus entry and exit. Most dialogs never need them.

PropDefaultBehavior
focusReadytrueWhile false, focus does not enter the dialog yet. Use it while content is still positioning or rendering. Once focus has entered, the trap stays on until close.
initialFocusundefinedFunction returning the element to focus on entry. null or an invalid element keeps the default: [data-autofocus], then the first tabbable element.
returnFocusundefinedFunction returning the element to focus on close. null, or a removed or disabled element, keeps the default: the element focused when the dialog opened.
vue
<GhostDialogContent
  :focus-ready="positioned"
  :initial-focus="() => selectedCell"
  :return-focus="() => (triggerEnabled ? null : inputElement)"
>
  ...
</GhostDialogContent>

Both functions run in the browser only, at the moment focus moves. They are never called during server rendering.

GhostDialogTitle ​

Sets id and data-part="title", and registers as the dialog's accessible name unless aria-label or aria-labelledby is provided directly on GhostDialogContent. Exposes { element, focus, blur }.

GhostDialogDescription ​

Sets id and data-part="description", and registers as the dialog's description (aria-describedby) on GhostDialogContent. Exposes { element, focus, blur }.

GhostDialogClose ​

Closes the enclosing GhostDialog on click or keyboard activation and exposes { element, focus, blur }.

useGhostDialog(options?) ​

Returns { isOpen, isModal, open, close, toggle, triggerId, contentId, titleId, descriptionId } for building custom dialog compositions over the same controllable state and deterministic IDs.

Accessibility ​

  • Accessible name & description: Always render GhostDialogTitle (or pass aria-label / aria-labelledby on GhostDialogContent). Missing accessible names log GHOST002 in development. For role="alertdialog", also render GhostDialogDescription (or pass aria-describedby) to avoid GHOST003.
  • Modal isolation: Modal dialogs combine aria-modal="true" with inert on background siblings (excluding [data-ghost-live] regions), ref-counted scroll locking, and looped Tab trapping.
  • Popovers inside dialogs: Portaled GhostPopover layers opened inside a modal dialog receive normal Tab navigation without focus pull-back, whereas inline popovers (:portal="false") keep Tab bounded inside the dialog.
  • Focus restoration: Closing via Escape, GhostDialogClose, overlay click, or controlled close restores focus to the trigger or previously focused element after background inert is cleared.

Specification ​

See GhostDialog specification and ADR 0012: Modal overlays.