Skip to content

useKeyboardNavigation

Edit on GitHub
@volvo-cars/react-aria v0.4.0

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>
);
}
  • ArrowRight and ArrowLeft focus the next and previous item when horizontal. They swap in right-to-left text
  • ArrowDown and ArrowUp focus the next and previous item when vertical
  • Home focuses the first item
  • End focuses the last item

Focus loops from the last item to the first, and the other way around.

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 computed direction of the first item
  • ref – optional ref to your own element, merged with the ref the hook returns
  • onKeyDown – optional handler called before the navigation handler

The hook returns an object with:

  • ref – ref to attach to the item element
  • onKeyDown – key handler to attach to the item element

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.

  • Each item must be inside a KeyboardNavigationProvider; without one, key presses do nothing
  • Matching keys call preventDefault and focus the target item
  • Reading the returned ref before its element has mounted throws an error
  • The hook moves focus only. Set tabIndex yourself if the group should be a single tab stop