Menhir

Tabs

View as Markdown

Layered sections of content, one shown at a time, each chosen by its tab: the WAI-ARIA tabs pattern. The List is one tab stop, and arrow keys move between the tabs. By default a tab is selected as it receives focus; with manual activation, by click, Enter or Space. A Panel that is not selected has the hidden attribute, so its content stays in the page, keeping its state, but is not shown or read out.

Size
2.0 kB

Example

Styling
Your name, email address and language.
TabsExample.tsx
import { Tabs } from "menhir/tabs";
import { For } from "solid-js";

const sections = [
  {
    value: "account",
    name: "Account",
    content: "Your name, email address and language.",
  },
  {
    value: "billing",
    name: "Billing",
    content: "Your plan, payment method and invoices.",
  },
  {
    value: "security",
    name: "Security",
    content: "Your password, passkeys and signed-in devices.",
  },
];

export default function TabsExample() {
  return (
    <Tabs.Root class="w-80 text-sm">
      <Tabs.List
        aria-label="Settings"
        class="flex border-b border-stone-300 dark:border-stone-700"
      >
        <For each={sections}>
          {(section) => (
            <Tabs.Tab
              value={section.value}
              class="-mb-px cursor-pointer border-b-2 border-transparent px-3 py-2 text-stone-600 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-selected:border-current data-selected:font-semibold data-selected:text-inherit dark:text-stone-400"
            >
              {section.name}
            </Tabs.Tab>
          )}
        </For>
      </Tabs.List>
      <For each={sections}>
        {(section) => (
          <Tabs.Panel
            value={section.value}
            class="py-3 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500"
          >
            {section.content}
          </Tabs.Panel>
        )}
      </For>
    </Tabs.Root>
  );
}

Anatomy

<Tabs.Root>
  <Tabs.List>
    <Tabs.Tab />
  </Tabs.List>
  <Tabs.Panel />
</Tabs.Root>

API reference

Root

<div> that holds which tab is selected. It has data-horizontal or data-vertical.

PropTypeDefaultDescription
valuestring | null—The selected tab's value, when controlled; null selects none. Pair it with onValueChange.
defaultValuestring—The value of the tab selected at first, when uncontrolled. Without one, the first enabled tab is selected.
onValueChange(value: string) => void—Called with the new value when the user selects a tab. A controlled Root changes only if value does.
orientation"horizontal" | "vertical""horizontal"Whether the tabs are side by side, moved between with Left and Right, or stacked, with Up and Down.
activation"automatic" | "manual""automatic"Whether a tab is selected when it receives focus, or only by click, Enter or Space. Choose manual when a Panel takes time to show, such as one that fetches its content.
render(props) => JSX.Element—Renders the root with your own component. Read once.

List

<div role="tablist">, one tab stop: Tab into it focuses the selected tab. Name it with aria-label or aria-labelledby. It has aria-orientation="vertical" when vertical, and data-horizontal or data-vertical.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the list with your own component. Read once.

Tab

<button type="button" role="tab" aria-selected aria-controls>, with data-selected or data-unselected, and data-disabled. Name it with its content. Its id is generated unless you give one. Its render function must render a <button>.

PropTypeDefaultDescription
valuestring—Identifies the tab in the Root's value, and its Panel. It is part of both ids, so it must not contain spaces.
disabledbooleanfalseWhether the tab cannot be selected. It stays focusable, with aria-disabled, so that keyboard and screen-reader users still find it.
render(props) => JSX.Element—Renders the tab with your own button component. Read once.

Panel

<div role="tabpanel"> named by its tab, hidden unless the tab is selected, with data-selected or data-unselected. It is a tab stop, for content with nothing focusable; when its content starts with a focusable element, pass tabindex={undefined}. Its id is generated, so that the tab can name it in the server's HTML too, and it takes no id of its own.

PropTypeDefaultDescription
valuestring—The value of the tab that shows this Panel.
render(props) => JSX.Element—Renders the panel with your own component. Read once.

Keyboard

KeysAction
TabMoves focus into the List, to the selected tab, or from the List to the selected Panel.
ArrowRightMoves focus to the next tab, or the first from the last. Horizontal lists; the previous tab in right-to-left text.
ArrowLeftMoves focus to the previous tab, or the last from the first. Horizontal lists; the next tab in right-to-left text.
ArrowDownMoves focus to the next tab, or the first from the last. Vertical lists.
ArrowUpMoves focus to the previous tab, or the last from the first. Vertical lists.
HomeMoves focus to the first tab.
EndMoves focus to the last tab.
SpaceSelects the focused tab. With automatic activation, focus has already selected it.
EnterSelects the focused tab. With automatic activation, focus has already selected it.