Interaction and DOM utilities
The interaction utilities provide shared browser primitives for the UI machines. They turn low-level DOM events, focus state, element bounds, scroll state, and accessibility announcements into consistent behavior that components can reuse. The utilities are the common browser layer beneath focus handling, overlays, menus, dialogs, popovers, lists, and other interactive features.
System role
The utilities connect component state to the browser without owning a component's open, closed, or selected state. A machine supplies the DOM nodes and policy. The utilities observe the browser, perform a focused DOM action, and return cleanup or event details so the machine can decide what happens next.
Primitive relationships
Each primitive covers one browser concern, but the concerns work together around the same interaction boundary. Focus and outside interaction define where the user is allowed to act. Positioning and scrolling keep that boundary usable. Live regions expose important changes to assistive technology.
The main relationship between these concerns is:
flowchart LR
S[Machine state] --> F[Focus and tab order]
S --> O[Outside interaction]
S --> P[Floating placement]
S --> R[Scroll control]
S --> A[Live announcement]
D[DOM nodes and browser events] --> F
D --> O
D --> P
D --> R
F --> B[Accessible interaction boundary]
O --> B
P --> B
R --> B
A --> U[Assistive technology]
References: packages/…/index.ts• L1-34 packages/…/index.ts• L1-44 packages/…/index.ts• L116-161 packages/…/get-placement.ts• L211-232
Accessibility and focus
Focus utilities keep keyboard movement, focus visibility, and assistive technology context aligned with the visual UI. They provide the small rules that larger machines need when they move focus into a new surface, keep focus inside it, or hide unrelated content from the accessibility tree.
Tabbable focus
The DOM query layer defines focusable and tabbable elements as a shared view of the document. It can account for disabled or hidden elements, radio groups, iframes, and optional shadow-root traversal. Initial focus uses an explicit choice first, then an autofocus marker, then the first suitable tabbable element, and finally the content root as a safe fallback.
Focus order is a contract between the machine and the page. A component that renders content in a portal must give the focus utility the right root and trigger so that Tab and Shift+Tab cross the boundary in the intended order.
References: packages/…/tabbable.ts• L1-9 packages/…/tabbable.ts• L130-160 packages/…/tabbable.ts• L173-245 packages/…/initial-focus.ts• L4-34 packages/…/proxy-tab-focus.ts• L20-35
Focus visibility
The focus-visible utility tracks the interaction modality that caused focus: keyboard, pointer, or virtual input such as a screen reader. Components can use this signal to show a focus indicator for keyboard and virtual focus without treating every programmatic or pointer focus as keyboard focus.
Tracking is scoped to a window or root and is shared by its listeners. Cleanup removes the subscriber and restores the browser focus hook when the last tracked window is released. This lets dialogs and other surfaces move focus without creating false focus rings.
References: packages/…/index.ts• L58-84 packages/…/index.ts• L224-264 packages/…/index.ts• L311-390
Focus containment and exposure
Focus trapping keeps keyboard focus inside one or more active elements and returns focus when the trap ends. The public helper resolves elements after a frame, activates the trap with safe defaults, and returns an idempotent cleanup path for a component lifecycle.
Modal surfaces also need to remove unrelated content from the accessibility tree. The aria-hidden utility applies that boundary to the rest of the document and restores it during cleanup. These two actions solve different problems: focus containment controls keyboard movement, while aria hiding controls what assistive technology can perceive.
References: packages/…/index.ts• L5-41 packages/…/index.ts• L8-30
Outside interaction boundaries
Outside interaction utilities turn pointer and focus movement into preventable signals for dismissible UI. They define “outside” using DOM containment plus related controlled or portalled content, instead of relying only on whether an event target is a child of the visible element.
Interaction detection
The interact-outside utility watches pointer-down and focus-in events for an element. It reports the original event, the target, whether the target is focusable, and whether the pointer event is a context-menu action. It can defer the check, ignore selected targets, and follow expanded controlled elements such as a portalled menu or listbox.
The detector also treats shadow-root paths, scrollbars, and nested browsing contexts as part of the boundary rules. This matters because a user can interact with content that is visually or logically attached to a surface while that content is not a DOM descendant of the surface.
References: packages/…/index.ts• L16-61 packages/…/index.ts• L116-161 packages/…/index.ts• L169-255 packages/…/frame-utils.ts• L1-54
Dismissable layers
Dismissable layers add policy around outside interaction. A layer stack records which surface is on top, which surface blocks pointer input, and which nested surfaces or branches remain inside the active boundary. Only the top layer normally responds to Escape, and a parent waits for nested layers to close before it dismisses.
The layer API makes outside events cancelable. A component can prevent dismissal when the event is part of a valid handoff, while the stack still keeps ordering, pointer blocking, nested-layer metadata, and cleanup consistent.
References: packages/…/dismissable-layer.ts• L17-72 packages/…/dismissable-layer.ts• L74-151 packages/…/layer-stack.ts• L27-99 packages/…/layer-stack.ts• L104-175
Positioning and geometry
Positioning utilities place a floating element relative to an anchor and keep it usable as the layout changes. Geometry utilities provide shared bounds and shape operations for components that need to compare, constrain, align, or measure DOM regions.
Floating placement
The popper utility computes a floating element's position from a real or virtual anchor. It can keep the element inside an overflow boundary, change its side when space is limited, slide it along an axis, match the anchor width, fit the available viewport space, position an arrow, and hide the element when its anchor is detached.
Placement can update when ancestors scroll or resize, when the element changes size, or when the layout shifts. The result is exposed through placement data and CSS values so a component can use the same placement contract for its panel, arrow, size limits, and transform origin.
References: packages/…/types.ts• L3-7 packages/…/types.ts• L19-128 packages/…/get-placement.ts• L10-24 packages/…/get-placement.ts• L283-355 packages/…/get-anchor.ts• L5-42
Rectangle operations
The rectangle utility provides a common geometry model for browser regions. It converts elements and other sources into rectangles, then supports operations such as intersection, containment, distance, alignment, constraint, and union. This keeps geometry decisions separate from a component's interaction state.
Use the collections, geometry, and localization page for the wider relationship between geometry helpers and the data or locale helpers used by component machines. This page focuses on the browser boundary that supplies measurements to those systems.
References: packages/…/index.ts• L1-21 packages/…/from-element.ts• L14-56 packages/…/middleware.ts• L86-97
Scrolling and viewport control
Scrolling utilities make movement predictable inside nested scroll areas and modal surfaces. They preserve the relationship between focus, visible content, scroll padding, and the viewport while avoiding changes to scroll containers that do not apply to the current element.
Element scrolling
The DOM query scroll helpers move an element into a named scroll root only when that root is an overflow element with scrollable content. A separate helper computes a target position from the root and element bounds while honoring scroll padding and scroll margin. Components can also read the current scroll position from either an element or a window.
The scroll-snap utility builds snap positions from child alignment and scroll padding. It normalizes transformed layouts and handles both left-to-right and right-to-left horizontal scrolling, so a component can select the correct point without duplicating browser-specific coordinate rules.
References: packages/…/scroll.ts• L4-58 packages/…/index.ts• L25-55 packages/…/index.ts• L65-75 packages/…/index.ts• L158-220
Body scroll locking
The remove-scroll utility temporarily locks the document scroll container for modal content. It preserves the page position, compensates for the missing scrollbar where needed, and uses a fixed-body strategy for iOS. Locks are counted per document so nested surfaces can release their own lock without unlocking a still-active surface.
References: packages/…/index.ts• L25-31 packages/…/index.ts• L32-96 packages/…/index.ts• L98-121
Announcements and DOM access
The remaining primitives expose browser state in forms that machines can use without repeating platform checks. They cover screen-reader announcements, DOM scope and ownership, safe queries, and the document or window associated with a node.
Live announcements
The live-region utility announces text through an ARIA live region. It supports polite and assertive levels, an optional root, delayed updates, and a debug view for development. Each announcement replaces the prior region and writes the message after the configured delay so assistive technology receives a fresh change.
Live announcements are for information that is not already clear from the focused DOM. The owning machine decides what message to send and when to destroy the announcer; the utility only manages the browser-facing announcement surface.
References: packages/…/index.ts• L1-7 packages/…/index.ts• L16-21 packages/…/index.ts• L38-89
DOM querying and scope
The DOM query package is the shared browser vocabulary for the machines. It exports safe selector helpers, node and document checks, active-element lookup, containment across shadow roots, ownership checks, event helpers, timing helpers, and the focus and scroll primitives described above.
Machines can create a scope that resolves the document, window, active element, and IDs from a chosen document or shadow root. This lets the same interaction rules work in the main document, a shadow tree, or another supported browser root without changing the machine's state model.
References: packages/…/index.ts• L1-34 packages/…/query.ts• L1-39 packages/…/node.ts• L69-143 packages/…/scope.ts• L5-24
Related subsystem pages
These utilities are the shared browser layer. The linked pages explain the higher-level behavior that combines them into user-facing interaction systems:
- Focus, dismissal, and positioning explains how machines coordinate focus return, outside interaction, and floating placement.
- Overlay and disclosure machines explains the state and lifecycle rules for dialogs, drawers, popovers, menus, and related surfaces.
- Collections, geometry, and localization explains reusable item data, geometry support, and locale-aware behavior around these DOM primitives.
Boundary ownership
The component machine owns policy such as whether an outside event dismisses a surface, which element receives initial focus, and which message is announced. The utilities own browser observation and cleanup. Keeping this boundary clear allows several component types to share the same primitives while preserving their own state rules.
References: packages/…/index.ts• L264-282 packages/…/dismissable-layer.ts• L154-177 packages/…/index.ts• L10-41 packages/…/index.ts• L84-97
Change checklist
When these files or sections are changed, remember to consider these:
- When focus or tabbable rules change, check portalled content, shadow-root traversal, iframe traversal, and the trigger-to-content Tab path together.
- When outside detection changes, check
aria-controlsrelationships, dismissable branches, persistent elements, nested layer ordering, and pointer-blocking behavior together. - When placement output changes, check the CSS contract for position, arrow placement, size limits, transform origin, detached anchors, and cleanup restoration.
- When scroll behavior changes, check nested scroll roots, scroll padding and margin, transformed layouts, right-to-left horizontal scrolling, and modal body-lock reference counting.
- When live-region behavior changes, check the selected ARIA level, announcement delay, root document, replacement of an older message, and destroy cleanup.
- When DOM scope or node ownership changes, check main-document, shadow-root, iframe, and cross-window event paths.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Portalled content is not linked to its trigger or included as a branch | A menu, listbox, or nested surface can look outside and dismiss its owner | Preserve the controlled-element relationship or register the content as a dismissable branch |
| A focus root has no valid tabbable element and no usable fallback | Initial focus or Tab boundary behavior can leave focus in the wrong place | Provide the correct root and an explicit initial focus choice when the surface has unusual focus rules |
| A floating element has no current anchor or its anchor is detached | Placement is skipped or the floating element becomes hidden when configured to follow a detached anchor | Resolve the anchor and floating node at the point of update and handle the unplaced state |
| A modal scroll lock cleanup is skipped or released more than once | The page can remain locked or unlock while another modal still needs the lock | Keep each lock handle paired with one release; the utility counts active locks per document |
| A live announcement uses the wrong document or root | The message is not exposed in the active browsing context | Create the live region with the document and root used by the owning surface |
| A query or focus calculation ignores a shadow root or nested frame | Keyboard order, active-element checks, or outside detection can disagree with what the user sees | Use the relevant root scope and enable shadow or frame traversal where the component supports it |
Key files
| File | Why to read it |
|---|---|
| packages/…/index.ts | Public map of the shared browser primitives |
| packages/…/tabbable.ts | Focusable and tabbable order used by focus management |
| packages/…/initial-focus.ts | Initial focus and Tab boundary policy |
| packages/…/node.ts | Document, window, active-element, containment, and shadow-root rules |
| packages/…/index.ts | Pointer and focus outside boundary behavior |
| packages/…/dismissable-layer.ts | Layer-level dismissal policy and cleanup |
| packages/…/layer-stack.ts | Nested layer order, branches, and pointer-blocking state |
| packages/…/index.ts | Keyboard, pointer, and virtual focus modality tracking |
| packages/…/index.ts | Public focus-trap lifecycle |
| packages/…/get-placement.ts | Floating placement and update lifecycle |
| packages/…/types.ts | Positioning contract for anchors, boundaries, sizing, and updates |
| packages/…/index.ts | Shared rectangle and geometry operations |
| packages/…/scroll.ts | Element visibility and scroll-root helpers |
| packages/…/index.ts | Snap-point calculation across direction and transforms |
| packages/…/index.ts | Modal document scroll locking |
| packages/…/index.ts | Screen-reader announcement lifecycle |
| packages/…/index.ts | Accessibility-tree boundary for modal content |