Skip to content

Tabs

Switches between related views of the same content without leaving the page.

Ordered by Luca from Tornival for the Bergamo site. Delivery is expected on 14 December.

Installation

pnpm dlx shadcn@latest add @silkware/tabs

To copy the component by hand, install its dependencies:

pnpm add react-aria-components class-variance-authority cn

Then copy the source into components/ui/tabs.tsx:

components/ui/tabs.tsx
"use client"
import * as React from "react"import { cva, type VariantProps } from "class-variance-authority"import { cn } from "cn"import {  TabList as TabListPrimitive,  TabPanel as TabPanelPrimitive,  Tab as TabPrimitive,  Tabs as TabsPrimitive,} from "@animate-ui/react/primitives/react-aria"
function Tabs({  className,  ...props}: React.ComponentProps<typeof TabsPrimitive>) {  return (    <TabsPrimitive      data-slot="tabs"      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-lg p-[3px] text-muted-foreground group-data-horizontal/tabs:h-9 group-data-vertical/tabs:h-fit group-data-vertical/tabs:flex-col data-[variant=line]:rounded-none",  {    variants: {      variant: {        default: "bg-muted",        line: "gap-1 bg-transparent",      },    },    defaultVariants: {      variant: "default",    },  })
function TabsList({  className,  variant = "default",  ...props}: React.ComponentProps<typeof TabListPrimitive> &  VariantProps<typeof tabsListVariants>) {  return (    <TabListPrimitive      data-slot="tabs-list"      data-variant={variant}      className={cn(tabsListVariants({ variant }), className)}      {...props}    />  )}
function TabsTrigger({  className,  ...props}: React.ComponentProps<typeof TabPrimitive>) {  return (    <TabPrimitive      data-slot="tabs-trigger"      className={cn(        "relative inline-flex h-[calc(100%-1px)] flex-1 cursor-default items-center justify-center gap-1.5 rounded-md border border-transparent px-2 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 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-1.5 has-data-[icon=inline-start]:pl-1.5 data-[disabled]:pointer-events-none data-[disabled]:opacity-50 dark:text-muted-foreground dark:hover:text-foreground group-data-[variant=default]/tabs-list:data-selected:shadow-sm group-data-[variant=line]/tabs-list:data-selected:shadow-none group-data-[variant=default]/tabs-list:data-active:shadow-sm group-data-[variant=line]/tabs-list:data-active:shadow-none [&_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-selected:bg-transparent dark:group-data-[variant=line]/tabs-list:data-selected:border-transparent dark:group-data-[variant=line]/tabs-list:data-selected:bg-transparent",        "data-selected:bg-background data-selected:text-foreground dark:data-selected:border-input dark:data-selected:bg-input/30 dark:data-selected: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-selected:after:opacity-100",        className      )}      {...props}    />  )}
function TabsContent({  className,  ...props}: React.ComponentProps<typeof TabPanelPrimitive>) {  return (    <TabPanelPrimitive      data-slot="tabs-content"      className={cn("flex-1 text-sm outline-none", className)}      {...props}    />  )}
export { Tabs, TabsList, TabsTrigger, TabsContent, tabsListVariants }

Usage

import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
<Tabs defaultSelectedKey="details">  <TabsList aria-label="Purchase order PO-2041">    <TabsTrigger id="details">Details</TabsTrigger>    <TabsTrigger id="items">Items</TabsTrigger>  </TabsList>  <TabsContent id="details">Order details</TabsContent>  <TabsContent id="items">Order lines</TabsContent></Tabs>

Each TabsTrigger and its TabsContent share the same id. This is how React Aria links them, so there is no value prop as in some other libraries.

Examples

Default

A segmented list on a muted track. Use it for two to five views of one object.

Ordered by Luca from Tornival for the Bergamo site. Delivery is expected on 14 December.

Line

variant="line" on TabsList removes the track and underlines the selected tab. Use it when the tabs sit on a page and not inside a card.

Seven inspections are waiting for a result.

Vertical

orientation="vertical" on Tabs stacks the list and switches the arrow keys to up and down. Use it when the labels are long or there are more than five.

Your name, role and the site you work from.

With icons

Put data-icon="inline-start" on the icon so the trigger tightens its padding. Keep the text: an icon alone does not say what the view holds.

Orders waiting to be picked.

Disabled

isDisabled on a TabsTrigger skips it in keyboard navigation and ignores presses. Use it only for a view that exists but is not available yet, and make sure the panel explains why.

Open and past orders placed with Tornival.

Controlled

Use selectedKey and onSelectionChange to choose the tab from outside the list, for example to open the right view from a link in the page.

Output was up 4% on last week across all lines.

Accessibility

KeyAction
TabMoves focus into the tab list, onto the selected tab, then to the panel.
ArrowRightMoves focus to the next tab in a horizontal list.
ArrowLeftMoves focus to the previous tab in a horizontal list.
ArrowDownMoves focus to the next tab in a vertical list.
ArrowUpMoves focus to the previous tab in a vertical list.
HomeMoves focus to the first tab.
EndMoves focus to the last tab.

React Aria provides:

  • The tablist, tab and tabpanel roles, with aria-selected, aria-controls and aria-labelledby wired between a tab and its panel.
  • Arrow key navigation that skips disabled tabs, and a selected tab that is the only one in the tab order.
  • Selection that follows focus by default.
  • A focus ring for keyboard focus only.

You provide:

  • An aria-label on TabsList that names what the tabs switch, for example "Purchase order PO-2041".
  • A visible text label on every tab.
  • Content in each panel that makes sense without the others.

Guidelines

When to use

  • To split one object into views that are read one at a time: an order into details, items and history.
  • When the views are peers and the user should see all the names at once.

When not to use

  • To move between pages or sections of the app. Use a Navigation Menu or a Sidebar: tabs change content in place and do not change the address.
  • To walk through steps in a fixed order. Use numbered steps in a form.
  • To show and hide one block of secondary content. Use a Collapsible, or an Accordion for several blocks.

Content

  • Use one or two words in sentence case: "Items", not "Order Items".
  • Name the view, not the action: "History", not "View history".
  • Keep a stable order, and put the most used view first.
  • Do not add a count unless it changes what the user does next.

Order PO-2041, Bergamo site.

Do

Use tabs for views of the same object.

All orders.

Don’t

Don’t use tabs as the main navigation. Each section is a page of its own.

API reference

Tabs

The root. Renders a div through the React Aria Tabs. It accepts every React Aria Tabs prop and the props below.

PropTypeDefaultDescription
selectedKey?Key | nullNoneThe selected tab id, for a controlled tabs.
defaultSelectedKey?KeyNoneThe tab selected at first, for an uncontrolled tabs. The first enabled tab when omitted.
onSelectionChange?(key: Key) => voidNoneCalled when the selected tab changes.
orientation?"horizontal" | "vertical""horizontal"The direction of the list and of the arrow keys.
keyboardActivation?"automatic" | "manual""automatic"Whether focusing a tab selects it, or whether it needs Enter or Space.
isDisabled?booleanfalseDisables every tab.
className?stringNoneClasses merged after the base classes.

TabsList

The row or column of triggers. Renders a div with the tablist role through the React Aria TabList.

PropTypeDefaultDescription
variant?"default" | "line""default"A muted track with a raised selected tab, or a transparent list with an underline.
aria-label?stringNoneThe name of the tab list. Required when there is no visible label.
className?stringNoneClasses merged after the variant classes.

TabsTrigger

One tab. Renders a div with the tab role through the React Aria Tab, so it accepts every Tab prop.

PropTypeDefaultDescription
id?KeyNoneLinks the tab to the panel with the same id. Set it on every tab.
isDisabled?booleanfalseSkips the tab in keyboard navigation and ignores presses.
href?stringNoneMakes the tab a link that navigates on selection.
className?stringNoneClasses merged after the base classes.

TabsContent

The panel of one tab. Renders a div with the tabpanel role through the React Aria TabPanel.

PropTypeDefaultDescription
id?KeyNoneThe id of the tab that shows this panel. Set it on every panel.
shouldForceMount?booleanfalseKeeps the panel mounted while it is hidden.
className?stringNoneClasses merged after the base classes.

tabsListVariants

The class function behind TabsList, from class-variance-authority: tabsListVariants({ variant: "line" }).