Accessible Tab Component
What you'll build
A tab component with 3-4 panels where users can switch tabs via mouse, keyboard (arrow keys, Home, End), and screen readers announce the current tab.
Goals
- Implement the WAI-ARIA tabs pattern correctly
- Support keyboard navigation (Arrow Left/Right, Home, End)
- Manage focus and aria-selected/aria-hidden states
- Make it reusable for any number of tabs
Prerequisites
- HTML structure
- CSS for styling active/inactive tabs
- JavaScript keyboard event handling
- Basic ARIA attributes
Steps
- 1
Study the ARIA tabs pattern
Read the WAI-ARIA Authoring Practices for tabs. Acceptance: the tablist has role='tablist', each tab has role='tab' and aria-controls, each panel has role='tabpanel' and aria-labelledby. Document the required attributes before coding.
Hint
URL: https://www.w3.org/WAI/ARIA/apg/patterns/tabs/ — focus on the keyboard interaction section.
- 2
Build the HTML structure
Create a tablist with N tabs and N corresponding panels. Wire aria-controls (tab→panel) and aria-labelledby (panel→tab). Only the active panel is visible initially.
Hint
Use matching id attributes so aria-controls can reference them, e.g. tab-1 controls panel-1.
- 3
Implement click activation
Clicking a tab activates it: set aria-selected=true on the clicked tab and false on others, show the corresponding panel and hide the rest. Acceptance: only one tab is selected at a time.
Hint
Loop over all tabs to deactivate, then activate the clicked one.
- 4
Add keyboard navigation
Arrow Right/Down moves focus to the next tab, Arrow Left/Up to the previous, Home to the first, End to the last. When focus moves, the newly focused tab becomes active. Acceptance: all keys work and no tab is skipped.
Hint
Use e.key to switch on 'ArrowRight', 'ArrowLeft', 'Home', 'End'. Wrap around at the ends.
- 5
Test with a screen reader
Run NVDA or VoiceOver and navigate the tabs. Acceptance: the screen reader announces 'tab, X of N, selected' for the active tab and lets you switch with arrows. Verify focus is visible.
Hint
If the screen reader does not announce 'selected', check that aria-selected is on the focused tab, not just the clicked one.
Try it yourself
What you learned
You built a component to WAI-ARIA standards — accessible tab components are a common interview task and a real test of whether you can make interactive UIs work for keyboard and screen reader users.
Related Resources
Related Lessons
- HTML Accessibility
ARIA roles and attributes for tabs.
- JavaScript Events
Keyboard event handling for arrow navigation.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.