Skip to content

Guides

Overlay lifecycle

How Dialog, Drawer, Popover, Tooltip, HoverCard, Menu and the select popups open and close on native dialog and popover elements, which attributes they expose, and the onOpenChange close reasons.

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 overlaysAnchored overlays
ComponentsDialog, AlertDialog, Drawer, MobileNav, LayoutPanel (overlay), CommandPalettePopover, Tooltip, HoverCard, Menu, Select, Combobox, MultiSelector, Tokenizer, date pickers, ToastRegion
Element<dialog>, the popup itself<div popover>
Opened withshowModal()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
DismissalEscape, backdrop pressEscape, outside press (light dismiss), focus leaving for hover overlays
Positioncentred, or slid in from data-placement / data-sideanchored 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

AttributeMeaning
data-openOn the popup while the overlay is requested open
data-closedOn the popup while it is requested closed (children stay mounted until the exit transition has finished)
data-placementRequested 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().

CSS
.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:

VariableMeaning
--var-ui-popup-offsetGap between the trigger and the popup (falls back to space-xs)
--var-ui-popup-max-heightMaximum height (falls back to the viewport minus a margin)
--var-ui-popup-max-widthMaximum 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:

TS
onOpenChange?: (open: boolean, details: OverlayChangeEventDetails) => void;
TS
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:

ReasonCause
trigger-pressThe trigger was pressed (including a popovertarget invoker)
close-pressA Close part, or <form method="dialog">, was activated inside the popup
escape-keyEscape
outside-pressA press on the dialog backdrop, or outside a popover (light dismiss)
hoverHover intent opened or closed a Tooltip or HoverCard
focusFocus entered or left the trigger or popup
imperativeYour 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.

TSX
<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.