Tabs
Views that share one place, one shown at a time: the sections of a report, the pages of a settings screen. An indicator slides to the selected tab. For a choice that changes a value and shows no view of its own, use the segmented control; for steps that must be done in order, use the stepper.
Install
npx shadcn@latest add https://thrumui.dev/r/tabs.jsonAdds components/ui/tabs.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.
import { Tabs } from "@/components/ui/tabs";Props
| Prop | Type | Default | Description |
|---|---|---|---|
Tabs: value | string | number | null | – | The selected tab's value, when the parent holds it. Use it with onValueChange. null selects none. |
Tabs: defaultValue | string | number | null | Default 0 | The value selected at first, when the tabs hold it themselves. Set it: the default of 0 only matches a tab whose value is 0. |
Tabs: onValueChange | (value, details) => void | – | Called with the new value when another tab is selected. |
Tabs: orientation | "horizontal" | "vertical" | Default "horizontal" | A row of tabs above the views, or a column of tabs beside them. It also decides which arrow keys move. |
Tabs: sound | boolean | Default true | Play a soft click when the reader picks another tab, if the visitor has sound on. |
Tabs: className | string | – | Extra classes for the root, merged last. Set a height here for a vertical list that should scroll. |
TabsList: variant | "underline" | "pill" | Default "underline" | underline draws a 2 pixel line on the selected tab's edge. pill slides a filled thumb behind it, inside a framed track. |
TabsList: size | "sm" | "md" | "lg" | Default "md" | The height of every tab: 28, 32 or 40 pixels. A pill list is that tall including its frame. |
TabsList: activateOnFocus | boolean | Default true | Select a tab as soon as the arrow keys reach it. Set false when a view is slow to show: the arrows then only move the focus, and Enter or Space selects. |
TabsList: loopFocus | boolean | Default true | Whether the arrow keys wrap from the last tab to the first. |
TabsList: className | string | – | Extra classes for the list. w-full on a pill list makes the track span its container. |
TabsTrigger: value | string | number | Required | Names the TabsContent this tab shows. Unique within the tabs. |
TabsTrigger: disabled | boolean | Default false | Dimmed and cannot be selected. The arrow keys still reach it, so a screen reader can say it is there. |
TabsTrigger: ...props | button props | – | Every other button attribute. Children can hold an icon or a count beside the text. |
TabsContent: value | string | number | Required | The tab this view belongs to. |
TabsContent: keepMounted | boolean | Default false | Keep the view in the page while it is hidden, so a form inside it keeps its state. |
TabsContent: className | string | – | Extra classes for the view. |
Motion and sound
Motion
- The indicator slides to the selected tab and takes its width over 200 ms. A second pick during the slide continues from where it is.
- Tab labels change colour over 150 ms.
- A list wider than its container scrolls to bring the selected tab into view, with the browser's own smooth scrolling.
- Nothing moves when the page loads: the indicator starts on the selected tab.
- Reduced motion: the indicator, the colours and the scroll position change at once.
Sound
- A soft click when the reader picks another tab, by pointer or by keyboard.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- It follows the WAI-ARIA tabs pattern: a tablist of tabs with aria-selected, each linked to its tabpanel with aria-controls.
- Name the list with an aria-label, as the example does. Each view is named by its tab.
- The selected tab carries the indicator as well as a stronger text colour, so selection never rests on colour alone.
- The view takes focus after the list, so a keyboard reader lands on it even when it holds no control.
- Labels do not wrap in a row of tabs: the list scrolls instead, and the focused tab is always brought into view. In a column they wrap.
- The focus ring is drawn inside the tab, so the edge of a scrolling list never cuts it off.
| Keys | Action |
|---|---|
| Tab | Move into the list, onto the selected tab, then on to the view. The list is one tab stop. |
| ← / → | In a row of tabs: move to the previous or next tab and select it. Past the end it wraps. In right-to-left text the keys swap. |
| ↑ / ↓ | In a column of tabs: move to the previous or next tab and select it. |
| Home / End | Move to the first or last tab. |
| Enter / Space | Select the focused tab. Needed only when activateOnFocus is false. |
Source
"use client";
import { createContext, useContext, useEffect, useRef } from "react";
import { Tabs as TabsPrimitive } from "@base-ui/react/tabs";
import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
export type TabsVariant = "underline" | "pill";
export type TabsSize = "sm" | "md" | "lg";
type Look = { variant: TabsVariant; size: TabsSize };
const LookContext = createContext<Look>({ variant: "underline", size: "md" });
export type TabsProps = TabsPrimitive.Root.Props & {
/** Play a soft click when the reader picks another tab, if the visitor has sound on. */
sound?: boolean;
};
/**
* Views that share one place, one shown at a time. `Tabs` holds the selected value, `TabsList` holds the `TabsTrigger`s,
* and each `TabsContent` is shown while its value is selected. Values are yours to choose: strings read best.
*/
export function Tabs({ sound = true, className, onValueChange, ...props }: TabsProps) {
return (
<TabsPrimitive.Root
data-slot="tabs"
onValueChange={(value, details) => {
// Only a pick by the reader. The fallback to another tab when one is removed or disabled is silent.
if (sound && details.reason === "none") play("select");
onValueChange?.(value, details);
}}
className={cn("flex min-w-0 flex-col gap-3 data-[orientation=vertical]:flex-row data-[orientation=vertical]:gap-4", className)}
{...props}
/>
);
}
export type TabsListProps = TabsPrimitive.List.Props & {
/** `underline` draws a line under the selected tab; `pill` slides a filled thumb behind it. */
variant?: TabsVariant;
/** The height of every tab: `sm` is 28 pixels, `md` 32, `lg` 40. */
size?: TabsSize;
};
const LIST: Record<TabsVariant, string> = {
underline: "border-b data-[orientation=vertical]:border-e data-[orientation=vertical]:border-b-0",
pill: "gap-0.5 rounded-md border border-input bg-muted p-0.5 data-[orientation=horizontal]:w-fit",
};
const INDICATOR: Record<TabsVariant, string> = {
// A 2 pixel line on the edge the views are on: under a row of tabs, beside a column of them.
underline:
"bg-primary data-[orientation=horizontal]:bottom-0 data-[orientation=horizontal]:left-0 data-[orientation=horizontal]:h-0.5 data-[orientation=horizontal]:w-(--active-tab-width) data-[orientation=horizontal]:translate-x-(--active-tab-left) " +
"data-[orientation=vertical]:end-0 data-[orientation=vertical]:top-0 data-[orientation=vertical]:h-(--active-tab-height) data-[orientation=vertical]:w-0.5 data-[orientation=vertical]:translate-y-(--active-tab-top)",
// The thumb covers the selected tab only, so every other label is read against the track.
pill: "top-0 left-0 h-(--active-tab-height) w-(--active-tab-width) translate-x-(--active-tab-left) translate-y-(--active-tab-top) rounded-sm bg-primary",
};
/**
* The row or column of tabs, with the indicator that slides to the selected one. A list wider than its container
* scrolls, and the selected tab is brought into view. Name it with an `aria-label`.
*/
export function TabsList({ variant = "underline", size = "md", activateOnFocus = true, className, children, ...props }: TabsListProps) {
const list = useRef<HTMLDivElement>(null);
// Keep the selected tab inside a list that scrolls. Only the list moves: `scrollIntoView` would scroll the page as well.
useEffect(() => {
const box = list.current;
if (!box) return;
const reveal = (behavior: ScrollBehavior) => {
const tab = box.querySelector<HTMLElement>("[data-active]");
if (!tab) return;
const outer = box.getBoundingClientRect();
const inner = tab.getBoundingClientRect();
const left = inner.left < outer.left ? inner.left - outer.left : Math.max(0, inner.right - outer.right);
const top = inner.top < outer.top ? inner.top - outer.top : Math.max(0, inner.bottom - outer.bottom);
if (left || top) box.scrollBy({ left, top, behavior });
};
// At once on the first look; afterwards the way the stylesheet says, which is smoothly unless motion is reduced.
reveal("instant");
const observer = new MutationObserver(() => reveal("auto"));
observer.observe(box, { attributes: true, attributeFilter: ["data-active"], subtree: true });
return () => observer.disconnect();
}, []);
return (
<LookContext.Provider value={{ variant, size }}>
<TabsPrimitive.List
ref={list}
data-slot="tabs-list"
data-surface={variant === "pill" ? "control" : undefined}
data-variant={variant}
activateOnFocus={activateOnFocus}
className={cn(
"group/tabs-list relative flex max-w-full min-w-0 flex-none scroll-smooth [scrollbar-width:none] motion-reduce:scroll-auto",
"data-[orientation=horizontal]:overflow-x-auto data-[orientation=vertical]:max-h-full data-[orientation=vertical]:flex-col data-[orientation=vertical]:overflow-y-auto",
LIST[variant],
className,
)}
{...props}
>
{children}
<TabsPrimitive.Indicator
data-slot="tabs-indicator"
className={cn(
"pointer-events-none absolute transition-[translate,width,height] duration-200 ease-out motion-reduce:transition-none",
INDICATOR[variant],
)}
/>
</TabsPrimitive.List>
</LookContext.Provider>
);
}
const SIZE: Record<TabsSize, string> = {
sm: "min-h-7 px-2.5 text-xs",
md: "min-h-8 px-3 text-sm",
lg: "min-h-10 px-4 text-sm",
};
// Inside the pill frame a tab gives up the frame's 3 pixels on each side, so the control keeps the stated height.
const PILL_SIZE: Record<TabsSize, string> = {
// The pseudo-element keeps the small size a 24 pixel target.
sm: "min-h-5.5 px-2.5 text-xs after:absolute after:inset-x-0 after:-inset-y-px",
md: "min-h-6.5 px-3 text-sm",
lg: "min-h-8.5 px-4 text-sm",
};
/** One tab. Its `value` names the `TabsContent` it shows. */
export function TabsTrigger({ className, ...props }: TabsPrimitive.Tab.Props) {
const { variant, size } = useContext(LookContext);
return (
<TabsPrimitive.Tab
data-slot="tabs-trigger"
className={cn(
// Above the indicator, so the pill's thumb sits behind the label.
"relative z-10 inline-flex flex-none cursor-pointer items-center justify-center gap-2 rounded-sm font-medium whitespace-nowrap text-muted-foreground select-none",
"transition-[color] duration-150 ease-out hover:text-foreground motion-reduce:transition-none",
// The ring is drawn inside the tab: a list that scrolls would cut off one drawn outside.
"focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring",
"data-disabled:cursor-not-allowed data-disabled:opacity-50 data-disabled:hover:text-muted-foreground",
"data-[orientation=vertical]:justify-start data-[orientation=vertical]:py-1 data-[orientation=vertical]:text-start data-[orientation=vertical]:whitespace-normal",
variant === "underline"
? "data-active:text-foreground"
: // Until the thumb has been measured, the selected tab carries the fill itself.
"data-active:text-primary-foreground data-active:hover:text-primary-foreground group-has-[[data-slot=tabs-indicator][hidden]]/tabs-list:data-active:bg-primary",
variant === "underline" ? SIZE[size] : PILL_SIZE[size],
className,
)}
{...props}
/>
);
}
/** The view for one tab. Only the selected one is in the page, unless `keepMounted` is set. */
export function TabsContent({ className, ...props }: TabsPrimitive.Panel.Props) {
return (
<TabsPrimitive.Panel
data-slot="tabs-content"
className={cn("min-w-0 flex-auto rounded-sm text-sm focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring", className)}
{...props}
/>
);
}Files and dependencies
- Adds
components/ui/tabs.tsx- Packages
@base-ui/react@1.9.0- Brings along
- Sound engine
Changelog
- 1.0.0 · 2026-12-03First release.