Tabs
UI Coding
Easy
20 min
state
accessibility
design-system
Build a Tabs component. A row of tab buttons sits above a content area, and only the selected tab's panel is visible.
The starter has the data (items with value, label, panel) and renders every panel at once.
Requirements
- The first tab is selected by default. Accept an optional
defaultValueprop to start on a different tab. - Clicking a tab selects it and shows only its panel.
- The selected tab is visually distinct (not just by colour: add an underline or a border too).
- Use the tab roles:
- the button row is a
role="tablist"with anaria-label - each button is a
role="tab"witharia-selectedandaria-controls - each panel is a
role="tabpanel"witharia-labelledbypointing back to its tab.
- the button row is a
- IDs must be unique even when two
Tabsrender on one page.
Arrow-key navigation is not required here. It's covered in Tabs II.
Follow-up
- Should hidden panels stay mounted? When does it matter (form state, video playback, expensive renders)?
- How would you support a controlled
value/onChangeAPI? - What would you change to sync the selected tab to the URL?
Spotted a mistake or a rights concern? Report an issue