Skip to content
A11Y-02Intermediate6 min readNew

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.

focus.css
/* 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.

Deployment region
  • Frankfurteu-central
  • Amsterdameu-west
  • Londoneu-west
  • Virginiaus-east
  • San Joseus-west
  • Sao Paulosa-east
  • Tokyoap-northeast
  • Singaporeap-southeast
  • Sydneyap-southeast
Last input
Focus the list
aria-activedescendant
option-fra
Selected
London
A keyboard friendly listboxFocus the list with Tab, move with the arrow keys, jump with Home and End, type a letter to find a region, and press Enter or Space to select it.

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.

toolbar.tsx
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.

TechniqueReal focus movesBest for
Roving tabindexYesToolbars, tabs, radio groups, grids
aria-activedescendantNo, it stays on the containerComboboxes and listboxes driven from a text field

Expected keys by pattern

PatternKeys people expect
Tabs← and → move between tabs, Home and End jump to the first and last
Menu↑ and ↓ move, Enter activates, Esc closes and returns focus to the trigger
Listbox↑ and ↓ move, typing a character jumps to the next match, Space selects
DialogTab cycles inside the dialog, Esc closes it
DisclosureEnter or Space shows and hides the panel

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.

use-return-focus.ts
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-activedescendant when 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.