Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 39 ยท Projects and Practice

Project: Accessible Tabs

Build a tabs component with slots, the tab, tablist and tabpanel ARIA roles, roving tabindex, arrow-key navigation, a change event and a selected property.

Tabs are the classic test of a component author. Mouse users see a row of buttons and panels; screen reader users need a precise structure of roles and states; keyboard users expect arrow keys. Getting all three right shows that you understand ARIA, focus handling, slots and events.

Design

We want markup that reads naturally:

HTML
<x-tabs>
  <button slot="tab">Overview</button>
  <div slot="panel">...</div>
  <button slot="tab">Details</button>
  <div slot="panel">...</div>
</x-tabs>

Tabs and panels are light DOM children placed into two named slots. Pairing is by order: the first tab controls the first panel. Keeping them in the light DOM has an accessibility benefit: id references such as aria-controls and aria-labelledby work only inside one DOM tree, and both elements are in the same (light) tree.

Requirements

  1. The tab strip has role="tablist"; each tab has role="tab"; each panel has role="tabpanel".
  2. Exactly one tab is selected: aria-selected="true", tabindex="0"; the others are aria-selected="false", tabindex="-1" (roving tabindex).
  3. Arrow keys move between tabs (wrapping), Home/End jump to the ends. We use automatic activation: moving focus selects the tab.
  4. Panels not selected are hidden with the hidden attribute.
  5. A selected property (index) and a tab-change event.
flowchart TB
  K["Key pressed on a tab"] --> A{"Which key?"}
  A -- "Right / Left" --> N["next or previous tab, wrapping"]
  A -- "Home / End" --> E["first or last tab"]
  N --> S["select(index)"]
  E --> S
  S --> U["update aria-selected, tabindex, hidden"]
  U --> V["focus tab + dispatch tab-change"]

Keyboard handling funnels into one select() method.

The component

Accessible tabs component

Press Tab to enter the strip (you land on the selected tab), then use the arrows. Press Tab again and focus jumps to the following control, not to the next tab: that is the roving tabindex at work.

Walkthrough

  • Pairing by index in connectedCallback generates ids and connects aria-controls and aria-labelledby. These references work because tabs and panels are siblings in the same tree.
  • select() is the only mutator. Click, keyboard and the selected setter all call it, so state cannot drift. The modulo expression wraps negative and too-large indexes.
  • emit and focus options avoid firing an event or stealing focus on the initial render.
  • Styling hooks. Selected styling uses ::slotted([aria-selected="true"]), so the visual state follows the accessible state. The accent color is the variable --tabs-accent.

This version assumes the tabs are present when the component connects. A production component would also listen to slotchange on both slots to handle tabs added or removed later, and would support a selected attribute reflecting the property.

Manual and automatic activation

We selected on focus ("automatic activation"), which is fine when panels render instantly. If switching is slow (network content), use manual activation: arrows only move focus, and Enter or Space selects. The ARIA pattern allows either; pick one deliberately.

Test checklist

  1. Tab lands on the selected tab only, and Tab again leaves the component.
  2. Screen reader announces "Overview, tab, selected, 1 of 3" and the panel's label.
  3. Hidden panels are not reachable and not announced.
  4. Arrow keys wrap at both ends; Home and End work.
  5. The page can listen to tab-change, set selected, and restyle with --tabs-accent.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why are tabs and panels kept in the light DOM here?

    IDs do not cross shadow boundaries, so slotting light DOM nodes keeps the ARIA references valid.
  2. 2. What does roving tabindex mean for the unselected tabs?

    Only the selected tab is in the tab order; arrow keys move between the rest.
  3. 3. What is the benefit of routing click, keys and the property setter through select()?

    A single mutator keeps aria-selected, tabindex, hidden panels and events consistent.
  4. 4. When would you choose manual activation?

    Manual activation avoids loading a panel for every tab focused while arrowing past it.

Summary

  • Tabs use roles tablist, tab and tabpanel, with aria-selected, aria-controls and aria-labelledby.
  • Roving tabindex and arrow, Home and End keys give keyboard users native-like control.
  • Slot light DOM tabs and panels so ID references stay valid.
  • One select() method keeps state, DOM and events consistent.

Next: Practice Exercises and Challenges