Dostępność

a11y/dialog-focus

A modal moves focus inside on open, keeps Tab inside, closes on Escape, and returns focus to the control that opened it.

poważnaręczneWCAG 2.4.3, 2.1.2 · Afe-a11y-dialog

Dlaczego

If focus stays behind the overlay, keyboard and screen reader users keep working in a page they cannot see. If it is lost on close, they start again from the top of the page.

Poprawka

On open, focus the first field, or the least destructive button in a confirmation, or the dialog heading (tabindex="-1") when the content is long; autofocus on that element inside <dialog> does it. While open, the rest of the page is inert (showModal() does this). On close, focus the trigger again; if it is gone, focus the next logical place. Test with the keyboard and a screen reader.

Przykłady

let trigger
openButton.addEventListener('click', () => { trigger = document.activeElement; dialog.showModal() })
dialog.addEventListener('close', () => trigger?.focus())

Źródła

Fakty

Identyfikator
a11y/dialog-focus
Waga
poważna
Sprawdzanie
ręczne ocenia człowiek; raport podaje to jako listę kontrolną
Dotyczy plików
page
WCAG 2.2
2.4.3, 2.1.2, poziom A
Uczy jej skill
fe-a11y-dialog Dialogs and modals
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain a11y/dialog-focus