Accessibility

a11y/table-sort

Sortable columns use a button inside the th, and aria-sort only on the header of the column that is sorted.

moderatemanualWCAG 2.1.1, 4.1.2 · Afe-a11y-tables

Why

A click handler on the <th> cannot be reached with the keyboard and does not say it can be activated. Without aria-sort nobody using a screen reader knows the current order.

Fix

Put <button type="button"> inside each sortable <th> with the column name as its text; set aria-sort="ascending" or "descending" on the sorted <th> only, and remove it from the others. Announce the new order in a polite live region if the table is long.

Examples

<th scope="col" aria-sort="ascending"><button type="button">Date <span aria-hidden="true">▲</span></button></th>
<th scope="col"><button type="button">Amount</button></th>

Sources

Facts

Id
a11y/table-sort
Severity
moderate
Check
manual a person reviews it; the report lists it as a checklist
Applies to
html angular vue svelte astro jsx
WCAG 2.2
2.1.1, 4.1.2, level A
Taught by the skill
fe-a11y-tables Data tables
Since version
0.1.0

Rule file on GitHub

Read it in the terminal

npx github:dawidolko/frontend-skills explain a11y/table-sort