Focus, dismissal, and positioning utilities
These utilities provide the browser behavior that makes overlays safe and usable. They keep keyboard focus inside active surfaces, detect when a user acts outside a surface, hide unrelated content from assistive technology, prevent background scroll, and place floating content near its anchor. They also account for portals, nested layers, shadow roots, frames, and changing viewport bounds. See Interaction utilities, Overlay and disclosure machines, and Framework integrations for the related system view.
Utility boundary
Overlay support roles
The utilities separate overlay behavior into related responsibilities. Focus management controls where the keyboard can move. Dismissal management decides when an overlay should close. Background management limits what users can see or scroll behind a modal surface. Placement management keeps floating content aligned with its reference element.
These responsibilities work together but do not decide whether an overlay is open. Overlay and disclosure machines own that state and call the utilities as effects of their state changes.
| Concern | User-visible result |
|---|---|
| Focus trap | Focus enters the surface, stays within its focus order, and can return to the prior control. |
| Outside interaction | Pointer, focus, and Escape actions can request dismissal. |
| Background suppression | Content outside the active surface is hidden or inert for assistive technology and input. |
| Scroll lock | The page or app-shell scroller stops moving without a layout jump. |
| Placement | Floating content follows its anchor and adapts to available space. |
| Portal coordination | Content rendered elsewhere in the DOM can remain part of the same interaction boundary. |
Overlay lifecycle
An overlay normally enables these utilities when it becomes active and releases them when it closes. The machine remains the source of truth, while each utility returns cleanup work so focus, event listeners, DOM attributes, styles, and scroll state do not remain after the overlay ends.
The following flow shows the relationship between overlay state and browser coordination.
flowchart TD
A[Overlay becomes active] --> B[Register interaction boundary]
B --> C[Manage focus]
B --> D[Suppress background]
B --> E[Lock scroll when needed]
B --> F[Place floating content]
C --> G[Outside or Escape event]
G --> H[Machine requests close]
H --> I[Run cleanup and restore browser state]
References: packages/…/dismissable-layer.ts• L74-151 packages/…/focus-trap.ts• L606-693 packages/…/index.ts• L98-121
Focus management
Focus traps
FocusTrap keeps keyboard focus inside one or more active containers. On activation it selects an initial or fallback target, watches focus and keyboard navigation, and wraps movement across the available tabbable elements. On deactivation it can return focus to the element that was active before the trap.
The trap supports nested active surfaces through a shared stack. When a new trap becomes active, the prior trap pauses. Closing the top trap lets the prior trap resume. This prevents two overlays from competing for the same focus events.
Portalled controls can remain inside the trap when an expanded controller in the trap points to them with aria-controls. Consumers can also provide persistent elements for content that cannot be found through that relationship.
References: packages/…/focus-trap.ts• L31-62 packages/…/focus-trap.ts• L256-291 packages/…/focus-trap.ts• L298-455 packages/…/types.ts• L103-175 packages/…/types.ts• L203-246
Focus targets
Initial focus is a policy choice, not only a DOM query. The focus helpers allow a component to choose a specific target, honor an explicit autofocus marker, skip selected controls, or use the content root when no tabbable control is suitable. A component can also opt out of initial focus.
The trap requires a usable focus target while active unless the caller explicitly disables initial focus. If the chosen target is missing or disconnected, it falls back to a valid target when one exists. Animated surfaces can delay activation or focus return until their content is ready.
References: packages/…/initial-focus.ts• L4-33 packages/…/focus-trap.ts• L536-575 packages/…/focus-trap.ts• L606-639 packages/…/focus-trap.ts• L643-692
Focus visibility
The focus-visible utility tracks interaction modality so a focus ring reflects how focus was reached. Keyboard and assistive-technology focus is treated as visible, while pointer-driven focus is not. Programmatic focus during autofocus or focus restoration keeps the current modality instead of creating an unwanted ring.
This state is global per window and can be observed from a chosen document or shadow-root context. It also handles labels, text inputs, virtual clicks, window blur, and browser focus events so components do not need to infer modality from one event type.
References: packages/…/index.ts• L60-90 packages/…/index.ts• L104-179 packages/…/index.ts• L191-264 packages/…/index.ts• L311-389
Dismissal boundaries
Outside interaction
trackInteractOutside turns pointer-down and focus changes outside a node into cancelable custom events. It ignores the node itself, related controlled content, configured exclusions, and pointer positions that fall inside the node even when event targets are unreliable. This lets a component distinguish a real outside action from a browser or layout edge case.
The utility watches the relevant document and can also observe parent and child frame documents. Touch input waits for the later click before dispatching the outside event. A deferred mode moves detection to a later browser turn so a newly committed surface can establish its boundary before the event is handled.
References: packages/…/index.ts• L16-42 packages/…/index.ts• L116-161 packages/…/index.ts• L163-255 packages/…/frame-utils.ts• L1-54
Dismissable layers
trackDismissableElement adds an active surface to a layer stack and connects outside interaction, focus outside, and Escape to one dismissal request. Only the topmost layer handles Escape, and a lower layer does not dismiss when a higher pointer-blocking layer owns the event. Nested layers and registered branches can therefore behave as one coordinated interaction tree.
The layer can block pointer events outside itself, keep selected persistent elements interactive, and exclude nested or related containers from dismissal. Cleanup removes the layer, restores pointer behavior for the remaining stack, and unregisters all event handlers.
References: packages/…/dismissable-layer.ts• L17-72 packages/…/dismissable-layer.ts• L74-151 packages/…/dismissable-layer.ts• L154-177
Background suppression
Aria hiding and inertness
The background suppression utilities keep an active surface available to assistive technology while reducing access to unrelated content. They walk the document outside the target and apply either aria-hidden or inert, with support for browsers that do not provide native inert behavior.
Suppression is reference-counted and restores attributes only when the last active suppression ends. Existing author-set state is preserved. Controlled content associated with the target can remain available, which is important when an overlay or menu is rendered through a portal.
References: packages/…/aria-hidden.ts• L11-53 packages/…/walk-tree-outside.ts• L43-69 packages/…/walk-tree-outside.ts• L70-141
Scroll locking
preventBodyScroll stops the document's active scroll container while a modal surface is open. It accounts for scrollbar width, stable scrollbar gutters, right-to-left layouts, and iOS behavior so the page does not shift or lose its scroll position.
Locks are counted per document. A nested dialog, drawer, or framework remount can acquire its own lock and release it independently. The underlying styles and lock marker are restored only after the final owner releases the lock.
References: packages/…/index.ts• L12-30 packages/…/index.ts• L32-96 packages/…/index.ts• L98-121
Floating placement
Popper placement
The popper utility positions a floating element from an element or virtual anchor. It starts with a requested side and alignment, then adapts the result to the available boundary by applying an offset, flipping, sliding, sizing, arrow placement, and optional detached-reference hiding.
Placement can use absolute or fixed coordinates. The utility can follow scroll, resize, element-size, and layout changes. It exposes the computed coordinates and size values through callbacks and CSS variables so a framework adapter can apply styles or control exit animations.
References: packages/…/get-placement.ts• L10-24 packages/…/get-placement.ts• L84-159 packages/…/get-placement.ts• L268-373 packages/…/types.ts• L19-128
Placement styles
getPlacementStyles supplies the style contract for a floating element and its arrow. The contract places the element at computed coordinates, keeps it isolated above nearby content, and can match the anchor width or fit the available viewport size.
Before placement is known, the floating element is moved out of view and does not accept pointer input. When the anchor is detached and hiding is enabled, placement coordination also removes the element from visibility and pointer interaction.
References: packages/…/get-styles.ts• L16-57 packages/…/get-placement.ts• L325-355
Portal and browser coordination
Portal boundaries
Portals change where content is mounted without changing which overlay owns it. The focus, outside-interaction, and suppression utilities preserve that ownership through expanded controller relationships, persistent elements, and layer branches. This keeps a portalled menu, listbox, or dialog from being mistaken for unrelated page content.
The framework adapters mount content into a selected container or document body. They can disable portalling for server rendering or local layouts, and they can resolve a document from a shadow root. Consumers must keep the semantic links and interaction registrations aligned with the mounted content.
References: packages/…/portal.tsx• L7-22 packages/…/portal.tsx• L11-37 packages/…/portal.ts• L1-21 packages/…/types.ts• L203-246 packages/…/index.ts• L31-42
Viewport coordination
Browser viewport state can differ from the layout viewport, especially on mobile when browser chrome or the on-screen keyboard changes the visual viewport. trackVisualViewport gives components a small event source for the current visible width and height so overlay layout can react without owning browser-specific rules.
This helper complements popper auto-updates and scroll locking. It reports the current visual size, while the placement utility decides how the floating surface fits and the scroll utility decides which document styles must change.
References: packages/…/visual-viewport.ts• L3-21 packages/…/types.ts• L81-102 packages/…/index.ts• L61-87
Change checklist
When these files or sections are changed, remember to consider these:
- Keep focus-trap portal discovery, outside-interaction controlled-element checks, and aria suppression controlled-element checks aligned when the
aria-controlsoraria-expandedcontract changes. - Preserve the topmost-layer rule when adding a new dismissal source or pointer-blocking behavior.
- Keep cleanup idempotent for focus traps, dismissable layers, scroll locks, and placement observers because nested overlays and framework remounts can release the same behavior more than once.
- When changing portal mount rules, check the document or shadow-root used by focus, outside events, suppression, and placement.
- When changing placement CSS variables or style ownership, update both the popper producer and the framework or component consumer that renders the floating surface.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| An active focus trap has no tabbable node and no fallback focus target. | Activation fails because the trap cannot keep focus in a usable place. | Provide a valid initial or fallback target, or explicitly opt out of initial focus when the component owns focus another way. |
| A portalled control has no recognized controller or persistent-element relationship. | Focus or outside interaction can treat the portalled content as outside the active surface. | Keep the expanded controller relationship or register the content as persistent or as a dismissable branch. |
| A lower dismissable layer receives an outside or Escape event while a higher layer is active. | The wrong surface closes or a modal boundary is bypassed. | Let the layer stack decide whether the layer is topmost or below a pointer-blocking layer. |
| A scroll lock cleanup runs while another caller still holds a lock. | Background content becomes scrollable behind an active modal surface. | Use the per-document lock release returned by each acquisition. |
| Placement runs after its anchor or floating node is replaced without observer cleanup. | Stale updates can write coordinates or listeners for detached content. | Release the placement handle when the node changes and let the next handle resolve the new nodes. |
| A floating reference leaves its clipping boundary. | The surface remains visible in a place that no longer represents its anchor. | Enable detached-reference hiding when the component must disappear with its reference. |
Key files
| File | Why to read it |
|---|---|
| packages/…/focus-trap.ts | Main focus boundary lifecycle, nested trap coordination, focus return, and portalled controlled-element handling. |
| packages/…/types.ts | Focus policy choices such as initial focus, fallback focus, Escape, outside clicks, and persistent elements. |
| packages/…/index.ts | Definition of outside pointer and focus events across DOM, shadow-root, touch, and frame cases. |
| packages/…/dismissable-layer.ts | Layer stack contract that connects outside events and Escape to dismissal. |
| packages/…/aria-hidden.ts | Public background suppression choices for aria-hidden, inert, and feature fallback. |
| packages/…/index.ts | Document scroll-lock ownership and browser-specific restoration behavior. |
| packages/…/get-placement.ts | Floating placement lifecycle and adaptation to boundaries, size, and changing anchors. |
| packages/…/get-styles.ts | CSS contract for floating content and arrows. |
| packages/…/visual-viewport.ts | Small browser coordination surface for visual viewport changes. |
| packages/…/portal.tsx | React portal mount behavior and document or shadow-root selection. |
| packages/…/portal.ts | Svelte portal action lifecycle and cleanup behavior. |