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)

Three orders this month. One is on its way.
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>;}

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

tabs anatomy
Anatomy perspectiveFlat
Layers
OverviewActivitySettingsYour workspace at a glance.
tabs.root

The tabs themselves; the list, triggers and panels go inside it.

  • tabs.root holds which tab shows, the arrow-key axis, and the focus rules. It renders a <div>.
  • tabs.list is the row of tabs, with role="tablist". It also writes aria-orientation.
  • tabs.trigger is one tab. It is a <button> with role="tab", and its value is required.
  • tabs.content is one panel, with role="tabpanel". Its value is 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

valuestring
The value of the tab that is showing. Omit it and no tab shows: a tab is named by its value, 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'.
loopboolean
Arrow past the last tab and land on the first. Omit it and the ends stop. Default false.
selectOnFocusboolean
Arrowing to a tab shows it. Turn it off and arrowing only moves focus; Enter or Space shows it. Default true.
onChange(value: string) => void
Called with the new value when a person changes tab. Omit it and the tabs still change; the call site simply does nothing.

tabs.content

valuestring
Required. Names the panel. The trigger part with the same value shows it. Required.

tabs.list

No props of its own.

tabs.trigger

valuestring
Required. Names the tab. The content part with the same value is the panel it shows. Required.