Dostępność

a11y/menu-keyboard

Menus implement their keys - arrows move between items, Enter activates, Escape closes and returns focus to the button.

poważnaręczneWCAG 2.1.1 · Afe-a11y-menus

Dlaczego

A role="menu" turns off the screen reader's own reading keys and hands the keyboard to the page. If the page does not implement arrows, Enter and Escape, the menu cannot be used at all.

Poprawka

Opening with Enter, Space or Down Arrow focuses the first item (Up Arrow: the last); Up and Down move with wrapping; Home and End jump; typing a letter moves to the next item starting with it; Enter activates; Escape closes and focuses the button; Tab closes and moves on. Only one item has tabindex="0". If this is more than the use case needs, use a disclosure (button plus list of links) instead.

Przykłady

menu.addEventListener('keydown', (e) => {
  if (e.key === 'Escape') { close(); button.focus() }
  if (e.key === 'ArrowDown') focusItem(index + 1)
  if (e.key === 'ArrowUp') focusItem(index - 1)
})

Źródła

Fakty

Identyfikator
a11y/menu-keyboard
Waga
poważna
Sprawdzanie
ręczne ocenia człowiek; raport podaje to jako listę kontrolną
Dotyczy plików
page
WCAG 2.2
2.1.1, poziom A
Uczy jej skill
fe-a11y-menus Navigation, disclosures and menus
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain a11y/menu-keyboard