Skip to the content
human-kit design
42 — Navigation

Tabs

Sections of one place, one in view at a time, picked from a strip or a rail.

Last visit on 12 March, next one in two weeks.

Installation

pnpm dlx @human-kit/design add tabs
npx @human-kit/design add tabs
yarn dlx @human-kit/design add tabs
bunx @human-kit/design add tabs

The command copies the files to src/lib/components/ui/tabs. It also installs the packages that the files import.

Usage

<script lang="ts">
	import * as Tabs from '$lib/components/ui/tabs';
</script>

<Tabs.Root value="summary">
	<Tabs.List aria-label="Patient record">
		<Tabs.Tab value="summary">Summary</Tabs.Tab>
		<Tabs.Tab value="documents">Documents</Tabs.Tab>
	</Tabs.List>
	<Tabs.Panel value="summary">Last visit on 12 March.</Tabs.Panel>
	<Tabs.Panel value="documents">Twelve documents, all signed.</Tabs.Panel>
</Tabs.Root>

Examples

Open a tab from elsewhere

bind:value holds the open tab, so the page can read it and set it: a button in one panel that sends the reader to another, or a tab kept in the URL.

Two documents are waiting for a signature.

Guidelines

  • Use line for the sections of a page or a card: titles over a rule, the open one underlined.
  • Use segment to switch between views of the same thing, such as a list and a calendar. Two to four options, each a word, or an icon and a word.
  • Keep the labels short and of one kind: nouns for sections, not a mix of nouns and verbs.
  • Give count when the reader picks a tab by how much it holds. Zero is left out, so an empty tab stays quiet.
  • Do not use tabs for steps that go in order, or for content the reader has to compare: they show one panel at a time.
  • The list does not scroll. A strip too long for its place is better split.

Accessibility

  • The list is a role="tablist", each tab a role="tab" button with aria-selected and aria-controls, and each panel a role="tabpanel" named by its tab.
  • The list is one tab stop. The arrow keys move between the tabs and wrap at the ends; Home and End go to the first and the last enabled one.
  • With keyboardActivation="automatic" an arrow opens the tab it lands on. Use manual when a panel is slow to draw: the arrows only move the focus, and Enter or Space opens.
  • Tab from the list goes into the open panel, so its content is one key away.
  • Name the list with aria-label when nothing on the page already says what the tabs split.
  • A disabled tab is skipped by the arrow keys.

API reference

Tabs.Root

PropTypeDefault
variant'line' | 'segment'

Titles over a rule, or a few options on a rail. The parts read it from here.

'line'
valuestring | number | null

The value of the open tab. Bindable.

—
defaultValuestring | number | null

The open tab at the start, when you give no value. With neither, the first enabled tab opens.

—
onChange(value: string | number | null) => void

Called when another tab opens.

—
controlledValueboolean

Stops the write-back to value: the tab changes only when the parent sends the new value down from onChange.

false
keyboardActivation'automatic' | 'manual'

Whether an arrow key opens the tab it lands on, or only moves the focus.

'automatic'
disabledboolean

Turns off every tab.

false
disabledKeysIterable<string | number>

The values of the tabs to turn off, as disabled on each would.

—
elementHTMLDivElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>.

Tabs.List

PropTypeDefault
elementHTMLDivElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>. It draws the line, or the chip, that slides to the open tab.

Tabs.Tab

PropTypeDefault
valuestring | number

Which panel the tab opens. Required.

—
iconComponent

An icon before the label.

—
countnumber

How many items the panel holds, after the label. Zero is left out.

—
disabledboolean

Turns the tab off; the arrow keys skip it.

false
elementHTMLButtonElement | null

The rendered element. Bindable.

—

Plus every attribute of a native <button>, but type, role and aria-selected, which the tab sets.

Tabs.Panel

PropTypeDefault
valuestring | number

The tab that opens this panel. Required.

—
forceMountboolean

Keeps the panel in the DOM while it is closed, hidden and inert, so its state stays: a half-filled form, a scroll position.

false
elementHTMLDivElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>.