Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 38 ยท Projects and Practice

Project: Build an Accessible Tabs Component

Build a reusable, accessible tabs component in Lit using named slots, ARIA roles, roving tabindex, arrow-key navigation, a change event and CSS custom properties for theming.

Tabs look simple and hide a lot of detail: ARIA roles, which tab is focusable, arrow-key behavior, and panels that must be linked to their tabs. They are the classic test of a component author. In this project you build ui-tabs so that its users write only plain markup:

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

The component finds the tabs and panels from its slots, wires up the accessibility attributes, and handles the keyboard. This uses slots, events and accessible widget patterns.

Requirements

  1. Click or press a tab to show its panel.
  2. Arrow Left/Right move focus and selection; Home/End jump to the first/last tab.
  3. Only the selected tab is in the page tab order (roving tabindex), so Tab key moves past the tab list into the panel.
  4. Screen readers get role="tablist", role="tab", role="tabpanel", aria-selected, aria-controls and aria-labelledby.
  5. A tab-change event reports the new index.
  6. Colors come from CSS custom properties.

How the pieces connect

flowchart LR
    S1["slot=tab elements"] --> L["ui-tabs listens to slotchange"]
    S2["slot=panel elements"] --> L
    L --> W["adds roles, ids, aria-*"]
    K["keydown / click"] --> SEL["selected index"]
    SEL --> W
    SEL --> E["tab-change event"]

The component never owns the tabs' content; it only decorates the nodes you slot in.

The component

Slotted elements are light DOM nodes, so the component can set attributes on them directly. Because that is a side effect on foreign nodes, we do it in updated, after each render, and not in render.

ui-tabs

Walkthrough

Discovering content. querySelectorAll(':scope > [slot="tab"]') finds only direct children with the right slot name. The slotchange listener calls requestUpdate(), so adding or removing a tab later re-wires everything. See slots and composition.

Decorating the nodes. updated() sets role, aria-selected, tabindex, ids and the panel link. Setting hidden on the panel hides it from everyone, including assistive technology.

Roving tabindex. Only the selected tab has tabIndex = 0. Pressing Tab enters the tab list on the selected tab, and pressing Tab again leaves it for the panel. Inside the list, the arrow keys do the moving, which matches how native widgets behave.

Wrapping. (index + count) % count wraps in both directions, including negative numbers.

Event. tab-change bubbles and is composed, so it crosses shadow boundaries and parent code can listen on the ui-tabs element.

Theming. --tabs-accent and --tabs-border are the component's public styling API. Users override them from outside: ui-tabs { --tabs-accent: tomato; }.

Design decisions to discuss

  • Automatic vs manual activation. Here, an arrow key both moves focus and selects the tab. For panels that are expensive to show, the ARIA guide also allows "manual" activation: arrows move focus, and Enter or Space selects.
  • Why not render the tabs ourselves? A tabs property with labels would be simpler to write but less flexible: users could not put icons or badges inside a tab. Slots keep markup in the user's hands.
  • Initial selection. Support an attribute so authors can start on another tab: <ui-tabs selected="2"> works already, thanks to reflect: true and the Number type.

Extensions

  1. Add a vertical attribute that switches to Up/Down keys and a column layout.
  2. Add disabled tabs that the arrow keys skip.
  3. Sync selected with location.hash.
  4. Write tests for the keyboard behavior using the patterns from the testing lessons.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a roving tabindex?

    The tab list is one stop in the Tab order. Arrow keys move inside it, like a native radio group.
  2. 2. Why does the component use updated() to set ARIA attributes?

    Templates render the component's own shadow DOM. Slotted nodes belong to the user, so we decorate them after the render completes.
  3. 3. Why should tab-change use bubbles and composed?

    composed lets an event cross the shadow boundary; bubbles lets ancestors catch it.
  4. 4. What happens when the user presses ArrowRight on the last tab?

    The modulo expression wraps the index, matching common tab widget behavior.

Summary

  • Slots keep markup flexible; the component adds roles, ids and ARIA state after each render.
  • Roving tabindex plus arrow, Home and End keys give native-like keyboard behavior.
  • Fire a composed event for state changes and expose CSS custom properties for styling.
  • Decide on automatic or manual activation and document it.

Next: Project: A Sortable, Filterable Data Table