Skip to content

Tabs ​

Several panels in one place, one shown at a time, chosen from a row of tabs.

A trigger and its panel find each other through the value they share: the ids are minted once per value by the context, so neither part has to know the other exists, and they can be built in any order. Selection follows focus — arrowing to a tab shows its panel — which is the right default when panels are already rendered and switching costs nothing; activateOnFocus: false is there for panels that fetch on display, where arrowing past three tabs would fire three requests.

Hidden panels stay mounted, so a half-filled form or a scroll position inside one survives a trip to another tab. They carry hidden, which keeps them out of the layout and the accessibility tree without removing them from the DOM.

sh
ream nebula:add tabs

Molecule · molecules/Tabs.ts · no dependencies

Usage ​

ts
import { html } from '@c9up/aurora'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@c9up/nebula'

Tabs({
  defaultValue: 'account',
  children: () => html`
    ${TabsList({ children: html`
      ${TabsTrigger({ value: 'account', children: 'Account' })}
      ${TabsTrigger({ value: 'password', children: 'Password' })}
    ` })}
    ${TabsContent({ value: 'account', children: 'Your profile.' })}
    ${TabsContent({ value: 'password', children: 'Change your password.' })}
  `,
})

Tabs takes its children as () =>, because its parts read its context — see composition. TabsList provides nothing, so its children can be a plain value: they are built inside the Tabs thunk anyway.

Examples ​

From data ​

sections is a signal, so the triggers and the panels are each built in an accessor: the Tabs thunk runs once, and a list read directly in it would never change again.

ts
Tabs({
  children: () => html`
    ${TabsList({
      children: () =>
        sections().map((section) =>
          TabsTrigger({ value: section.id, children: section.title, disabled: section.locked }),
        ),
    })}
    ${() => sections().map((section) => TabsContent({ value: section.id, children: section.body }))}
  `,
})

With no defaultValue, the first enabled trigger built is selected: a disabled tab selected by default would show a panel that nothing can navigate back to.

Controlled ​

Pass value and the app owns the selection. A signal can be both the value and the change handler, and then anything in the page can switch tabs:

ts
import { signal } from '@c9up/aurora'
import { Button } from '@c9up/nebula'

const tab = signal('overview')

Tabs({
  value: tab,
  onValueChange: tab,
  children: () => html`
    ${TabsList({ children: html`
      ${TabsTrigger({ value: 'overview', children: 'Overview' })}
      ${TabsTrigger({ value: 'activity', children: 'Activity' })}
    ` })}
    ${TabsContent({ value: 'overview', children: 'Twelve open issues.' })}
    ${TabsContent({ value: 'activity', children: 'Three commits today.' })}
  `,
})

Button({ variant: 'link', children: 'See the activity', onClick: () => tab('activity') })

value wins whenever it reads as a string. onValueChange is called only when the selection actually changes — clicking the tab already selected says nothing.

Underlined tabs ​

variant: 'line' on the list drops the filled surface for a rule under the active tab:

ts
Tabs({
  defaultValue: 'code',
  children: () => html`
    ${TabsList({ variant: 'line', children: html`
      ${TabsTrigger({ value: 'code', children: 'Code' })}
      ${TabsTrigger({ value: 'issues', children: 'Issues' })}
    ` })}
    ${TabsContent({ value: 'code', children: 'src/, tests/, README.md' })}
    ${TabsContent({ value: 'issues', children: 'No open issues.' })}
  `,
})

Vertical ​

ts
Tabs({
  orientation: 'vertical',
  defaultValue: 'general',
  children: () => html`
    ${TabsList({ children: html`
      ${TabsTrigger({ value: 'general', children: 'General' })}
      ${TabsTrigger({ value: 'members', children: 'Members' })}
      ${TabsTrigger({ value: 'danger', children: 'Danger zone' })}
    ` })}
    ${TabsContent({ value: 'general', children: 'Name and avatar.' })}
    ${TabsContent({ value: 'members', children: 'Four members.' })}
    ${TabsContent({ value: 'danger', children: 'Delete the project.' })}
  `,
})

The list stacks beside the panels, and the arrows that move between tabs become ArrowUp and ArrowDown.

Panels that load on display ​

ts
Tabs({
  activateOnFocus: false,
  defaultValue: 'sales',
  children: () => html`
    ${TabsList({ children: html`
      ${TabsTrigger({ value: 'sales', children: 'Sales' })}
      ${TabsTrigger({ value: 'refunds', children: 'Refunds' })}
    ` })}
    ${TabsContent({ value: 'sales', children: ReportPanel({ kind: 'sales' }) })}
    ${TabsContent({ value: 'refunds', children: ReportPanel({ kind: 'refunds' }) })}
  `,
})

The arrows now only move focus; Enter, Space or a click selects. ReportPanel stands for your own component.

API ​

Tabs ​

PropTypeDefaultDescription
defaultValuestringthe first enabled triggerSelected at first render when uncontrolled.
valueReactive<string | undefined>—Controlled selection. When it reads as a string, it wins over the internal one.
orientation'horizontal' | 'vertical''horizontal'The list's direction, and which arrows move between tabs. Read once, at setup.
activateOnFocusbooleantrueSelecting follows focus. false waits for Enter, Space or a click.
onValueChange(value: string) => void—Called when the selection changes.
classReactive<string>—Merged into the root's classes.
childrenParts—The list and the panels, built in () =>.

TabsList ​

PropTypeDefaultDescription
variant'default' | 'line''default'default gives the active tab a surface; line underlines it. Read once, at setup.
childrenSlot—The TabsTriggers.
classReactive<string>—Merged last into the list's classes.

TabsTrigger ​

PropTypeDefaultDescription
valuestring—The panel it shows.
childrenSlot—The tab's label. An SVG inside is sized to 16 px.
disabledbooleanfalseDisables the tab; the arrows skip it.
classReactive<string>—Merged into the button's classes.

TabsContent ​

PropTypeDefaultDescription
valuestring—The tab that shows it.
childrenSlot—The panel's content.
classReactive<string>—Merged into the panel's classes.

tabsListVariants ​

tabsListVariants({ variant?, class? }): string — the classes a TabsList carries, for a list you render yourself.

Accessibility ​

The WAI-ARIA tabs pattern: the list is role="tablist" with aria-orientation; each trigger is role="tab" with aria-selected and aria-controls pointing at its panel; each panel is role="tabpanel" with aria-labelledby pointing back at its tab, and tabindex="0" so Tab from the list reaches the panel even when it holds nothing focusable. data-state="active" or "inactive" is set on each tab for styling.

The list is one tab stop:

KeyAction
TabEnters the list on the tab last reached with the arrows — the first enabled one to begin with, even when another is selected — then moves on to the panel.
ArrowRight / ArrowLeftNext / previous tab, when horizontal. Mirrored in a right-to-left page. Selects it unless activateOnFocus: false.
ArrowDown / ArrowUpNext / previous tab, when vertical. Selects it unless activateOnFocus: false.
Home / EndFirst / last tab. Selects it unless activateOnFocus: false.
Enter / SpaceSelects the focused tab.

The arrows wrap from the last tab to the first and back, and skip disabled tabs. The direction is read from the page's computed style — see right to left.