Navigation · Material 3: Tabs
Tabs
Sibling views of one thing. The indicator slides on a spring; the new panel rises in from the side you moved toward.
Examples and variants
Pill (default)
Minimal's segmented look.
function TabsPill() {
return (
<Tabs defaultValue="overview" className="w-full max-w-sm">
<TabsList>
<TabsTrigger value="overview">Overview</TabsTrigger>
<TabsTrigger value="activity">Activity</TabsTrigger>
<TabsTrigger value="settings">Settings</TabsTrigger>
</TabsList>
<TabsContent value="overview" className="text-sm text-muted-foreground">Everything at a glance.</TabsContent>
<TabsContent value="activity" className="text-sm text-muted-foreground">What changed, newest first.</TabsContent>
<TabsContent value="settings" className="text-sm text-muted-foreground">Name, members and danger zone.</TabsContent>
</Tabs>
)
}Underline
For dense headers.
function TabsUnderline() {
return (
<Tabs defaultValue="all" className="w-full max-w-sm">
<TabsList variant="underline">
<TabsTrigger value="all">All</TabsTrigger>
<TabsTrigger value="unread">Unread</TabsTrigger>
<TabsTrigger value="flagged">Flagged</TabsTrigger>
</TabsList>
<TabsContent value="all" className="text-sm text-muted-foreground">All messages.</TabsContent>
<TabsContent value="unread" className="text-sm text-muted-foreground">Only unread.</TabsContent>
<TabsContent value="flagged" className="text-sm text-muted-foreground">Only flagged.</TabsContent>
</Tabs>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/tabs.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install @base-ui/react cnCopy this file to the same paths in your project. Imports use the
@/alias for your project root.components/ui/tabs.tsxtabs
components/ui/tabs.tsx"use client" import * as React from "react" import { Tabs as TabsPrimitive } from "@base-ui/react/tabs" import { cn } from "cn" /* Tabs: sibling views of one thing. One indicator slides between tabs on a spring; the new panel rises in from the side you moved toward. variant="pill" (default, Minimal) or "underline". */ const VariantContext = React.createContext<"pill" | "underline">("pill") const DirectionContext = React.createContext<number>(0) function Tabs({ className, onValueChange, defaultValue, value, ...props }: TabsPrimitive.Root.Props) { const [dir, setDir] = React.useState(0) const previous = React.useRef<unknown>(value ?? defaultValue) return ( <DirectionContext.Provider value={dir}> <TabsPrimitive.Root data-slot="tabs" className={cn("flex flex-col gap-stack-md", className)} defaultValue={defaultValue} value={value} onValueChange={(next, details) => { const list = (details?.event?.target as HTMLElement | undefined)?.closest("[role=tablist]") if (list) { const values = [...list.querySelectorAll<HTMLElement>("[role=tab]")].map((tab) => tab.dataset.value) setDir(Math.sign(values.indexOf(String(next)) - values.indexOf(String(previous.current)))) } previous.current = next onValueChange?.(next, details) }} {...props} /> </DirectionContext.Provider> ) } function TabsList({ className, variant = "pill", playful = false, children, ...props }: TabsPrimitive.List.Props & { variant?: "pill" | "underline" /** Jelly: the indicator overshoots on a bouncier spring (marketing, playful products). */ playful?: boolean }) { return ( <VariantContext.Provider value={variant}> <TabsPrimitive.List data-slot="tabs-list" data-variant={variant} data-playful={playful || undefined} className={cn( "relative inline-flex w-fit items-center", variant === "pill" ? "h-(--tabs-height) rounded-(--tabs-corner) bg-(--tabs-container) p-(--segmented-padding)" : "gap-inline-md", className )} {...props} > <TabsPrimitive.Indicator data-slot="tabs-indicator" className={cn( "tabs-indicator absolute", variant === "pill" ? "top-(--segmented-padding) bottom-(--segmented-padding) rounded-(--tabs-corner) bg-(--tabs-indicator) shadow-(--segmented-indicator-elevation)" : "bottom-0 h-(--stroke-width-medium) rounded-(--shape-full) bg-(--color-fg-default)" )} /> {children} </TabsPrimitive.List> </VariantContext.Provider> ) } function TabsTrigger({ className, value, ...props }: TabsPrimitive.Tab.Props) { const variant = React.useContext(VariantContext) return ( <TabsPrimitive.Tab data-slot="tabs-trigger" data-value={String(value)} value={value} className={cn( "relative z-[1] inline-flex h-full items-center justify-center gap-inline-xs px-3 text-sm whitespace-nowrap text-muted-foreground outline-none transition-colors duration-(--duration-quick) hover:text-foreground focus-visible:shadow-(--focus-ring) data-[active]:text-foreground [&_svg]:size-(--size-icon-sm)", variant === "pill" ? "rounded-(--tabs-corner)" : "pb-2 data-[active]:font-medium", className )} {...props} /> ) } function TabsContent({ className, ...props }: TabsPrimitive.Panel.Props) { const dir = React.useContext(DirectionContext) return ( <TabsPrimitive.Panel data-slot="tabs-content" data-from={dir > 0 ? "end" : dir < 0 ? "start" : undefined} className={cn("tabs-panel outline-none focus-visible:shadow-(--focus-ring)", className)} {...props} /> ) } export { Tabs, TabsList, TabsTrigger, TabsContent }
Imports
What the examples on this page import:
import {
import { AppBar } from "@/components/ui/app-bar"
import { Badge } from "@/components/ui/badge"
import { Breadcrumb } from "@/components/ui/breadcrumb"
import { Button } from "@/components/ui/button"
import { DropdownMenuItem } from "@/components/ui/dropdown-menu"
import { Fab } from "@/components/ui/fab"
import { Navigation, NavigationItem } from "@/components/ui/navigation"
import { Pagination } from "@/components/ui/pagination"
import { SplitButton } from "@/components/ui/split-button"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { Toolbar, ToolbarButton, ToolbarGroup, ToolbarSeparator } from "@/components/ui/toolbar"Anatomy
- 1List · --tabs-height, --tabs-container, --tabs-corner (pill).
- 2Indicator · --tabs-indicator; underline uses --stroke-width-medium.
- 3Tab · Label and optional icon.
- 4Panel · The content for the active tab.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultValue / value | string | — | Active tab. |
TabsList variant | "pill" | "underline" | "pill" | Look. |
TabsList playful | boolean | false | Jelly: the pill overshoots on a bouncier spring. |
onValueChange | (value) => void | — | Called on change. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--tabs-height | var(--size-control-md) | 40px | same |
--tabs-indicator | var(--surface-container-lowest) | oklch(1 0 0) | oklch(0.14 0 0) |
--tabs-container | var(--surface-container) | oklch(0.965 0.005 95) | oklch(0.12 0 0) |
--tabs-corner | var(--shape-full) | 999px | same |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Switch | Indicator slides on a spring; panel enters from the travel direction. | --spring-snappy, --duration-settle |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Arrow keys move between tabs; Home and End jump.
- tablist, tab and tabpanel roles are wired.
Usage
Do
- Use for 2 to 6 views of the same object.
Don’t
- Don't use for steps in a flow or for page navigation.
Code
"use client"
import * as React from "react"
import { Tabs as TabsPrimitive } from "@base-ui/react/tabs"
import { cn } from "cn"
/*
Tabs: sibling views of one thing. One indicator slides between tabs on a
spring; the new panel rises in from the side you moved toward.
variant="pill" (default, Minimal) or "underline".
*/
const VariantContext = React.createContext<"pill" | "underline">("pill")
const DirectionContext = React.createContext<number>(0)
function Tabs({ className, onValueChange, defaultValue, value, ...props }: TabsPrimitive.Root.Props) {
const [dir, setDir] = React.useState(0)
const previous = React.useRef<unknown>(value ?? defaultValue)
return (
<DirectionContext.Provider value={dir}>
<TabsPrimitive.Root
data-slot="tabs"
className={cn("flex flex-col gap-stack-md", className)}
defaultValue={defaultValue}
value={value}
onValueChange={(next, details) => {
const list = (details?.event?.target as HTMLElement | undefined)?.closest("[role=tablist]")
if (list) {
const values = [...list.querySelectorAll<HTMLElement>("[role=tab]")].map((tab) => tab.dataset.value)
setDir(Math.sign(values.indexOf(String(next)) - values.indexOf(String(previous.current))))
}
previous.current = next
onValueChange?.(next, details)
}}
{...props}
/>
</DirectionContext.Provider>
)
}
function TabsList({
className,
variant = "pill",
playful = false,
children,
...props
}: TabsPrimitive.List.Props & {
variant?: "pill" | "underline"
/** Jelly: the indicator overshoots on a bouncier spring (marketing, playful products). */
playful?: boolean
}) {
return (
<VariantContext.Provider value={variant}>
<TabsPrimitive.List
data-slot="tabs-list"
data-variant={variant}
data-playful={playful || undefined}
className={cn(
"relative inline-flex w-fit items-center",
variant === "pill" ? "h-(--tabs-height) rounded-(--tabs-corner) bg-(--tabs-container) p-(--segmented-padding)" : "gap-inline-md",
className
)}
{...props}
>
<TabsPrimitive.Indicator
data-slot="tabs-indicator"
className={cn(
"tabs-indicator absolute",
variant === "pill"
? "top-(--segmented-padding) bottom-(--segmented-padding) rounded-(--tabs-corner) bg-(--tabs-indicator) shadow-(--segmented-indicator-elevation)"
: "bottom-0 h-(--stroke-width-medium) rounded-(--shape-full) bg-(--color-fg-default)"
)}
/>
{children}
</TabsPrimitive.List>
</VariantContext.Provider>
)
}
function TabsTrigger({ className, value, ...props }: TabsPrimitive.Tab.Props) {
const variant = React.useContext(VariantContext)
return (
<TabsPrimitive.Tab
data-slot="tabs-trigger"
data-value={String(value)}
value={value}
className={cn(
"relative z-[1] inline-flex h-full items-center justify-center gap-inline-xs px-3 text-sm whitespace-nowrap text-muted-foreground outline-none transition-colors duration-(--duration-quick) hover:text-foreground focus-visible:shadow-(--focus-ring) data-[active]:text-foreground [&_svg]:size-(--size-icon-sm)",
variant === "pill" ? "rounded-(--tabs-corner)" : "pb-2 data-[active]:font-medium",
className
)}
{...props}
/>
)
}
function TabsContent({ className, ...props }: TabsPrimitive.Panel.Props) {
const dir = React.useContext(DirectionContext)
return (
<TabsPrimitive.Panel
data-slot="tabs-content"
data-from={dir > 0 ? "end" : dir < 0 ? "start" : undefined}
className={cn("tabs-panel outline-none focus-visible:shadow-(--focus-ring)", className)}
{...props}
/>
)
}
export { Tabs, TabsList, TabsTrigger, TabsContent }