Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Tabs

Tabs show one panel of content at a time, with a list of tabs to switch between them. A keyboard user reaches the tab list with one Tab press, moves between the tabs with the arrow keys and reaches the open panel with the next Tab.

Package
@kvirn-ui/react
Pattern
APG Tabs
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use tabs when one page holds a few parts of the same thing, such as the details, documents and history of a case, and people need one part at a time.
  • Keep the labels short and in the same style. Content that people must read together doesn’t belong in tabs.
  • Not for going to another page: use Navigation with kv-navigation--horizontal. A tab never changes the URL.
  • Not for showing and hiding one block of text: use a Disclosure. Don’t put interactive content, such as a close button, inside a tab.

Example#

Building permit application for 4 Strand Road. The case awaits review.

Use cases#

Panels that are slow to show

By default, the arrow keys select the tab they move to, and its panel shows at once. If showing a panel loads data or is heavy, arrowing through the tabs would load every panel on the way. Set activationMode to manual: the arrows only move focus, and Enter or Space selects.

Building permit application for 4 Strand Road. The case awaits review.

Props in this example:

Information: Implement like this

Every panel stays in the page, so render a heavy panel’s content only while it is selected.

<Tabs.Root value={value} onValueChange={setValue}>
  <Tabs.Panel value="history">{value === 'history' && <History />}</Tabs.Panel>
</Tabs.Root>

A column of tabs beside the content

Use it when there are many tabs with short names, such as the sections of an application. The arrow keys become Down and Up, and the list gets aria-orientation.

Maria Lind, 4 Strand Road.

Props in this example:

A tab that can’t be used right now

Sometimes a tab can’t be used until something has happened, such as a decision on a case. People can still reach it with the arrow keys, and a screen reader (software that reads the screen aloud) says it is unavailable, so they can find it. Link the reason to it with aria-describedby.

The decision tab opens when the case has been decided.

Building permit application for 4 Strand Road. The case awaits review.

Props in this example:

Warning: Don't forget

Disable a tab sparingly, and say why in text people can read, as in What you need to do.

Choosing the tab from your own code

Use it when something else on the page must select a tab or read which one is selected. You hold the value and change it from onValueChange, which only reports what the user did.

Building permit application for 4 Strand Road. The case awaits review.

Selected tab: Details

Props in this example:

Information: Did you know?

Tabs never change the URL, so Back leaves the page. If each part needs its own address, use Navigation.

A panel that starts with a link or a field

A panel is a Tab stop (a place where the Tab key stops, set with tabindex="0"), so keyboard users can reach a panel that holds only text and scroll it. When its first content is a link or a field, pass tabIndex={-1} so Tab goes straight to that link or field and the panel isn’t an extra stop.

Props in this example:

Accessibility#

What it does for you#

  • APG pattern: Tabs, with the keyboard practice for a composite widget (one Tab stop, a roving tabindex).
  • Deviations: two, both approved by the maintainer. (1) A panel has tabindex="0" by default. APG sets it only when the panel has no focusable content, and a headless panel cannot know its first content without scanning the DOM. One redundant Tab stop is the lesser evil: no default would strand keyboard users on a panel of scrolling text (2.1.1). A panel that starts with a focusable element passes tabIndex={-1}. (2) A disabled tab stays focusable (aria-disabled="true", as the project's keyboard rules say for items of a composite widget), and automatic activation never selects it, so focus and selection can differ. APG is silent on both.
  • Native elements used: <button type="button"> for a tab, <div> for the root, the list and a panel. The roles are explicit: tablist, tab and tabpanel.

Tabs show one panel of content at a time, with a list of tabs to switch between them: a keyboard user passes the tab list with one Tab press, moves between the tabs with the arrow keys and reaches the selected panel with the next Tab, and a screen reader user usually hears "tab, 2 of 3, selected". Tabs change content on the same page. For links to other pages, use Navigation (kv-navigation--horizontal): a tab never navigates.

PartElement / roleARIANotes
Tabs.Root<div>none. data-orientationOwns the selected value. No role: it is a plain container for the list and the panels
Tabs.List<div> → tablistaria-label or aria-labelledby (yours). aria-orientation="vertical" only when verticalName it when a page has more than one. Horizontal is the default of the role, so aria-orientation is only set when vertical. Owns the arrow keys, Home and End
Tabs.Tab<button type="button"> → tabaria-selected (true or false, always set), aria-controls (its panel's id), aria-disabled="true" when disabledNever a native disabled: a disabled tab stays focusable. Its name is its text. data-selected and data-disabled for the theme.
Tabs.Panel<div> → tabpanelaria-labelledby (its tab's id), hidden while it isn't selected, tabindex="0"Named by its tab. Always rendered, with hidden while it isn't selected, so every aria-controls and aria-labelledby resolves. Not inert, not unmounted. A tabIndex you pass wins: -1 for a panel that starts with a focusable element. data-selected for the theme
every tab–tabindex="0" on one, -1 on the othersRoving tabindex. DOM focus is on the tab, never aria-activedescendant. The one tab with 0 is the focused tab while focus is in the list, and the selected tab otherwise. Test: tabs.test.tsx › roving tabindex
the ids–id on every tab and panelFrom the root's useId() and the tab's value, each character outside [A-Za-z0-9-] escaped as _<hex code point>_, so ids are unique and an IDREF has no space. Not an index: a panel needs its tab's id on the first server render, before anything registers, and an index breaks when the tabs reorder. Test: tabs-ids.test.ts, tabs.test.tsx › every tab controls its panel and every panel is named by its tab, for values with spaces and punctuation too

Tabs have no strings of their own: every name is the consumer's, from their translations. They announce nothing: selection is in aria-selected, and a screen reader says "tab, 2 of 3, selected" from the roles.

One of value and defaultValue is required (by the types). There is no "first tab" fallback, because the tabs register after the server render, and the server markup must already say which tab is selected and which is the Tab stop. A value that no tab has leaves no Tab stop and shows no panel, so a dev warning fires (tabs-value-without-tab:<value>, 2.1.1, 4.1.2). So does a tab with no panel, or a panel with no tab (tabs-unpaired:<value>, 4.1.2).

It announces nothing.

What you need to do#

  • Name the list with aria-label from your translations, or aria-labelledby, when a page has more than one set of tabs, so a screen reader user can tell them apart.
  • Keep the labels short, and in the same register ("Details", "History"). A long label wraps inside its tab and never scrolls the list sideways (1.4.10).
  • Give every tab a panel and every panel a tab, with the same value, and give value or defaultValue the value of a tab. Render every Tabs.Panel: do not unmount the ones that aren't selected, because the tab's aria-controls would point at nothing. For lazy content, render a panel's children only while its tab is selected, using your own value.
  • Use activationMode="manual" only for slow panels. Otherwise keep the default.
  • Pass tabIndex={-1} to a panel that starts with a focusable element (a link, a button or a field), so Tab goes straight to it and the panel is not a redundant stop. A panel with only text keeps its tabindex="0".
  • Use a tab only for content on the same page. For links to other pages, or a trail through a service, use Navigation (kv-navigation--horizontal) or a stepper. A tab never changes the URL.
  • Disable a tab sparingly, and say why where users can read it: a disabled tab is dimmed and read as unavailable, and it stays focusable so it can be found.
  • The panel is not a landmark and has no heading of its own. The page's heading outline continues inside it: don't skip levels.
  • Don't put interactive content in a tab. A close button or a menu inside a tab is not supported, and the tab's click would also fire for it.

Warning: Don't forget

Name the tab list with aria-label or aria-labelledby when a page has more than one set of tabs, so a screen reader user can tell them apart (4.1.2 Name, Role, Value, level A). Every example on this page does it.

Visual and modes#

  • Focus indicator: a tab and a panel each show the same focus ring as a link: at least 2px thick, set off from the element (2.4.7 Focus Visible, level AA; 2.4.13 Focus Appearance, level AAA).
  • Target size: a tab is at least 24 × 24 CSS px, and taller in the default theme (2.5.8). A story asserts that threshold, also in the compact density.
  • State without colour: the selected tab is bold and has a straight bar at its edge, not colour alone (1.4.1, 1.4.11). A disabled tab is dimmed text and aria-disabled.
  • forced-colors behaviour (Windows contrast themes): the bar, the line under the list and the text use the system's colours, a disabled tab uses the system's disabled-text colour, and the focus ring uses its highlight colour. The forced-colours sweep checks that every tab keeps a visible boundary and shows a focus indicator when it has focus (1.4.11).
  • reduced-motion behaviour: nothing animates, and the reduced-motion sweep checks that no animation keeps looping (2.3.3).
  • Reflow: the list wraps, row by row, and never scrolls sideways. A long label wraps inside its tab. A vertical set stacks below 40rem (1.4.10). The reflow sweep runs every story at 320 CSS px and fails on horizontal overflow, and one story uses a long Finnish text. The text-spacing sweep applies the 1.4.12 spacing and fails on a box that clips its content.
  • Right to left: logical properties only, and the arrows flip (above). The bar of a vertical tab is at the inline end.

Focus management#

  • Initial focus: not moved. Tab enters at the selected tab.
  • Trap: no. Tab and Shift+Tab always leave the list (2.1.2).
  • Restore to: not applicable. Selecting a tab never moves focus: it stays on the tab, and the panel is the next Tab stop.
  • Never obscured by: the tab list is not sticky by default. A sticky header above the tabs is the page's to allow for with scroll-padding (2.4.11).

Information: Did you know?

Choosing a tab never moves focus. It stays on the tab, and the next Tab press goes to the panel. A keyboard user moves from the tabs to the panel with one Tab press.

WCAG success criteria#

  • 1.3.1 Info and Relationships (A): tablist, tab and tabpanel, a tab and its panel tied by aria-controls and aria-labelledby.
  • 1.4.1 Use of Color (A), 1.4.11 Non-text Contrast (AA): the selected tab is a bar and a weight, and the default theme's bar and focus ring are held to a 3:1 contrast in every theme.
  • 1.4.10 Reflow (AA): the list wraps and never scrolls sideways.
  • 2.1.1 Keyboard (A), 2.1.2 No Keyboard Trap (A): every tab and panel is reachable, and Tab always leaves.
  • 2.4.3 Focus Order (A), 2.4.7 Focus Visible (AA): one Tab stop, DOM order, the panel next.
  • 2.5.8 Target Size (Minimum) (AA): a tab is at least 24 × 24 CSS px.
  • 4.1.2 Name, Role, Value (A): roles, names, aria-selected, aria-disabled.

Keyboard#

Focus
roving tabindex. The one Tab stop is the selected tab, or the tab that has focus while focus is in the list
Selection follows focus
yes, with activationMode="automatic" (the default), except onto a disabled tab. No with activationMode="manual": Enter or Space selects
Arrows wrap
yes
Shortcuts
none

The tab list is one Tab stop. The selected tab has tabindex="0" and the others -1. While focus is in the list, the tab that has focus is the stop instead (the selected one when focus and selection agree), so Tab from a tab that is not selected leaves the list for the panel and does not move on to the selected tab. When focus leaves the list the selected tab is the stop again. The server render is already right: the selected tab 0, the others -1. Arrow keys flip in right-to-left text, read from the list's computed direction, so a dir on a container works without a provider. Tabs are ordered by their place in the page.

KeyWhereWhat happens
Tabbefore the listMoves focus to the selected tab, the one Tab stop of the list
Test: tabs.test.tsx › Tab enters at the selected tab
Tabon a tabLeaves the list for the selected panel, also from a tab that is not selected. A panel has tabindex="0". A panel that starts with a focusable element sets tabIndex={-1}, and Tab goes straight to that element
Test: tabs.test.tsx › Tab leaves the tab list for the panel
Shift+Tabin the panelBack to the selected tab, and the next Shift+Tab leaves the list
Test: tabs.test.tsx › Shift+Tab returns to the selected tab and leaves the list
ArrowRight / ArrowLefttab list, horizontalNext and previous tab, wrapping from the last to the first and back. Automatic activation selects the tab it moves to
Test: tabs.test.tsx › ArrowRight and ArrowLeft move, wrap and select
ArrowLeft / ArrowRighttab list, horizontal, right to leftThe arrows flip: ArrowLeft is the next tab and ArrowRight the previous
Test: tabs.test.tsx › right to left: the arrows flip
ArrowDown / ArrowUptab list, verticalNext and previous tab, wrapping. They don't flip in right-to-left text. ArrowLeft and ArrowRight are not taken in a vertical list, and ArrowDown and ArrowUp are not taken in a horizontal one
Test: tabs.test.tsx › vertical: ArrowDown and ArrowUp move
Home / Endtab listFirst and last tab. Automatic activation selects it
Test: tabs.test.tsx › Home and End go to the ends
Arrows, Enter, Spacemanual activationThe arrows, Home and End move focus only: the selection stays where it was. Enter or Space on the focused tab selects it, through the native click
Test: tabs.test.tsx › manual activation: arrows move focus, Enter and Space select
Arrows, Enter, Spacedisabled tabA disabled tab is reachable with the arrows and read as unavailable (aria-disabled). Automatic activation never selects it, so focus can be on a tab that is not selected. Enter, Space and a click do nothing
Test: tabs.test.tsx › a disabled tab is reachable and never selected
(with a modifier key)tab listNot taken: Control+ArrowRight, Alt+ArrowRight, Shift+End and the like stay the browser's and the page's
Test: tabs.test.tsx › keys with Control, Alt, Meta or Shift are left alone
(a key already handled)tabA key that a tab or your own handler already handled (defaultPrevented) is not taken again
Test: tabs.test.tsx › a key that was already handled is not taken again

Escape and the typed characters are not handled, and there is no typeahead (APG Tabs has none). A key from an element that isn't a tab is never the list's: a nested set of tabs in a panel keeps its own arrows (tabs.test.tsx › a nested Tabs keeps its own arrows).

Why these choices. Arrows wrap because APG's example does, and Home and End are implemented because they cost nothing. Automatic activation is the default, as APG advises when the panels show at once. Use activationMode="manual" when showing a panel is slow (it loads data, or is large): then arrowing through the tabs doesn't trigger it for each tab. Focus and selection can differ on a disabled tab in automatic mode, and in manual mode always: the contract says so, and the tab with focus is always the Tab stop while focus is in the list.

API reference#

import { Tabs, useTabs } from '@kvirn-ui/react'

Tabs.Root

Renders <div> with no role: a plain container for the list and the panels. It owns the selected value, takes every attribute of a <div> and passes ref to it. The default theme is described on Theming.

PropTypeDefaultDescription
valuestring–Controlled: the value of the selected tab. Pair it with onValueChange and change it yourself. One of value and defaultValue is required.
defaultValuestring–Uncontrolled: the value of the tab that is selected to begin with. There is no first-tab fallback.
onValueChange(value: string, details: TabsChangeDetails) => void–Called when the user selects a tab, with { reason: "press" | "arrow-key" | "home-end-key", event }. Never for a disabled tab or the tab that is already selected.
activationMode'automatic' | 'manual''automatic'Automatic: the arrow keys, Home and End select the tab they move to. Manual: they only move focus, and Enter or Space selects.
orientation'horizontal' | 'vertical''horizontal'The axis of the arrow keys: Left and Right (flipped in right-to-left text), or Down and Up when vertical.
Class or attributeValuesMeaning
kv-tabsalwaysThe part class. The default theme styles it.
data-orientation"horizontal" or "vertical"The orientation. The default theme lays a vertical set out beside its panels.

Tabs.List

Renders <div> with the role tablist. It owns the arrow keys, Home and End. Name it with aria-label or aria-labelledby.

Class or attributeValuesMeaning
kv-tabs-listalwaysThe part class.
aria-orientation"vertical" or absentSet only when vertical. Horizontal is the default of the tablist role.

Tabs.Tab

Renders <button type="button"> with the role tab. Its name is its text.

PropTypeDefaultDescription
valuestringRequiredThe tab’s value: ties it to the Tabs.Panel with the same value.
disabledbooleanfalseUnavailable: aria-disabled="true", and it can’t be selected. The tab stays focusable so users can find it. Never a native disabled.
onClickMouseEventHandler<HTMLButtonElement>–Called after the tab is selected, and never while the tab is disabled.
Class or attributeValuesMeaning
kv-tabs-tabalwaysThe part class.
aria-selected"true" or "false"Always set. Says which tab is selected.
aria-disabled"true" or absentSet on a disabled tab. Not passed directly.
data-selectedpresent or absentThe tab is selected.
data-disabledpresent or absentThe tab is disabled.

Tabs.Panel

Renders <div> with the role tabpanel, named by its tab. Every panel is rendered, and the ones that aren’t selected are hidden.

PropTypeDefaultDescription
valuestringRequiredThe value of the tab that shows this panel.
tabIndexnumber0Pass -1 when the panel starts with a focusable element, so Tab goes straight to it.
Class or attributeValuesMeaning
kv-tabs-panelalwaysThe part class.
hiddenpresent or absentPresent while the panel isn’t selected. Every panel stays rendered.
data-selectedpresent or absentThe panel is shown.

useTabs

Options

PropTypeDefaultDescription
valuestring–Controlled: the value of the selected tab. Pair it with onValueChange and change it yourself. One of value and defaultValue is required.
defaultValuestring–Uncontrolled: the value of the tab that is selected to begin with. There is no first-tab fallback.
onValueChange(value: string, details: TabsChangeDetails) => void–Called when the user selects a tab, with { reason: "press" | "arrow-key" | "home-end-key", event }. Never for a disabled tab or the tab that is already selected.
activationMode'automatic' | 'manual''automatic'Automatic: the arrow keys, Home and End select the tab they move to. Manual: they only move focus, and Enter or Space selects.
orientation'horizontal' | 'vertical''horizontal'The axis of the arrow keys: Left and Right (flipped in right-to-left text), or Down and Up when vertical.

Result

PropTypeDefaultDescription
valuestring–The value of the selected tab.
orientation'horizontal' | 'vertical'–The orientation in use.
activationMode'automatic' | 'manual'–The activation mode in use.
rootPropsTabsRootPartProps–Spread on the root <div>: class and data-orientation.
listPropsTabsListPartProps–Spread on the list <div>: class, role, the keys and the blur handler.
getTabProps(value: string, options?: { disabled?: boolean; onClick?: MouseEventHandler }) => TabsTabPartProps–The props of one <button>: id, role, tabindex, aria-selected, aria-controls, handlers and a ref.
getPanelProps(value: string) => TabsPanelPartProps–The props of one <div>: id, role, tabindex, aria-labelledby, hidden and a ref.

Known issues#

  • Focus and selection can differ. A disabled tab takes focus and is not selected in automatic mode, and in manual mode focus moves without selecting. The tab with focus is the Tab stop while focus is in the list, so Tab always leaves the list for the panel.
  • One redundant Tab stop on a panel whose first content is focusable, unless the consumer passes tabIndex={-1}.
  • Hidden panels still mount. Every panel is rendered with hidden: a heavy panel should render its children only while its tab is selected, using your own value.
  • A tab that unmounts while it has focus leaves no Tab stop in the list until focus moves. Closable tabs are not supported.
  • Before hydration the tabs are already right (the selected tab 0, the others -1, every panel rendered), but the arrow keys need JavaScript.

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apasstabs.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apasstabs.test.tsx › is a tablist of tabs and panels with the names the consumer gives, and warns about nothing; expectNoA11yViolations
Keyboard contract rows (Vitest browser)passn/apass11 rows, each names a test (tabs.test.tsx › Tab enters at the selected tab)
Focus order and managementpassn/apasstabs.test.tsx asserts focus (toHaveFocus); first rule: Tab enters at the selected tab
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

What to check#

With a screen reader and the keyboard, try:

  • Tab into the list: focus lands on the selected tab.
  • Right and Left move between the tabs, wrap, and change the panel.
  • Tab from a tab goes to the panel.
  • With activationMode="manual" the arrows move focus and Enter selects.
  • A disabled tab is reached and read as unavailable.