Skip to Content
ohooks

useRovingTabIndex

Implements the WAI-ARIA roving tabIndex keyboard navigation pattern for composite widgets.

Loading preview...

The useRovingTabIndex hook implements the WAI-ARIA roving tabIndex pattern for toolbars, tablists, menus, and radio groups, managing arrow-key navigation, Home/End shortcuts, looping, disabled item skipping, and DOM focus synchronization.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useRovingTabIndex.json

Usage

Examples

Horizontal Toolbar with Disabled Items

Pass { disabled: true } to getItemProps so arrow keys automatically skip disabled buttons while navigating.

Vertical Menu List

Set orientation: "vertical" to navigate items using ArrowUp and ArrowDown.

API Reference

UseRovingTabIndexOptions

Configuration options for orientation, looping, and controlled/uncontrolled active index.

Property
Specification
count?
number
Total number of items (inferred automatically from registered refs if omitted).
activeIndex?
number
Controlled active item index.
defaultActiveIndex?
numberdefault:0
Initial active item index in uncontrolled mode
orientation?
uniondefault:'horizontal'
Arrow key navigation orientation
loop?
uniondefault:true
Whether arrow navigation wraps around at the ends
rtl?
uniondefault:false
Flip horizontal arrow keys for right-to-left layouts
onActiveIndexChange?
function
Callback fired when the active index changes.

RovingItemOptions

Per-item options passed to getItemProps(index, options).

Property
Specification
disabled?
union
Whether this specific item is disabled and skipped during navigation.
onKeyDown?
function
Custom onKeyDown handler composed with roving keyboard navigation.
onFocus?
function
Custom onFocus handler composed with active index synchronization.

RovingTabIndexItemProps

Props returned by getItemProps to spread onto each focusable item element.

Property
Specification
refrequired
function
Ref callback that registers the item's DOM node.
tabIndexrequired
number
0 for the active tabbable item, -1 for all other items.
onKeyDownrequired
function
Keyboard handler managing arrow, Home, and End navigation.
onFocusrequired
function
Focus handler that updates the active index when focused.

UseRovingTabIndexReturn

Active index state and helper methods returned by the hook.

Property
Specification
activeIndex
number
Currently active item index.
setActiveIndex
function
Updates the active index without moving DOM focus.
focusItem
function
Updates the active index and moves DOM focus to the item.
getItemProps
function
Generates ref, tabIndex, onKeyDown, and onFocus props for an item at index.