ADR 0012: Modal overlays
- Status: accepted
- Date: 2026-11-13
- Decision owners: repository maintainers
- Relates to: ADR 0003 (naming), ADR 0011 (overlay primitives), focus.md (
useFocusScope)
Context
Upcoming modal overlay components (GhostDialog, GhostAlertDialog, GhostDrawer, GhostCommandPalette) build on top of the overlay primitives from ADR 0011 (GhostPortal, useDismissableLayer) and useFocusScope, and introduce three additional modal-specific requirements:
- Scroll locking: preventing the underlying document from scrolling while a modal is open, without causing horizontal layout shift when classic scrollbars disappear, and nesting safely when one modal opens over another.
- Background isolation: ensuring everything outside the active modal cannot be reached by pointer, keyboard navigation, find-in-page, or assistive technology while still keeping live notification regions interactive and announced.
- Nested layer focus coordination: allowing a non-modal dismissable layer (such as a
GhostPopover,GhostSelect, orGhostDropdownMenuportaled todocument.body) opened inside a modal to acceptTabnavigation without the outer modal's trappeduseFocusScopeyanking focus back.
Per ADR 0011, headless modal primitives stay in @ghostvue/vue without importing @ghostvue/themes or adding external runtime dependencies. In this first slice (PR 1), the primitives remain internal to @ghostvue/vue until GhostDialog (PR 2) validates the public modal contract.
Decision
1. Background isolation: inert on siblings (useInertOthers) plus aria-modal="true"
Modal overlays mark their content surface with aria-modal="true" and apply the HTML inert attribute to background siblings through useInertOthers:
useInertOthers(elements, enabled)walks from each kept element up todocument.bodyand setsinerton every sibling that does not contain a kept element.- Ref-counted per element: a module-level
WeakMap<Element, number>tracks onlyinertattributes added byuseInertOthers. Elements that were alreadyinertbefore activation are never adopted or cleared. Nested modals keep shared background subtreesinertuntil the final modal closes. - No
MutationObserveron late nodes: nodes appended todocument.bodyafter activation (such as a portaledGhostPopoveropened from inside the modal) are left untouched so child overlays remain interactive. [data-ghost-live]exclusion:useInertOthersskips any sibling carrying[data-ghost-live]so live regions, screen-reader announcers, and toast containers remain perceivable and interactive while a modal is open.- SSR-safe cleanup: all DOM traversal runs after
onMountedand restores everyinertattribute it added on disable or unmount.
Why inert on siblings plus aria-modal="true" instead of aria-modal alone
aria-modal="true" alone communicates modality to supporting screen readers, but browser and assistive-technology compliance remains uneven across AT/browser pairings, and aria-modal does not stop pointer clicks, touch exploration, scroll-into-view focus, or browser-chrome Tab entry into background DOM nodes. Pairing aria-modal="true" (semantic role contract) with inert on siblings (platform enforcement of hit-testing, focusability, selection, and accessibility tree removal) gives consistent isolation without aria-hidden="true" hazards on focusable elements.
2. Scroll lock (useScrollLock)
useScrollLock(enabled)setsoverflow: hiddenon the effective viewport scroller (document.documentElementwhen rootoverflow-xoroverflow-yis notvisible, otherwisedocument.bodyper CSS Overflow 3 overflow propagation) while enabled and restores every modified inline property (overflow-x,overflow-y,padding-right) and its!importantpriority (omitting an emptystyleattribute if none existed) only when a module-level reference count drops back to0.- Scrollbar compensation: when
window.innerWidth - documentElement.clientWidth > 0,useScrollLockadds that scrollbar width to the locked target's computedpadding-rightso fixed/centered content does not shift horizontally. scrollbar-gutter: stableskip: whendocument.documentElementresolvesscrollbar-guttercontainingstable, space is already reserved by the browser, sopadding-rightcompensation is skipped to avoid double padding.- SSR-safe: defers all
windowanddocumentreads/writes untilonMountedand releases on disable or unmount.
3. Nested trap and dismissable layer rules
- Trapped focus ownership: per focus.md, only the innermost currently trapped
useFocusScopeownsTab. Untrapped scopes do not steal trap ownership from an ancestor modal. - Dismissable layer ownership: per ADR 0011, only the topmost
useDismissableLayerhandlesEscapeand outsidepointerdown. - Upper dismissable layer
Tabrule:useDismissableLayeranduseFocusScopeshare an internal layer stack (dismissable-layer-stack.ts). When a trappeduseFocusScopeobservesTab, it checks whetherdocument.activeElementsits inside a dismissable layer stacked above the scope's own layer. If so, the trapped scope ignoresTabso focus moves naturally inside a portaled popover or menu opened from within the modal, while still pulling focus back from any layer stacked below the scope.
4. Why not native <dialog> / showModal()
GhostVue builds modals from GhostPortal, useDismissableLayer, useFocusScope, useScrollLock, and useInertOthers rather than wrapping <dialog>.showModal():
- Unified overlay stacking:
showModal()promotes<dialog>into the browser top layer, which paints and stacks separately fromGhostPortaloverlays portaled intodocument.body. A non-modalGhostPopover,GhostSelect, orGhostDropdownMenuportaled tobodywould render behind a top-layer<dialog>unless every overlay switched to Popover API top-layer promotion and manual z-order re-promotion. - Unified dismissal policy:
showModal()interceptsEscapethrough the nativecancelevent with browser-specific quirks (such as immediate close without fulluseDismissableLayerstack coordination across IME composition or nested non-dialog layers). - Predictable SSR and transitions:
<dialog open>renders non-modally on the server and cannot smoothly hydrate intoshowModal()without visual and backdrop jumps, and exit animations on top-layer<dialog>require@starting-style/allow-discreteworkarounds before.close(). - Consistent focus policies:
showModal()enforces browser-specific initial focus and restore behavior that diverges fromuseFocusScope'sdata-autofocus,autoFocus,restoreFocus, and nested-trap rules.
Alternatives considered
aria-modal="true" without inert on siblings. Rejected. Leaves background controls reachable via pointer, programmatic/browser-chrome focus, and older or mobile screen readers that do not fully prune background subtrees from aria-modal alone.
Native <dialog> with showModal(). Rejected for the stacking, dismissal, SSR hydration, transition, and focus-policy reasons above. Revisit only if all floating GhostVue overlays move to top-layer popover promotion and browser top-layer exit transition support becomes uniform across targets.
Exporting useScrollLock and useInertOthers immediately. Rejected for PR 1. Keeping both primitives internal until GhostDialog lands avoids locking in low-level signatures before the first consumer component exercises them.
Consequences
@ghostvue/vuegains internaluse-scroll-lock.ts,use-inert-others.ts, and shareddismissable-layer-stack.ts.useFocusScopeanduseDismissableLayercoordinateTabacross portaled child layers without changing any public API.GhostDialog(PR 2) will compose these primitives withGhostPortalandaria-modal="true".
Compatibility implications
None for public APIs: no new exports, manifests, or runtime dependencies are added in PR 1. SSR renders remain pure with zero window/document access during setup. Like layerStack and trappedStack, scroll-lock and inert ref-counting assume a single runtime copy of @ghostvue/vue per document.