Overlay lifecycle
Dialog, AlertDialog, Drawer, MobileNav, CommandPalette, Popover, Tooltip, HoverCard, Menu and the Select, Combobox and date-picker popups are native top-layer elements. Modal overlays are a <dialog> opened with showModal(). Everything anchored to a trigger is a [popover] element placed with CSS anchor positioning. The browser provides the top layer, the inert page behind a modal, Escape, focus return and light dismiss; var-ui adds the recipes, the onOpenChange contract and the reasons below.
The attributes, selectors and variables on this page are public API (same semver rules as class names). The recipes use exactly these hooks, so you can target them in your own CSS.
The two lifecycles
| Modal overlays | Anchored overlays | |
|---|---|---|
| Components | Dialog, AlertDialog, Drawer, MobileNav, LayoutPanel (overlay), CommandPalette | Popover, Tooltip, HoverCard, Menu, Select, Combobox, MultiSelector, Tokenizer, date pickers, ToastRegion |
| Element | <dialog>, the popup itself | <div popover> |
| Opened with | showModal() | showPopover() (and the popovertarget invoker where a plain button opens it) |
| Open state | [open] and :modal | :popover-open |
| Backdrop | ::backdrop of the popup (colour: --var-ui-dialog-overlaybackground) | none |
| Dismissal | Escape, backdrop press | Escape, outside press (light dismiss), focus leaving for hover overlays |
| Position | centred, or slid in from data-placement / data-side | anchored to the trigger (position-anchor, data-placement) |
There is no portal, no layer provider and no z-index: popups render in place in the DOM and the top layer orders them by insertion. Themes and token scopes still reach them because custom properties inherit from the DOM parent. See Plain CSS for what rendering in place means for forms and events.
State attributes
| Attribute | Meaning |
|---|---|
data-open | On the popup while the overlay is requested open |
data-closed | On the popup while it is requested closed (children stay mounted until the exit transition has finished) |
data-placement | Requested placement on anchored popups (top, bottom start, end, …) or drawers (start, end, bottom) |
data-open and data-closed mirror the requested state; they are written from state, not for animation. Animate from the native selectors instead ([open], :popover-open).
Enter and exit
Enter and exit are plain CSS: the closed look is the base rule, the open look sits under the native open selector, and @starting-style provides the “from” state of the enter. The transition list includes overlay and display with allow-discrete, so the element stays rendered in the top layer while it animates out after close() or hidePopover().
.var-ui-dialog__popup {
opacity: 0;
transition:
opacity 150ms,
overlay 150ms allow-discrete,
display 150ms allow-discrete;
}
.var-ui-dialog__popup[open] {
opacity: 1;
}
@starting-style {
.var-ui-dialog__popup[open] {
opacity: 0;
}
}Anchored popups use the same shape with :popover-open. Durations and easings are the --var-ui-duration-* and --var-ui-easing-* tokens, so changing a token changes every overlay. prefers-reduced-motion: reduce sets transition: none, so the popup appears and disappears at once.
React keeps the popup’s children mounted until the exit has finished (it waits for the element’s running animations), then unmounts them, so heavy bodies are not mounted while closed. Astro renders the markup once and the platform does the rest.
Scroll lock
A global rule locks page scroll while any modal dialog is open: html:has(dialog:modal) { overflow: hidden }, with html { scrollbar-gutter: stable } so the page does not shift. Dialog popups also set overscroll-behavior: contain. There is no hook to call. Select, Menu and other popovers do not lock scroll.
Anchor positioning
Anchored popups name their trigger with anchor-name and place themselves with position-anchor, position-area and position-try-fallbacks, so the browser flips them near the viewport edge. These custom properties tune them:
| Variable | Meaning |
|---|---|
--var-ui-popup-offset | Gap between the trigger and the popup (falls back to space-xs) |
--var-ui-popup-max-height | Maximum height (falls back to the viewport minus a margin) |
--var-ui-popup-max-width | Maximum width (falls back to the viewport minus a margin) |
data-placement holds the requested placement; the browser may flip it. Select, Combobox and MultiSelector popups match the trigger width with anchor-size(width).
Change event details
Compound Dialog, AlertDialog, Drawer, Popover, Tooltip, HoverCard, Menu and CommandPalette call:
onOpenChange?: (open: boolean, details: OverlayChangeEventDetails) => void;type OverlayOpenChangeReason =
| 'trigger-press'
| 'close-press'
| 'escape-key'
| 'outside-press'
| 'imperative'
| 'hover'
| 'focus';
type OverlayChangeEventDetails = {
reason: OverlayOpenChangeReason;
event: Event | null;
cancel: () => void;
isCanceled: boolean;
};Every change has a reason, read from the native signal that caused it:
| Reason | Cause |
|---|---|
trigger-press | The trigger was pressed (including a popovertarget invoker) |
close-press | A Close part, or <form method="dialog">, was activated inside the popup |
escape-key | Escape |
outside-press | A press on the dialog backdrop, or outside a popover (light dismiss) |
hover | Hover intent opened or closed a Tooltip or HoverCard |
focus | Focus entered or left the trigger or popup |
imperative | Your own code (setOpen, a controlled open change), requestClose(), a back gesture, or any close with no observed gesture — including a menu item activating |
cancel() vetoes the change. For a controlled overlay (you pass open), the overlay stays in whatever state open says, so ignore the request in the parent instead of calling cancel() and then setting state. Chromium lets a page cancel Escape on a modal dialog once per user activation; after that the dialog closes natively and var-ui re-opens it when open is still true, so the state is right but the focus return and a close event happen in between.
<Dialog.Root
onOpenChange={(open, details) => {
if (!open && details.reason === 'outside-press') {
details.cancel();
return;
}
setOpen(open);
}}
>
…
</Dialog.Root>createOverlayChangeDetails({ reason, event }) builds a details object with cancel() / isCanceled, for code that wraps an overlay and forwards its changes.
Focus
Focus is native. showModal() moves focus to the first autofocus or focusable descendant and returns it to the previously focused element on close. Popovers do not move focus on open (add autofocus inside to opt in) and return it on hide. Select, Combobox and Menu popups are driven by React Aria, which handles their roving focus.
Related
- Getting started: providers, direction and how themed overlays render in place
- Plain CSS: styling overlays with native selectors, and the form and event caveats of rendering in place
- CSS variables: design tokens and component variables
- Dialog, Popover, Tooltip, Menu