Dostępność
a11y/prefer-native-element
Use the native element instead of a role it already provides (button, a href, input, h1–h6, nav, main, ul, li).
drobnastatyczneWCAG 4.1.2 · Afe-a11y-aria
Dlaczego
A role changes only what is announced. A div role="button" still needs tabindex, Enter and Space handling, a disabled state and form behaviour written by hand, and each missing piece is a bug for keyboard and screen reader users.
Poprawka
Swap the element: <button type="button">, <a href>, <input type="checkbox">, <h2>, <nav>, <ul>/<li>. Restyle it with CSS (all: unset or a reset) if the look is the reason for the div. Keep a role only where no native element exists (tablist, switch on a checkbox, img on an svg).
Przykłady
<!-- bad -->
<div role="button" tabindex="0" onclick="save()">Save</div>
<!-- good -->
<button type="button" onclick="save()">Save</button>Źródła
Fakty
- Identyfikator
a11y/prefer-native-element- Waga
- drobna
- Sprawdzanie
- statyczne sprawdzane w kodzie źródłowym przez
audit - Dotyczy plików
htmlangularvuesvelteastrojsx- WCAG 2.2
- 4.1.2, poziom A
- Uczy jej skill
fe-a11y-ariaARIA, names and live regions- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain a11y/prefer-native-element