Tabs
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
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.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | null | — | The selected tab's value, when controlled; null selects none. Pair it with onValueChange. |
defaultValue | string | — | 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.
| Prop | Type | Default | Description |
|---|---|---|---|
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>.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | — | Identifies the tab in the Root's value, and its Panel. It is part of both ids, so it must not contain spaces. |
disabled | boolean | false | Whether 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.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | — | The value of the tab that shows this Panel. |
render | (props) => JSX.Element | — | Renders the panel with your own component. Read once. |
Keyboard
| Keys | Action |
|---|---|
| Tab | Moves focus into the List, to the selected tab, or from the List to the selected Panel. |
| ArrowRight | Moves focus to the next tab, or the first from the last. Horizontal lists; the previous tab in right-to-left text. |
| ArrowLeft | Moves focus to the previous tab, or the last from the first. Horizontal lists; the next tab in right-to-left text. |
| ArrowDown | Moves focus to the next tab, or the first from the last. Vertical lists. |
| ArrowUp | Moves focus to the previous tab, or the last from the first. Vertical lists. |
| Home | Moves focus to the first tab. |
| End | Moves focus to the last tab. |
| Space | Selects the focused tab. With automatic activation, focus has already selected it. |
| Enter | Selects the focused tab. With automatic activation, focus has already selected it. |