Tabs
Tabs example: tabbed panels with a pill-shaped default list, or a line variant that marks the active tab with a bar. Works horizontally or as a vertical side list.
pnpm dlx shadcn add @01orbital/tabs"use client"
import { Tabs as TabsPrimitive } from "@base-ui/react/tabs"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
function Tabs({
className,
orientation = "horizontal",
...props
}: TabsPrimitive.Root.Props) {
return (
<TabsPrimitive.Root
data-slot="tabs"
data-orientation={orientation}
className={cn(
"group/tabs flex gap-2 data-horizontal:flex-col",
className
)}
{...props}
/>
)
}
const tabsListVariants = cva(
"group/tabs-list inline-flex w-fit items-center justify-center rounded-full p-1 text-muted-foreground group-data-horizontal/tabs:h-9 group-data-vertical/tabs:h-fit group-data-vertical/tabs:flex-col group-data-vertical/tabs:rounded-2xl data-[variant=line]:rounded-none",
{
variants: {
variant: {
default: "bg-muted",
line: "gap-1 bg-transparent",
},
},
defaultVariants: {
variant: "default",
},
}
)
function TabsList({
className,
variant = "default",
...props
}: TabsPrimitive.List.Props & VariantProps<typeof tabsListVariants>) {
return (
<TabsPrimitive.List
data-slot="tabs-list"
data-variant={variant}
className={cn(tabsListVariants({ variant }), className)}
{...props}
/>
)
}
function TabsTrigger({ className, ...props }: TabsPrimitive.Tab.Props) {
return (
<TabsPrimitive.Tab
data-slot="tabs-trigger"
className={cn(
"relative inline-flex h-[calc(100%-1px)] flex-1 items-center justify-center gap-2 rounded-full border border-transparent! px-3 py-1 text-sm font-medium whitespace-nowrap text-foreground/60 transition-all group-data-vertical/tabs:w-full group-data-vertical/tabs:justify-start group-data-vertical/tabs:rounded-2xl group-data-vertical/tabs:px-3 group-data-vertical/tabs:py-1.5 hover:text-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:outline-1 focus-visible:outline-ring disabled:pointer-events-none disabled:opacity-50 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2 aria-disabled:pointer-events-none aria-disabled:opacity-50 dark:text-muted-foreground dark:hover:text-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
"group-data-[variant=line]/tabs-list:bg-transparent group-data-[variant=line]/tabs-list:data-active:bg-transparent dark:group-data-[variant=line]/tabs-list:data-active:border-transparent dark:group-data-[variant=line]/tabs-list:data-active:bg-transparent",
"data-active:bg-background data-active:text-foreground dark:data-active:border-input dark:data-active:bg-input/30 dark:data-active:text-foreground",
"after:absolute after:bg-foreground after:opacity-0 after:transition-opacity group-data-horizontal/tabs:after:inset-x-0 group-data-horizontal/tabs:after:bottom-[-5px] group-data-horizontal/tabs:after:h-0.5 group-data-vertical/tabs:after:inset-y-0 group-data-vertical/tabs:after:-right-1 group-data-vertical/tabs:after:w-0.5 group-data-[variant=line]/tabs-list:data-active:after:opacity-100",
className
)}
{...props}
/>
)
}
function TabsContent({ className, ...props }: TabsPrimitive.Panel.Props) {
return (
<TabsPrimitive.Panel
data-slot="tabs-content"
className={cn("flex-1 text-sm outline-none", className)}
{...props}
/>
)
}
export { Tabs, TabsList, TabsTrigger, TabsContent, tabsListVariants }
"use client"
import {
Tabs,
TabsContent,
TabsList,
TabsTrigger,
} from "@/components/ui/tabs"
// The copy: change text here, not in the markup.
const PANELS = [
{
value: "preview",
tab: "Preview",
body: "The block renders at its own natural height, on your theme tokens.",
mono: false,
},
{
value: "code",
tab: "Code",
body: "Source lands in your repo — edit it like anything else you wrote.",
mono: false,
},
{
value: "install",
tab: "Install",
body: "npx shadcn add @01orbital/centered-hero-04",
mono: true,
},
] as const
/** Tabs with three panels. */
export default function TabsDemo() {
return (
<Tabs defaultValue="preview" className="w-full max-w-md">
<TabsList>
{PANELS.map((panel) => (
<TabsTrigger key={panel.value} value={panel.value}>
{panel.tab}
</TabsTrigger>
))}
</TabsList>
{PANELS.map((panel) => (
<TabsContent
key={panel.value}
value={panel.value}
className={
panel.mono
? "pt-4 font-mono text-sm/6 text-muted-foreground"
: "pt-4 text-sm/6 text-muted-foreground"
}
>
{panel.body}
</TabsContent>
))}
</Tabs>
)
}
Examples
Underline Tabstabs-underline
Tabs example: the line variant with the active bar as wide as the word, the triggers spaced by a gap instead of padding.
pnpm dlx shadcn add @01orbital/tabs-underlineimport {
Tabs,
TabsContent,
TabsList,
TabsTrigger,
} from "@/components/ui/tabs"
// The copy: change text here, not in the markup.
const PANELS = [
{
value: "overview",
tab: "Overview",
body: "A quiet release: two fixes, one new palette, nothing to migrate.",
},
{
value: "changes",
tab: "Changes",
body: "Toasts stack from the bottom again. The ember palette joins the orbs.",
},
{
value: "credits",
tab: "Credits",
body: "Ines Marlowe, Tomas Reyes and everyone who filed a report.",
},
] as const
/**
* The line variant with the bar hugging the word: the stock trigger pads
* each side and draws its bar across the padding, so here the padding goes
* and the list spaces the triggers with a gap instead.
*/
export default function TabsUnderline() {
return (
<Tabs defaultValue="overview" className="w-full max-w-md">
<TabsList variant="line" className="gap-6">
{PANELS.map((panel) => (
<TabsTrigger key={panel.value} value={panel.value} className="px-0">
{panel.tab}
</TabsTrigger>
))}
</TabsList>
{PANELS.map((panel) => (
<TabsContent key={panel.value} value={panel.value}>
<p className="pt-2 text-sm text-muted-foreground">{panel.body}</p>
</TabsContent>
))}
</Tabs>
)
}
Dependencies
npm: @base-ui/react, class-variance-authority, cn. The CLI installs them for you. cn merges Tailwind class names, in place of clsx and tailwind-merge.
Used in blocks
9 blocks are built with Tabs: