← Back to Frontend, page 3

frontend-0041 · Hard senior

How do you build an accessible tabs widget with roles, roving tabindex and arrow key navigation?

Use role tablist, tab and tabpanel, link them with aria-controls and aria-labelledby, mark the active tab with aria-selected, keep only the active tab in the Tab order with tabindex, and move between tabs with the arrow keys, Home and End.

Full answer

The ARIA Authoring Practices describe a pattern that users of assistive technology expect. For tabs:

  • The container has role="tablist". Each tab is a button with role="tab". Each panel has role="tabpanel".
  • aria-selected="true" is on the active tab and false on the others. aria-controls on each tab points to its panel, and the panel has aria-labelledby pointing back to the tab.
  • Only the selected tab is in the page's Tab order (tabindex="0"), and the others have tabindex="-1". This is a roving tabindex. One Tab press enters the widget, and the next Tab press leaves it, so a long list of tabs does not trap the user.
  • Left and Right arrow keys move to the previous and next tab, and wrap around at the ends. Home and End go to the first and last tab. In the simple "automatic activation" model, focus and selection move together.
  • Inactive panels are hidden with the hidden attribute, which removes them from the tree and the layout.
  • The panel can take tabindex="0" when it has no focusable content, so keyboard users can reach it.

Do this only if you need custom tabs. If the content is a set of page links, use a nav list with links. For disclosure of a single section, details and summary are enough. A custom widget costs code and testing.

Common bugs: using aria-selected without updating it, hiding panels with CSS in a way that leaves them in the tree, forgetting to call focus() on the new tab, and leaving every tab at tabindex="0".

The test presses Tab to enter the widget, then ArrowRight, End, and ArrowLeft, and reads which tab has focus and which panel is visible. It then reads the Chrome accessibility snapshot to confirm the selected tab. No screen reader was run.

html
<div role="tablist" aria-label="Course sections" id="tabs">
  <button role="tab" id="t1" aria-controls="p1" aria-selected="true" tabindex="0">Overview</button>
  <button role="tab" id="t2" aria-controls="p2" aria-selected="false" tabindex="-1">Lessons</button>
  <button role="tab" id="t3" aria-controls="p3" aria-selected="false" tabindex="-1">Reviews</button>
</div>
<div role="tabpanel" id="p1" aria-labelledby="t1">About the course</div>
<div role="tabpanel" id="p2" aria-labelledby="t2" hidden>Lesson list</div>
<div role="tabpanel" id="p3" aria-labelledby="t3" hidden>Student reviews</div>
<script>
  const tabs = [...document.querySelectorAll('[role=tab]')];
  function select(i) {
    tabs.forEach((t, n) => {
      const on = n === i;
      t.setAttribute('aria-selected', on);
      t.tabIndex = on ? 0 : -1;
      document.getElementById(t.getAttribute('aria-controls')).hidden = !on;
    });
    tabs[i].focus();
  }
  tabs.forEach((t, i) => t.addEventListener('click', () => select(i)));
  document.getElementById('tabs').addEventListener('keydown', (e) => {
    const i = tabs.indexOf(document.activeElement);
    if (i < 0) return;
    const keys = { ArrowRight: (i + 1) % tabs.length, ArrowLeft: (i + tabs.length - 1) % tabs.length, Home: 0, End: tabs.length - 1 };
    if (e.key in keys) { e.preventDefault(); select(keys[e.key]); }
  });
</script>

Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The check used real key presses (Tab, ArrowRight, End, ArrowLeft) and read the focused tab and visible panel. The accessibility snapshot showed the selected tab. No screen reader was run.

Key points

  • Roles: tablist, tab, tabpanel, linked by aria-controls and aria-labelledby.
  • aria-selected marks the active tab.
  • Roving tabindex: only the active tab is a Tab stop.
  • Arrow keys, Home and End move between tabs.
  • Hide inactive panels with the hidden attribute.

Common mistakes

  • Leaving all tabs at tabindex 0.
  • Switching the panel with a click handler only, with no key support.
  • Forgetting to update aria-selected.
  • Building custom tabs where a list of links would do.

Follow-ups

  • When should activation wait for Enter instead of following the arrow keys?
  • How would you support a vertical tablist?
  • How does this differ from an accordion?

Tips

  • Name the three roles and the roving tabindex idea first.
  • Say when not to build it.