Keyboard navigation patterns
Focus order, roving tabindex and arrow key navigation, the patterns behind menus, listboxes and toolbars.
Updated
Plenty of people never touch a mouse: keyboard users, screen reader users, people with switch devices and power users who simply prefer the keyboard. Keyboard support is not an extra feature. It is how a large share of people operate your interface.
Tab moves between widgets
Tab and Shift Tab move focus through interactive elements in DOM order. Keep the DOM order aligned with the visual order, and never remove the focus outline without putting a clearly visible replacement in its place.
/* Hide the ring for mouse clicks only, keep it for the keyboard */
button:focus:not(:focus-visible) {
outline: none;
}
button:focus-visible {
outline: 2px solid var(--focus-color);
outline-offset: 2px;
}Arrow keys move inside widgets
Composite widgets such as menus, tabs, listboxes, toolbars and grids take up a single tab stop. Once focus is inside, the arrow keys move between items. A toolbar with twenty buttons then costs one press of Tab instead of twenty.
- Last input
- Focus the list
- aria-activedescendant
- option-fra
- Selected
- London
Roving tabindex
With a roving tabindex, only the active item has tabindex="0" and every other item has tabindex="-1". When an arrow key is pressed, you update the values and call focus() on the new item, so the browser's real focus moves along with it.
function Toolbar({ items }: { items: string[] }) {
const [current, setCurrent] = useState(0)
const refs = useRef<(HTMLButtonElement | null)[]>([])
const move = (next: number) => {
const index = (next + items.length) % items.length
setCurrent(index)
refs.current[index]?.focus()
}
return (
<div role='toolbar' aria-label='Formatting'>
{items.map((item, index) => (
<button
key={item}
ref={(node) => { refs.current[index] = node }}
tabIndex={index === current ? 0 : -1}
onKeyDown={(event) => {
if (event.key === 'ArrowRight') move(index + 1)
if (event.key === 'ArrowLeft') move(index - 1)
}}
>
{item}
</button>
))}
</div>
)
}aria-activedescendant
The alternative keeps real focus on the container and points aria-activedescendant at the id of the active item. Screen readers announce that item as if it had focus. It shines in comboboxes, where focus has to stay in the text field while people move through suggestions, and it is what the listbox demo above uses.
Expected keys by pattern
Managing focus when things open and close
When a dialog or menu opens, move focus into it. When it closes, send focus back to the element that opened it. If focus falls back to the top of the page, keyboard users have to find their place all over again.
function useReturnFocus(open: boolean) {
const trigger = useRef<HTMLElement | null>(null)
useEffect(() => {
if (open) {
trigger.current = document.activeElement as HTMLElement
} else {
trigger.current?.focus()
}
}, [open])
}Summary
- Keep the focus order logical and the focus ring visible.
- Composite widgets are one tab stop; arrow keys move inside them.
- Use a roving tabindex when real focus should move,
aria-activedescendantwhen it should stay put. - Return focus to the trigger when a dialog or menu closes.
Finished this lesson?
Mark it complete to track your progress across every track in the curriculum.