Introducing a new way to review your PRs.

Read the announcement

chakra-ui / zag

Build your design system in React, Solid, Vue, Svelte or Vanilla. Powered by finite state machines

View on GitHub

19 documents · Updated Sep 21, 2026

docs / selection-and-navigation-machines

Selection and navigation machines

Selection and navigation machines coordinate how people move through related items and how the system records the active or selected items. They turn ordered or hierarchical data into stable focus, selection, expansion, paging, and navigation behavior across disclosure panels, option pickers, menus, trees, carousels, steppers, and paged data.

This page complements component machines. See form and input machines for controls that submit or validate user input, and collections, geometry, and localization for the shared data and layout services used by these machines.

System model

Each machine keeps a small piece of user-facing state and maps input events to a next value. A collection supplies the valid items and their order. The machine then uses that order to move focus, resolve keyboard targets, apply selection rules, and report changes to the host application.

The common relationship is:

flowchart LR
    D[Items or tree nodes] --> C[Collection or item set]
    C --> F[Focus or highlight]
    F --> S[Selected, expanded, open, or current value]
    S --> R[Rendered state and change callbacks]
    U[Keyboard, pointer, input, or timer event] --> F
    U --> S

The machines separate a temporary navigation target from the committed value where that distinction matters. A highlighted option can move while a listbox or combobox waits for selection. A focused tab can move without changing the active tab in manual mode. Accordion and tree machines also keep expansion separate from selection.

Collection model

Collections give item-based machines one shared view of value, order, labels, disabled state, and search. This keeps navigation rules independent from the data source and lets the same behavior work with normal, grouped, virtualized, or hierarchical data.

List collections

The fully qualified collection source packages/…/list-collection.ts defines an ordered list of items. A list collection maps each item to a stable value and label, skips disabled items during first, last, next, and previous navigation, supports type-ahead search, and can return ranges for extended selection.

Listbox, select, and combobox machines consume this model. They use the same item order for highlighting and selection, while each machine decides when the highlight becomes a committed value.

References: packages/…/list-collection.ts• L21-29 packages/…/list-collection.ts• L123-152 packages/…/list-collection.ts• L238-273 packages/…/list-collection.ts• L320-353

Tree collections

The fully qualified tree collection source packages/…/tree-collection.ts represents parent and child relationships. It provides visible traversal, parent and sibling lookup, branch detection, value paths, and descendant lookup so a tree view can move through the current visible tree instead of a flat list.

Expansion changes which nodes are visible. The tree machine uses that visible set for keyboard movement, type-ahead, range selection, and optional virtual scrolling.

References: packages/…/tree-collection.ts• L29-72 packages/…/tree-collection.ts• L221-273 packages/…/tree-collection.ts• L275-329 packages/…/tree-collection.ts• L375-400

Disclosure and view selection

Disclosure and view machines expose a small set of named items. Their main job is to keep the active item and its content in sync while preserving predictable keyboard movement.

Accordion items

The accordion machine controls which panels are expanded. It supports one expanded item by default, optional multiple expansion, and optional collapse of the active item. Focus moves among triggers in first, last, next, and previous order.

Expansion is a value operation rather than a focus operation. A trigger can receive focus without changing its panel, and a click changes the value only when the current configuration permits that change.

References: packages/…/accordion.machine.ts• L8-39 packages/…/accordion.machine.ts• L52-91 packages/…/accordion.machine.ts• L93-141

Tabs

The tabs machine manages one active tab and one focused tab across a tab list and its content. It supports horizontal or vertical arrow movement, optional focus looping, and automatic or manual activation.

Automatic activation commits the tab as focus moves. Manual activation keeps focus movement separate until the user clicks or activates the tab. When tab triggers are links, the machine can also pass the selected tab to navigation behavior.

References: packages/…/tabs.machine.ts• L10-24 packages/…/tabs.machine.ts• L30-53 packages/…/tabs.machine.ts• L94-157 packages/…/tabs.machine.ts• L159-219

Option selection

Option machines use a highlight as the navigation target and a value as the committed selection. They share first, last, next, previous, and type-ahead behavior, but they differ in how they open, accept text, and close after selection.

The menu machine manages an open menu whose active item is highlighted. It supports keyboard movement, Home and End navigation, type-ahead, selection callbacks, and optional close-on-select behavior.

Menus can form a parent and submenu tree. Opening a submenu transfers the active context to the child, while closing it returns focus and the prior highlight to the parent. Pointer movement uses a short delay and a protected path so movement toward a submenu does not close it too early.

References: packages/…/menu.machine.ts• L57-77 packages/…/menu.machine.ts• L80-120 packages/…/menu.machine.ts• L421-551 packages/…/menu.machine.ts• L879-950

Listbox options

The listbox machine presents a collection for direct option navigation and selection. It supports single, multiple, and extended selection, with separate highlighted and selected values. Keyboard selection can replace, toggle, or extend a range depending on the selection mode and modifier keys.

The machine keeps selected items tied to the current collection and can move a virtualized list to the highlighted index. Type-ahead changes the highlight; selection occurs through the configured pointer or keyboard action.

References: packages/…/listbox.machine.ts• L21-33 packages/…/listbox.machine.ts• L116-134 packages/…/listbox.machine.ts• L283-328 packages/…/listbox.machine.ts• L344-468

Select options

The select machine combines a closed trigger with an open list of options. It supports single or multiple values, optional deselection, type-ahead from the trigger, and focus return when the list closes.

Selection updates both the machine value and the associated form representation. A single select can move directly to the next or previous option from the trigger, while an open select uses the highlighted option as the next selection target.

References: packages/…/select.machine.ts• L22-37 packages/…/select.machine.ts• L111-123 packages/…/select.machine.ts• L173-425 packages/…/select.machine.ts• L600-665

Combobox options

The combobox machine adds editable input and filtering to list selection. It tracks the input text, the highlighted option, and the selected values as related but distinct state.

Typing can open the list and produce suggestions. Depending on the input and selection settings, the machine can replace, preserve, or clear the input after selection. Enter selects the highlighted option when one exists; otherwise the machine either accepts a custom value or restores the last valid selection.

References: packages/…/combobox.machine.ts• L30-55 packages/…/combobox.machine.ts• L63-166 packages/…/combobox.machine.ts• L220-384 packages/…/combobox.machine.ts• L389-631

Hierarchical navigation

Hierarchical navigation machines manage more than one active item at a time. They coordinate the current branch, visible descendants, focus return, and movement between related links or nodes.

Tree view nodes

The tree view machine manages a tree with independent expanded, selected, checked, and focused values. It supports single or multiple node selection, range extension, branch expansion, type-ahead, and inline rename flow.

Arrow keys express tree structure: left moves to a parent or collapses an expanded branch, and right expands a branch or enters its first child. Up and down move through visible nodes, so collapsed branches do not add hidden nodes to the focus path.

References: packages/…/tree-view.machine.ts• L15-30 packages/…/tree-view.machine.ts• L32-115 packages/…/tree-view.machine.ts• L118-264 packages/…/tree-view.machine.ts• L362-442

The navigation menu machine manages a site navigation bar with one active top-level value and optional content viewport. It opens content on pointer entry or click, closes it after pointer or focus leaves, and keeps the active trigger and content aligned.

Within open content, focus can move through links and return to the trigger when the menu closes. Open and close delays prevent quick pointer movement across adjacent triggers from causing unstable content changes.

References: packages/…/navigation-menu.machine.ts• L18-29 packages/…/navigation-menu.machine.ts• L31-81 packages/…/navigation-menu.machine.ts• L105-149 packages/…/navigation-menu.machine.ts• L319-363

Sequential and page navigation

Sequential and page machines represent position with a number rather than an item value. They turn next, previous, first, last, and direct navigation into bounded changes and report the resulting position to the host application.

The carousel machine maps slides to scroll positions and exposes the current page. It supports next and previous navigation, direct slide targeting, keyboard control, pointer dragging, looping, autoplay, and detection of slides in view.

Scrolling is the source of truth during user interaction. After a drag or free scroll ends, the machine chooses the closest valid snap point and updates the page. Layout or slide-count changes refresh those snap points so page navigation continues to match the rendered slides.

References: packages/…/carousel.machine.ts• L10-28 packages/…/carousel.machine.ts• L40-74 packages/…/carousel.machine.ts• L94-123 packages/…/carousel.machine.ts• L145-241 packages/…/carousel.machine.ts• L439-491

Steps

The steps machine tracks the current step in a linear or free navigation flow. It exposes progress, completion, and whether a next or previous step exists.

Backward navigation is always allowed. In a linear flow, forward navigation checks the current step unless the step is marked skippable. An invalid forward move does not change the current step and instead reports the blocked action.

References: packages/…/steps.machine.ts• L5-38 packages/…/steps.machine.ts• L40-75 packages/…/steps.machine.ts• L77-123

Pagination pages

The pagination machine maps a data count and page size to a bounded page range. It tracks the current page and page size, exposes first and last page actions, and reports page changes with the active page size.

Changing the page size can make the current page invalid. The machine detects that case and returns to the first valid page so the visible data range stays within the count.

References: packages/…/pagination.machine.ts• L4-15 packages/…/pagination.machine.ts• L21-63 packages/…/pagination.machine.ts• L65-124 packages/…/pagination.types.ts• L50-110

Change checklist

When these files or sections are changed, remember to consider these:

  • When an item value, label, order, or disabled state changes, check highlight movement, type-ahead results, selected values, and the item-to-element mapping together.
  • When listbox, select, or combobox selection rules change, check the distinction between highlighted values, committed values, displayed text, and form values.
  • When tree expansion or visibility rules change, check arrow-key movement, range selection, type-ahead, and virtual scroll indexes against the visible node set.
  • When menu or navigation-menu open behavior changes, check focus return, sibling or child menu transitions, pointer delays, and link navigation as one flow.
  • When carousel slide count or layout changes, check page snap points, the current page, drag settling, and autoplay bounds.
  • When pagination count or page size changes, check page validity and the data range consumed by the host.
  • When steps validation or count changes, check direct forward jumps, next-step guards, completion reporting, and step-index bounds.

Important failure modes

TriggerConsequenceGuard
An item value is missing, duplicated, or not kept stableHighlight, selection, and rendered item state can refer to the wrong itemTreat collection values as stable identity and keep them unique across one machine instance
A highlighted list option is removed during filteringThe active descendant can point to an item that no longer existsRebuild or update the collection and let listbox, select, or combobox clear or restore the highlight
A tree branch is collapsed while focus or selection is inside itKeyboard movement can lose the visible path or show a stale nodeReconcile expansion with the visible-node traversal before the next navigation event
A menu or navigation menu closes outside its trigger and contentFocus can escape the navigation context or return to an obsolete triggerPreserve the trigger/content relationship and use the machine's close path for outside interaction
Carousel layout or slide count changes without refreshed snap pointsPage values and scroll positions can disagreeRefresh snap points and clamp the current page after slide or layout changes
Pagination page size makes the current page exceed the new totalThe data view can request an empty or invalid rangeRecompute total pages and move to a valid page before slicing data
A step index is outside 0..countThe steps machine rejects the state with a range errorKeep the current and requested step within the declared count

Key files

FileWhy to read it
packages/…/list-collection.tsDefines ordered item identity, disabled-item traversal, labels, and type-ahead search used by option machines
packages/…/selection.tsDefines the shared single, multiple, and extended selection rules
packages/…/tree-collection.tsDefines visible tree traversal, parent and sibling movement, and branch relationships
packages/…/accordion.machine.tsShows the separation between focused triggers and expanded panel values
packages/…/tabs.machine.tsShows active-versus-focused tab behavior and automatic versus manual activation
packages/…/menu.machine.tsShows highlighted menu navigation, item selection, and parent-submenu focus flow
packages/…/listbox.machine.tsShows collection-backed highlighting and range-aware selection
packages/…/select.machine.tsShows closed-trigger selection, open option navigation, and form-facing value changes
packages/…/combobox.machine.tsShows the relationship between input text, suggestions, highlight, and selected values
packages/…/tree-view.machine.tsShows visible tree navigation and independent expansion, selection, checking, and rename state
packages/…/navigation-menu.machine.tsShows delayed top-level navigation, content focus, and viewport coordination
packages/…/carousel.machine.tsShows page position derived from scroll, snap points, drag, and autoplay
packages/…/steps.machine.tsShows bounded sequential navigation and forward validation
packages/…/pagination.machine.tsShows page and page-size state derived from a data count