useKeyboardNavigation
Moves focus between a group of elements with the arrow keys, Home and End. Use it for composite widgets such as toolbars and custom tab lists, where the group is one tab stop and the arrow keys move between items.
@volvo-cars/react-aria exports createKeyboardNavigation, a factory that returns useKeyboardNavigation together with a matching KeyboardNavigationProvider and useAdvancedKeyboardNavigation. Call the factory once at module level, not inside a component.
Wrap the group in KeyboardNavigationProvider, then spread the props returned by useKeyboardNavigation onto each item. Items are ordered by their position in the DOM.
import { createKeyboardNavigation } from '@volvo-cars/react-aria';
const { KeyboardNavigationProvider, useKeyboardNavigation } = createKeyboardNavigation();
function ToolbarButton({ children }: { children: React.ReactNode }) { const props = useKeyboardNavigation<HTMLButtonElement>();
return ( <button type="button" {...props}> {children} </button> );}
function Toolbar() { return ( <KeyboardNavigationProvider> <div role="toolbar" aria-label="Text formatting" className="flex gap-8"> <ToolbarButton>Bold</ToolbarButton> <ToolbarButton>Italic</ToolbarButton> <ToolbarButton>Underline</ToolbarButton> </div> </KeyboardNavigationProvider> );}ArrowRightandArrowLeftfocus the next and previous item when horizontal. They swap in right-to-left textArrowDownandArrowUpfocus the next and previous item when verticalHomefocuses the first itemEndfocuses the last item
Focus loops from the last item to the first, and the other way around.
Vertical groups
Section titled “Vertical groups”Pass orientation: 'vertical' to use the up and down arrow keys instead:
const props = useKeyboardNavigation<HTMLButtonElement>({ orientation: 'vertical',});useKeyboardNavigation accepts an optional options object with the following properties:
orientation–'horizontal'or'vertical'; defaults to'horizontal'direction–'ltr'or'rtl'; only used when horizontal. Defaults to the computeddirectionof the first itemref– optional ref to your own element, merged with the ref the hook returnsonKeyDown– optional handler called before the navigation handler
The hook returns an object with:
ref– ref to attach to the item elementonKeyDown– key handler to attach to the item element
Custom keys
Section titled “Custom keys”useAdvancedKeyboardNavigation takes a function that receives next, prev, first and last, and returns an object that maps event.key values to one of those results. next and prev accept { loop: true } to wrap around.
const { useAdvancedKeyboardNavigation } = createKeyboardNavigation();
function Item() { const props = useAdvancedKeyboardNavigation<HTMLButtonElement>( ({ next, prev }) => ({ j: next(), k: prev(), }), );
return <button type="button" {...props} />;}Pass a stable function, for example one defined outside the component, to avoid recreating the key handler on every render.
Behavior
Section titled “Behavior”- Each item must be inside a
KeyboardNavigationProvider; without one, key presses do nothing - Matching keys call
preventDefaultand focus the target item - Reading the returned
refbefore its element has mounted throws an error - The hook moves focus only. Set
tabIndexyourself if the group should be a single tab stop