tabs
tabs is a row of tabs over a set of panels. Each tab shows one panel, and only one panel shows
at a time.
onChange called 0 time(s)
import { tabs } from '@markless/ui';export default function Account() @{ <tabs.root class="tabs" value="overview"> <tabs.list class="list" aria-label="Account"> <tabs.trigger class="tab" value="overview">Overview</tabs.trigger> <tabs.trigger class="tab" value="billing">Billing</tabs.trigger> <tabs.trigger class="tab" value="team" disabled>Team</tabs.trigger> <tabs.trigger class="tab" value="alerts">Alerts</tabs.trigger> </tabs.list> <tabs.content class="panel" value="overview" aria-label="Overview"> Three orders this month. One is on its way. </tabs.content> <tabs.content class="panel" value="billing" aria-label="Billing"> Your next invoice is due on the 1st. </tabs.content> <tabs.content class="panel" value="team" aria-label="Team"> Two people can see this account. </tabs.content> <tabs.content class="panel" value="alerts" aria-label="Alerts"> We email you when an order ships. </tabs.content> </tabs.root>;}
.tabs { display: grid; gap: 0.9em; width: min(31rem, 100%); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.tabs[ui-vertical] { grid-template-columns: auto minmax(0, 1fr); align-items: start;}.list { display: flex; flex-wrap: wrap; gap: 0.4em;}.list[ui-vertical] { flex-direction: column;}.tab { padding: 0.45em 0.9em; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif; text-align: start; cursor: pointer; transition: background 140ms ease;}.tab:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.tab[ui-selected] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.tab:disabled { opacity: 0.55; cursor: not-allowed;}.tab:disabled:hover { background: var(--paper);}.tab:focus-visible,.panel:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px;}.panel { min-height: 4.5em; padding: 0.85em 1em; border: 1px solid var(--ink); border-radius: 6px;}.panel[hidden] { display: none;}@media (prefers-reduced-motion: reduce) { .tab { transition: none; }}
Click a tab, then press the Right arrow. Focus moves to the next tab and shows its panel. The arrows walk past Team, because it is disabled.
Switch off SelectOnFocus, and the arrows move focus without showing the panel. Then Enter shows it.
A tab and a panel with the same value are a pair. The family never matches them by position.
The root's value names the tab that shows first. If you leave it out, no tab shows.
When to use it
Use tabs when a person switches between views of the same thing, such as the pages of an
account. If several sections can be open at once, use accordion instead.
Parts
The tabs themselves; the list, triggers and panels go inside it.
tabs.rootholds which tab shows, the arrow-key axis, and the focus rules. It renders a<div>.tabs.listis the row of tabs, withrole="tablist". It also writesaria-orientation.tabs.triggeris one tab. It is a<button>withrole="tab", and itsvalueis required.tabs.contentis one panel, withrole="tabpanel". Itsvalueis required.
The whole row of tabs is one Tab stop. Only the showing tab has tabindex="0". The showing
panel is a Tab stop too, so Tab from the tabs lands on the panel.
Every panel stays in the page. A panel that is not showing gets hidden. So what a person typed
in a panel, and its scroll position, are still there when they come back.
Styling hooks
| Attribute | On | Present when |
|---|---|---|
ui-selected |
trigger, content | This tab or panel is the one showing. |
ui-vertical |
root, list, trigger | orientation is vertical. |
A horizontal set writes no orientation attribute. Only the vertical one does.
Keyboard
The family's tests check each of these rows.
| Key | What it does |
|---|---|
| Right arrow or Left arrow | Moves to the next or the previous tab. In a vertical set, use Down and Up. |
| Home or End | Moves to the first or the last tab. |
| Enter or Space | Shows the focused tab. You need this only when selectOnFocus is false. |
By default a tab shows as soon as focus reaches it. Set selectOnFocus={false} and the arrows move
focus only. Then Enter or Space shows the tab.
The arrows stop at the ends. Set loop and they wrap around from the last tab to the first.
The arrows skip a disabled tab. An arrow pressed with Alt, Ctrl or Cmd is left to the browser.
Gotchas
API reference
tabs.root
valuestringThe value of the tab that is showing. Omit it and no tab shows: a tab is named by itsvalue, and nothing counts positions for you.Default''.orientation'horizontal' | 'vertical'Which axis the arrow keys walk. Omit it and the tabs run left to right.Default'horizontal'.loopbooleanArrow past the last tab and land on the first. Omit it and the ends stop.Defaultfalse.selectOnFocusbooleanArrowing to a tab shows it. Turn it off and arrowing only moves focus; Enter or Space shows it.Defaulttrue.onChange(value: string) => voidCalled with the new value when a person changes tab. Omit it and the tabs still change; the call site simply does nothing.
tabs.content
valuestringRequired.Names the panel. The trigger part with the same value shows it. Required.
tabs.list
No props of its own.
tabs.trigger
valuestringRequired.Names the tab. The content part with the same value is the panel it shows. Required.