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 tabsnpx @human-kit/design add tabsyarn dlx @human-kit/design add tabsbunx @human-kit/design add tabsThe 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
linefor the sections of a page or a card: titles over a rule, the open one underlined. - Use
segmentto 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
countwhen 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 arole="tab"button witharia-selectedandaria-controls, and each panel arole="tabpanel"named by its tab. - The list is one tab stop. The arrow keys move between the tabs and wrap at the ends;
HomeandEndgo to the first and the last enabled one. - With
keyboardActivation="automatic"an arrow opens the tab it lands on. Usemanualwhen a panel is slow to draw: the arrows only move the focus, andEnterorSpaceopens. Tabfrom the list goes into the open panel, so its content is one key away.- Name the list with
aria-labelwhen nothing on the page already says what the tabs split. - A disabled tab is skipped by the arrow keys.
API reference
Tabs.Root
| Prop | Type | Default |
|---|---|---|
variant | 'line' | 'segment' Titles over a rule, or a few options on a rail. The parts read it from here. | 'line' |
value | string | number | null The value of the open tab. Bindable. | — |
defaultValue | string | number | null The open tab at the start, when you give no | — |
onChange | (value: string | number | null) => void Called when another tab opens. | — |
controlledValue | boolean Stops the write-back to | false |
keyboardActivation | 'automatic' | 'manual' Whether an arrow key opens the tab it lands on, or only moves the focus. | 'automatic' |
disabled | boolean Turns off every tab. | false |
disabledKeys | Iterable<string | number> The values of the tabs to turn off, as | — |
element | HTMLDivElement | null The rendered element. Bindable. | — |
Plus every attribute of a <div>.
Tabs.List
| Prop | Type | Default |
|---|---|---|
element | HTMLDivElement | 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
| Prop | Type | Default |
|---|---|---|
value | string | number Which panel the tab opens. Required. | — |
icon | Component An icon before the label. | — |
count | number How many items the panel holds, after the label. Zero is left out. | — |
disabled | boolean Turns the tab off; the arrow keys skip it. | false |
element | HTMLButtonElement | null The rendered element. Bindable. | — |
Plus every attribute of a native <button>, but type, role and aria-selected, which the tab sets.
Tabs.Panel
| Prop | Type | Default |
|---|---|---|
value | string | number The tab that opens this panel. Required. | — |
forceMount | boolean 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 |
element | HTMLDivElement | null The rendered element. Bindable. | — |
Plus every attribute of a <div>.