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 grid navigation. Navigation stops at grid boundaries.
- ArrowLeft/ArrowRight – move within the row
- ArrowUp/ArrowDown – move between rows, the column is preserved
- Home/End – first/last item in the current row
- Ctrl+Home/Ctrl+End – first/last item in the grid
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 click items when they receive focus
via keyboard navigation, for example in tab-like interfaces where focus and selection
are 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: