use-roving-index
Implement roving tabindex keyboard navigation pattern
Source
Docs
Package
Usage
use-roving-index implements the roving tabindex keyboard navigation pattern.
In a group of focusable elements, only one element has tabIndex={0} (can be reached with Tab key),
while all others have tabIndex={-1}. Arrow keys move focus between items in the group.
Orientation
Set orientation to control which arrow keys are used for navigation:
'horizontal'(default) – ArrowLeft/ArrowRight'vertical'– ArrowUp/ArrowDown'both'– all four arrow keys
Grid navigation
Set columns to enable 2D grid navigation. ArrowLeft/ArrowRight navigate within a row,
ArrowUp/ArrowDown navigate across rows preserving the column position.
Navigation stops at grid boundaries. Use Ctrl+Home/Ctrl+End to jump to the first/last item in the grid,
and Home/End to jump to the first/last item in the current row.
Disabled items
Use isItemDisabled callback to mark items as disabled. Disabled items are skipped during
keyboard navigation. If the initially focused item is disabled, the first non-disabled item
receives focus instead.
Loop
By default, navigation wraps around at boundaries (loop is true).
Set loop={false} to stop at the first and last items.
Controlled mode
Use focusedIndex and onFocusChange to control the focused index externally:
activateOnFocus
Set activateOnFocus to true to automatically click items when they receive focus
via keyboard navigation. This is useful for tab-like interfaces where focus and selection
should be synchronized:
RTL support
Set dir="rtl" to swap ArrowLeft/ArrowRight behavior for right-to-left layouts:
Definition
Exported types
UseRovingIndexInput, UseRovingIndexGetItemPropsInput and UseRovingIndexReturnValue types are exported
from the @mantine/hooks package; you can import them in your application: