Get Started
Components
- Accordion
- Attachment
- Avatar
- Badge
- Breadcrumb
- Button
- Button Group
- Card
- Checkbox
- Code Block
- Collapsible
- Dialog
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP
- Item
- Label
- Marker
- Message
- Number Flow
- Pagination
- Popover
- Progress
- Radio Group
- Select
- Slider
- Sonner
- Spinner
- Switch
- Tooltip
- Separator
- Sheet
- Tabs
- Textarea
- Toggle
- Toggle Group
- Trace
AI Elements
Libraries
Utilities
Overview
View your key metrics and recent project activity.
You have 12 active projects and 3 pending tasks.
Analytics
Track performance and user engagement metrics.
Page views are up 25% compared to last month.
Reports
Generate and download your detailed reports.
You have 5 reports ready and available to export.
Settings
Manage your account preferences and options.
Configure notifications, security, and themes.
"use client";
import * as React from "react";
import { cn } from "@/lib/utils";
interface TabsContextValue {
baseId: string;
orientation: "horizontal" | "vertical";
setValue: (value: string) => void;
value: string;
}
const TabsContext = React.createContext<TabsContextValue | null>(null);
function useTabsContext(): TabsContextValue {
const ctx = React.useContext(TabsContext);
if (!ctx) {
throw new Error("Tabs components must be used within <Tabs>.");
}
return ctx;
}
interface TabsProps {
children?: React.ReactNode;
className?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
orientation?: "horizontal" | "vertical";
value?: string;
}
function Tabs({
children,
className,
value: valueProp,
defaultValue = "",
onValueChange,
orientation = "horizontal",
}: TabsProps) {
const isControlled = valueProp !== undefined;
const [uncontrolled, setUncontrolled] = React.useState(defaultValue);
const value = isControlled ? valueProp : uncontrolled;
const baseId = React.useId();
const setValue = React.useCallback(
(next: string) => {
if (!isControlled) {
setUncontrolled(next);
}
onValueChange?.(next);
},
[isControlled, onValueChange]
);
const ctx = React.useMemo<TabsContextValue>(
() => ({ baseId, orientation, setValue, value }),
[value, setValue, baseId, orientation]
);
return (
<TabsContext.Provider value={ctx}>
<div
className={cn(
"flex flex-col gap-2",
orientation === "vertical" && "flex-row",
className
)}
data-orientation={orientation}
data-slot="tabs"
>
{children}
</div>
</TabsContext.Provider>
);
}
type TabsListProps = React.ComponentPropsWithoutRef<"div">;
function TabsList({ children, className, onKeyDown, ...props }: TabsListProps) {
const ctx = useTabsContext();
const listRef = React.useRef<HTMLDivElement>(null);
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
onKeyDown?.(e);
if (e.defaultPrevented) {
return;
}
const root = listRef.current;
if (!root) {
return;
}
const triggers = Array.from(
root.querySelectorAll<HTMLButtonElement>(
"[data-slot=tabs-trigger]:not([disabled])"
)
);
if (triggers.length === 0) {
return;
}
const currentIdx = triggers.indexOf(
document.activeElement as HTMLButtonElement
);
if (currentIdx < 0) {
return;
}
let nextIdx = -1;
const isVertical = ctx.orientation === "vertical";
const prev = isVertical ? "ArrowUp" : "ArrowLeft";
const next = isVertical ? "ArrowDown" : "ArrowRight";
if (e.key === next) {
nextIdx = (currentIdx + 1) % triggers.length;
} else if (e.key === prev) {
nextIdx = (currentIdx - 1 + triggers.length) % triggers.length;
} else if (e.key === "Home") {
nextIdx = 0;
} else if (e.key === "End") {
nextIdx = triggers.length - 1;
}
if (nextIdx < 0) {
return;
}
e.preventDefault();
triggers[nextIdx].focus();
triggers[nextIdx].click();
};
return (
<div
{...props}
className={cn(
"bg-muted text-muted-foreground inline-flex h-9 w-fit items-center justify-center rounded-lg p-[3px] outline-none",
className
)}
data-slot="tabs-list"
onKeyDown={handleKeyDown}
ref={listRef}
role="tablist"
>
{children}
</div>
);
}
type TabsTriggerProps = Omit<
React.ComponentPropsWithoutRef<"button">,
"value" | "ref"
> & {
value: string;
ref?: React.Ref<HTMLButtonElement>;
};
function TabsTrigger({
children,
className,
onClick,
ref,
value,
...props
}: TabsTriggerProps) {
const ctx = useTabsContext();
const isActive = ctx.value === value;
const triggerId = `${ctx.baseId}-trigger-${value}`;
const contentId = `${ctx.baseId}-content-${value}`;
return (
<button
{...props}
aria-controls={contentId}
aria-selected={isActive}
className={cn(
"data-[state=active]:bg-background dark:data-[state=active]:text-foreground dark:data-[state=active]:border-input dark:data-[state=active]:bg-input/30 text-foreground dark:text-muted-foreground inline-flex h-[calc(100%-1px)] flex-1 items-center justify-center gap-1.5 rounded-md border border-transparent px-2 py-1 text-sm font-medium whitespace-nowrap transition-[color,box-shadow,opacity] duration-150 focus-visible:outline-none active:opacity-60 data-[state=active]:shadow-sm [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*=size-])]:size-4",
className
)}
data-slot="tabs-trigger"
data-state={isActive ? "active" : "inactive"}
id={triggerId}
onClick={(e) => {
onClick?.(e);
if (e.defaultPrevented) {
return;
}
ctx.setValue(value);
}}
ref={ref}
role="tab"
tabIndex={isActive ? 0 : -1}
type="button"
>
{children}
</button>
);
}
type TabsContentProps = Omit<React.ComponentPropsWithoutRef<"div">, "value"> & {
value: string;
as?: React.ElementType;
asChild?: boolean;
ref?: React.Ref<HTMLDivElement>;
};
function TabsContent({
as,
asChild,
children,
className,
ref,
value,
...props
}: TabsContentProps) {
const ctx = useTabsContext();
const isActive = ctx.value === value;
const triggerId = `${ctx.baseId}-trigger-${value}`;
const contentId = `${ctx.baseId}-content-${value}`;
const merged = {
...props,
"aria-hidden": !isActive,
"aria-labelledby": triggerId,
className: cn("flex-1 outline-none", className),
"data-slot": "tabs-content",
"data-state": isActive ? "active" : "inactive",
hidden: !isActive,
id: contentId,
role: "tabpanel",
tabIndex: 0,
};
if (asChild && React.isValidElement(children)) {
const child = children as React.ReactElement<Record<string, unknown>>;
return React.cloneElement(child, { ...merged, ref } as never);
}
const Component = (as ?? "div") as React.ElementType;
return (
<Component {...merged} ref={ref}>
{children}
</Component>
);
}
export { Tabs, TabsContent, TabsList, TabsTrigger };Tabs organize related content into panels that switch in place. Only one panel is visible at a time, keeping dense interfaces compact.
Installation
$ bunx --bun frakment add tabs$ npx frakment add tabs$ pnpm dlx frakment add tabs$ yarn dlx frakment add tabs
Copy and paste the following code into your project.
"use client";
import * as React from "react";
import { cn } from "@/lib/utils";
interface TabsContextValue {
baseId: string;
orientation: "horizontal" | "vertical";
setValue: (value: string) => void;
value: string;
}
const TabsContext = React.createContext<TabsContextValue | null>(null);
function useTabsContext(): TabsContextValue {
const ctx = React.useContext(TabsContext);
if (!ctx) {
throw new Error("Tabs components must be used within <Tabs>.");
}
return ctx;
}
interface TabsProps {
children?: React.ReactNode;
className?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
orientation?: "horizontal" | "vertical";
value?: string;
}
function Tabs({
children,
className,
value: valueProp,
defaultValue = "",
onValueChange,
orientation = "horizontal",
}: TabsProps) {
const isControlled = valueProp !== undefined;
const [uncontrolled, setUncontrolled] = React.useState(defaultValue);
const value = isControlled ? valueProp : uncontrolled;
const baseId = React.useId();
const setValue = React.useCallback(
(next: string) => {
if (!isControlled) {
setUncontrolled(next);
}
onValueChange?.(next);
},
[isControlled, onValueChange]
);
const ctx = React.useMemo<TabsContextValue>(
() => ({ baseId, orientation, setValue, value }),
[value, setValue, baseId, orientation]
);
return (
<TabsContext.Provider value={ctx}>
<div
className={cn(
"flex flex-col gap-2",
orientation === "vertical" && "flex-row",
className
)}
data-orientation={orientation}
data-slot="tabs"
>
{children}
</div>
</TabsContext.Provider>
);
}
type TabsListProps = React.ComponentPropsWithoutRef<"div">;
function TabsList({ children, className, onKeyDown, ...props }: TabsListProps) {
const ctx = useTabsContext();
const listRef = React.useRef<HTMLDivElement>(null);
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
onKeyDown?.(e);
if (e.defaultPrevented) {
return;
}
const root = listRef.current;
if (!root) {
return;
}
const triggers = Array.from(
root.querySelectorAll<HTMLButtonElement>(
"[data-slot=tabs-trigger]:not([disabled])"
)
);
if (triggers.length === 0) {
return;
}
const currentIdx = triggers.indexOf(
document.activeElement as HTMLButtonElement
);
if (currentIdx < 0) {
return;
}
let nextIdx = -1;
const isVertical = ctx.orientation === "vertical";
const prev = isVertical ? "ArrowUp" : "ArrowLeft";
const next = isVertical ? "ArrowDown" : "ArrowRight";
if (e.key === next) {
nextIdx = (currentIdx + 1) % triggers.length;
} else if (e.key === prev) {
nextIdx = (currentIdx - 1 + triggers.length) % triggers.length;
} else if (e.key === "Home") {
nextIdx = 0;
} else if (e.key === "End") {
nextIdx = triggers.length - 1;
}
if (nextIdx < 0) {
return;
}
e.preventDefault();
triggers[nextIdx].focus();
triggers[nextIdx].click();
};
return (
<div
{...props}
className={cn(
"bg-muted text-muted-foreground inline-flex h-9 w-fit items-center justify-center rounded-lg p-[3px] outline-none",
className
)}
data-slot="tabs-list"
onKeyDown={handleKeyDown}
ref={listRef}
role="tablist"
>
{children}
</div>
);
}
type TabsTriggerProps = Omit<
React.ComponentPropsWithoutRef<"button">,
"value" | "ref"
> & {
value: string;
ref?: React.Ref<HTMLButtonElement>;
};
function TabsTrigger({
children,
className,
onClick,
ref,
value,
...props
}: TabsTriggerProps) {
const ctx = useTabsContext();
const isActive = ctx.value === value;
const triggerId = `${ctx.baseId}-trigger-${value}`;
const contentId = `${ctx.baseId}-content-${value}`;
return (
<button
{...props}
aria-controls={contentId}
aria-selected={isActive}
className={cn(
"data-[state=active]:bg-background dark:data-[state=active]:text-foreground dark:data-[state=active]:border-input dark:data-[state=active]:bg-input/30 text-foreground dark:text-muted-foreground inline-flex h-[calc(100%-1px)] flex-1 items-center justify-center gap-1.5 rounded-md border border-transparent px-2 py-1 text-sm font-medium whitespace-nowrap transition-[color,box-shadow,opacity] duration-150 focus-visible:outline-none active:opacity-60 data-[state=active]:shadow-sm [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*=size-])]:size-4",
className
)}
data-slot="tabs-trigger"
data-state={isActive ? "active" : "inactive"}
id={triggerId}
onClick={(e) => {
onClick?.(e);
if (e.defaultPrevented) {
return;
}
ctx.setValue(value);
}}
ref={ref}
role="tab"
tabIndex={isActive ? 0 : -1}
type="button"
>
{children}
</button>
);
}
type TabsContentProps = Omit<React.ComponentPropsWithoutRef<"div">, "value"> & {
value: string;
as?: React.ElementType;
asChild?: boolean;
ref?: React.Ref<HTMLDivElement>;
};
function TabsContent({
as,
asChild,
children,
className,
ref,
value,
...props
}: TabsContentProps) {
const ctx = useTabsContext();
const isActive = ctx.value === value;
const triggerId = `${ctx.baseId}-trigger-${value}`;
const contentId = `${ctx.baseId}-content-${value}`;
const merged = {
...props,
"aria-hidden": !isActive,
"aria-labelledby": triggerId,
className: cn("flex-1 outline-none", className),
"data-slot": "tabs-content",
"data-state": isActive ? "active" : "inactive",
hidden: !isActive,
id: contentId,
role: "tabpanel",
tabIndex: 0,
};
if (asChild && React.isValidElement(children)) {
const child = children as React.ReactElement<Record<string, unknown>>;
return React.cloneElement(child, { ...merged, ref } as never);
}
const Component = (as ?? "div") as React.ElementType;
return (
<Component {...merged} ref={ref}>
{children}
</Component>
);
}
export { Tabs, TabsContent, TabsList, TabsTrigger };Update the import paths to match your project setup.
Usage
import {
Tabs,
TabsContent,
TabsList,
TabsTrigger,
} from "@/components/ui/tabs";<Tabs defaultValue="account">
<TabsList>
<TabsTrigger value="account">Account</TabsTrigger>
<TabsTrigger value="password">Password</TabsTrigger>
</TabsList>
<TabsContent value="account">Account settings.</TabsContent>
<TabsContent value="password">Password settings.</TabsContent>
</Tabs>Composition
Tabs
├── TabsList
│ └── TabsTrigger
└── TabsContentExamples
Vertical
Set orientation="vertical" to stack triggers beside the panel.
Update your display name and email address.
Change your password and enable two-factor authentication.
Control which emails and push notifications you receive.
"use client";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
export function TabsVertical() {
return (
<Tabs className="w-[400px]" defaultValue="account" orientation="vertical">
<TabsList>
<TabsTrigger value="account">Account</TabsTrigger>
<TabsTrigger value="password">Password</TabsTrigger>
<TabsTrigger value="notifications">Notifications</TabsTrigger>
</TabsList>
<TabsContent className="text-muted-foreground text-sm" value="account">
Update your display name and email address.
</TabsContent>
<TabsContent className="text-muted-foreground text-sm" value="password">
Change your password and enable two-factor authentication.
</TabsContent>
<TabsContent
className="text-muted-foreground text-sm"
value="notifications"
>
Control which emails and push notifications you receive.
</TabsContent>
</Tabs>
);
}Disabled
Set disabled on a trigger to prevent selection.
"use client";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
export function TabsDisabled() {
return (
<Tabs defaultValue="home">
<TabsList>
<TabsTrigger value="home">Home</TabsTrigger>
<TabsTrigger disabled value="settings">
Disabled
</TabsTrigger>
</TabsList>
</Tabs>
);
}With icons
Triggers accept icons before the label. Icons size themselves via [&_svg]:size-4.
"use client";
import { CodeIcon, FileCodeIcon } from "lucide-react";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
export function TabsIcons() {
return (
<Tabs defaultValue="preview">
<TabsList>
<TabsTrigger value="preview">
<CodeIcon />
Preview
</TabsTrigger>
<TabsTrigger value="code">
<FileCodeIcon />
Code
</TabsTrigger>
</TabsList>
</Tabs>
);
}In Context
Docs tabs
The component renders here.
Source code for this example.
"use client";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
export function TabsDocs() {
return (
<Tabs className="mx-auto w-full max-w-sm" defaultValue="preview">
<TabsList className="mx-auto">
<TabsTrigger value="preview">Preview</TabsTrigger>
<TabsTrigger value="code">Code</TabsTrigger>
</TabsList>
<TabsContent
className="text-muted-foreground pt-3 text-sm"
value="preview"
>
The component renders here.
</TabsContent>
<TabsContent className="text-muted-foreground pt-3 text-sm" value="code">
Source code for this example.
</TabsContent>
</Tabs>
);
}API Reference
Tabs
| Prop | Type | Default | Description |
|---|---|---|---|
defaultValue | string | — | Value of the tab open on first render. |
value | string | — | Controlled value of the open tab. |
onValueChange | function | — | Called when the active tab changes. |
orientation | horizontalvertical | horizontal | Layout axis of the tab list. |
TabsList
| Prop | Type | Default | Description |
|---|---|---|---|
motion | string | 260 cubic-bezier(0.22, 1, 0.36, 1) | Timing for the active-tab indicator slide. Set to off to disable, or pass duration + easing like 150 ease-out. |
The active-tab background slides between triggers using the Web Animations API. The default is 260ms with a smooth-out curve.
<TabsList motion="400 ease-in-out">
<TabsTrigger value="account">Account</TabsTrigger>
<TabsTrigger value="password">Password</TabsTrigger>
</TabsList>Set motion="off" to snap the indicator without animation. prefers-reduced-motion disables the slide automatically.
TabsTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | — | Identifier matching the associated TabsContent. |
disabled | boolean | false | Prevent the trigger from being selected. |
As Prop
TabsContent accepts as to render a custom element. TabsTrigger renders a native <button> by default.
Controlled
const [value, setValue] = React.useState("tab-1")
<Tabs value={value} onValueChange={setValue}>{/* ... */}</Tabs>Keyboard
| Key | Action |
|---|---|
← / → | Move between triggers (horizontal) |
↑ / ↓ | Move between triggers (vertical) |
Home / End | First / last trigger |
Enter / Space | Activate focused trigger |
Data Attributes
| Attribute | Values |
|---|---|
data-slot | tabs, tabs-list, tabs-trigger, tabs-content |
data-state | active | inactive |
data-orientation | horizontal | vertical |
Accessibility
- Follows the WAI-ARIA tabs pattern.
- Triggers expose
role="tab"andaria-selected. - Keyboard:
ArrowLeftandArrowRightmove between triggers.
Best Practices
- Keep tabs to three or four. More tabs become a nav, not a tab list.
- Pick a default. Empty tab groups confuse users about the current state.
- Do not nest tabs. Nested tabs lose context; use a segmented control instead.
- Do not lazy-load the first tab. The default tab should not flash.
Related Components
- Accordion — stacked collapsible sections
- Breadcrumb — navigate between pages