Skip to content

QuizNot attempted

Spot the bug: a clickable table header

What is wrong with this sortable header for keyboard and screen-reader users?
<th onClick={() => handleSort(column.key)}>
{column.label}
{sortedColumn === column.key && <span>{sortDirection === 'asc' ? ' ▲' : ' ▼'}</span>}
</th>

Select all that apply.

1–4 to choose, Enter to submit
Show answer & explanation

Correct answers:

  • It cannot be focused or activated from the keyboard
  • The sort state is not exposed: there is no aria-sort, only a glyph
  • Nothing tells assistive tech that it is clickable: it has no button role

A <th> isn't interactive. Put a real button inside it and expose the state on the header cell:

<th scope="col" aria-sort={active ? (dir === 'asc' ? 'ascending' : 'descending') : undefined}>
<button type="button" onClick={() => sortBy(col.key)}>
{col.label} <span aria-hidden="true">{active ? (dir === 'asc' ? '▲' : '▼') : ''}</span>
</button>
</th>

Set aria-sort on the sorted column only, and give the table a <caption>. Not tabIndex: it makes the cell focusable, but Enter and Space still do nothing and it still has no button role; a <button> gives you all three for free.