use-roving-index
Implement roving tabindex keyboard navigation pattern
Source
Package
Usage
use-roving-index implements the roving tabindex pattern.
Only one item in the group is reachable with the Tab key, arrow keys move focus between items:
Orientation
Set orientation to choose which arrow keys move focus:
horizontal(default) – ArrowLeft/ArrowRightvertical– ArrowUp/ArrowDownboth– all four arrow keys
Grid navigation
Set columns to navigate a grid. Navigation stops at the grid edges:
- 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 to mark items as disabled. Keyboard navigation skips them:
Loop
By default, navigation wraps around at the first and last items. Set loop: false to stop there:
Controlled mode
Use focusedIndex and onFocusChange to control the focused index externally:
activateOnFocus
Set activateOnFocus: true to click an item when it receives focus with the keyboard,
for example in tabs where focus and selection are synchronized:
RTL support
Set dir: 'rtl' to swap ArrowLeft and ArrowRight in right-to-left layouts:
Definition
Exported types
UseRovingIndexInput, UseRovingIndexGetItemPropsInput and UseRovingIndexReturnValue types are exported
from the @mantine/hooks package: