useListFocus@xds/core · hooks
Preview coming soon

Usage

Manages keyboard navigation within a linear list following WAI-ARIA menu/listbox/toolbar patterns. Supports arrow key navigation (vertical or horizontal), Home/End for boundaries, optional wrap-around, and Escape to close. Suitable for dropdown menus, toolbars, and any 1D focusable list.

Best practices

GuidancePractices
DoSet orientation to 'horizontal' for toolbars and tab bars, 'vertical' for dropdown menus.
DoProvide an onEscape callback for menus/dropdowns to return focus to the trigger.
Don'tUse for 2D grid navigation — prefer useGridFocus for grids and calendars.

Parameters

ParamTypeDescription
options
UseListFocusOptionsConfiguration object for list focus behavior. All fields are optional.
options.itemSelector
string (default: '[role="menuitem"]')Selector for focusable items within the list.
options.wrap
boolean (default: true)Whether arrow navigation wraps around at the ends.
options.onEscape
() => voidCallback when Escape key is pressed (e.g., close menu).
options.orientation
'horizontal' | 'vertical' (default: 'vertical')Navigation orientation. 'horizontal' uses ArrowLeft/ArrowRight, 'vertical' uses ArrowUp/ArrowDown.

Returns

FieldTypeDescription
listRefReact.RefObject<HTMLElement | null>Ref to attach to the list container element.
handleKeyDown(e: React.KeyboardEvent) => voidKey down handler to attach to the list container.
focusItem(index: number) => voidFocus a specific item by index (clamped to valid range).
focusFirst() => voidFocus the first focusable item in the list.
focusLast() => voidFocus the last focusable item in the list.

Import

ts
import {useListFocus} from '@xds/core/hooks'