Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Tabs for React Aria

Tabs for React on React Aria that show one panel at a time, with a filled or underlined list, a vertical layout and arrow-key navigation.

On this page9 sections

Tabs put a few views of one subject in the same spot and show one at a time: a project's overview, analytics and settings, or a snippet's preview and its code. Reach for them when people switch between views rather than read down the page.

When the sections should stack and several may be open together, use an Accordion. To pick a value that changes the content in place, such as a list or grid view, a Segment Group fits better.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Tabs anatomy: root, list, indicator, trigger, content

Usage

import {
  Card, CardContent, CardDescription, CardHeader, CardTitle,
} from "@/components/ui/card";
import {
  Tabs, TabsContent, TabsList, TabsTrigger,
} from "@/components/ui/tabs";
const TabsDemo = () => (
  <Tabs className="w-full max-w-sm" defaultSelectedKey="overview">
    <TabsList>
      <TabsTrigger id="overview">Overview</TabsTrigger>
      <TabsTrigger id="analytics">Analytics</TabsTrigger>
      <TabsTrigger id="reports">Reports</TabsTrigger>
      <TabsTrigger id="settings">Settings</TabsTrigger>
    </TabsList>
    <TabsContent id="overview">
      <Card>
        <CardHeader>
          <CardTitle>Overview</CardTitle>
          <CardDescription>
            View your key metrics and recent project activity. Track progress
            across all your active projects.
          </CardDescription>
        </CardHeader>
        <CardContent className="text-muted-foreground text-sm">
          You have 12 active projects and 3 pending tasks.
        </CardContent>
      </Card>
    </TabsContent>
    <TabsContent id="analytics">
      <Card>
        <CardHeader>
          <CardTitle>Analytics</CardTitle>
          <CardDescription>
            Track performance and user engagement metrics. Monitor trends and
            identify growth opportunities.
          </CardDescription>
        </CardHeader>
        <CardContent className="text-muted-foreground text-sm">
          Page views are up 25% compared to last month.
        </CardContent>
      </Card>
    </TabsContent>
    <TabsContent id="reports">
      <Card>
        <CardHeader>
          <CardTitle>Reports</CardTitle>
          <CardDescription>
            Generate and download your detailed reports. Export data in multiple
            formats for analysis.
          </CardDescription>
        </CardHeader>
        <CardContent className="text-muted-foreground text-sm">
          You have 5 reports ready and available to export.
        </CardContent>
      </Card>
    </TabsContent>
    <TabsContent id="settings">
      <Card>
        <CardHeader>
          <CardTitle>Settings</CardTitle>
          <CardDescription>
            Manage your account preferences and options. Customize your
            experience to fit your needs.
          </CardDescription>
        </CardHeader>
        <CardContent className="text-muted-foreground text-sm">
          Configure notifications, security, and themes.
        </CardContent>
      </Card>
    </TabsContent>
  </Tabs>
);

Accessibility

TabsList is a tablist, each trigger a tab that reports aria-selected, and each panel a tabpanel named by its tab. Only the selected tab is in the tab order, and the arrow keys move between the rest. The open panel joins the tab order only when it holds nothing focusable, so Tab goes from the list straight to the first field or link inside it. Give TabsList an aria-label when no heading says what the tabs are for, and keep a text label in every trigger.

Tab
From outside the list, moves focus to the selected tab. From a tab, moves focus to the open panel, or to the first field or link inside it.
Arrow Right
Moves focus to the next tab and selects it, skipping disabled tabs and wrapping from the last to the first. It works in a vertical list too.
Arrow Left
Moves focus to the previous tab and selects it, wrapping from the first to the last. It works in a vertical list too.
Arrow Down
In a vertical list, moves focus to the next tab and selects it.
Arrow Up
In a vertical list, moves focus to the previous tab and selects it.
Home
Moves focus to the first tab that isn't disabled and selects it.
End
Moves focus to the last tab that isn't disabled and selects it.
Enter
Selects the focused tab, which is how a tab gets selected when keyboardActivation is manual.
Space
Selects the focused tab, which is how a tab gets selected when keyboardActivation is manual.

Demos

Line

Overview, Analytics and Reports in a list with variant set to line on TabsList, and no panels under them. The filled background goes and a 2px line marks the selected tab. Use the line style when the tabs sit on a page already full of cards, where one more filled box would crowd it.

Preview width

Vertical

Account, Password and Notifications stacked in a column, with orientation set to vertical on Tabs. The demo shows only the list, and on a real page the open panel sits beside it. Up and Down move between the tabs, and Left and Right still work too. Pick it for a settings page with more sections, or longer names, than fit on one row.

Preview width

Disabled

Home is selected, and the second tab, labelled Disabled, has isDisabled on its TabsTrigger, so clicking it does nothing. The arrow keys skip it. Keep a disabled tab when the section exists but this account can't open it yet, and leave the tab out when it will never apply.

Preview width

Icons

Preview and Code, each with an icon before its label, the familiar switch between a rendered component and its source. The icons carry aria-hidden, so a screen reader announces only the words. Use icons when they make a mode quicker to spot, and keep the text: a trigger with only an icon needs an aria-label to have a name at all.

Preview width

RTL

The four tabs from the top of the page, in Arabic, inside a div with dir set to rtl and a Direction provider set to right to left. The attribute lays out the list and the panel from the right, and React Aria takes the arrow keys' direction from the provider's locale, so ArrowLeft moves to the next tab. Use this when the page language is Arabic or Hebrew.

Preview width

FAQ

How do I control the selected tab from state in React?

Hold the selected tab's id in state, pass it as selectedKey on Tabs and update it in onSelectionChange, which receives the id of the tab that was picked. React Aria matches each TabsTrigger to its TabsContent by id, not by a value.

Once the state is yours, anything can change it. A Next button at the foot of one panel can move to the following tab, and a page can open on a given tab by reading it from the URL.

Why is no tab selected when my Tabs first render?

React Aria never leaves Tabs empty: when defaultSelectedKey is missing it selects the first tab that isn't disabled, and pressing the selected tab again keeps it selected. If one tab shows up empty, check that its TabsContent has the same id as its TabsTrigger. Set defaultSelectedKey on Tabs to open on another tab.

How do I keep what was typed in a Tabs panel when switching tabs?

Add shouldForceMount to each TabsContent whose state should survive. React Aria removes an inactive panel by default, and with the prop it keeps the panel mounted and makes it inert. React Aria alone would leave it on screen, so this TabsContent hides it while it carries data-inert, with its input and scroll position intact.

Fields in a hidden panel still submit with their form, but the browser can't point at one that fails validation. Check those fields yourself and switch to the tab that holds the problem.

How do I make underline Tabs in React?

Set variant to line on TabsList, as the Line demo does. The list drops its muted background, and each trigger draws a 2px bar under itself that shows only while it is selected, so the line fades from tab to tab instead of sliding. The bar is an after pseudo-element in TabsTrigger, in components/ui/tabs, where you can change its color or thickness.

How do I show Tabs vertically, with the list beside the panel?

Set orientation to vertical on Tabs. The root then lays out as a row, so the triggers stack down one side and the open panel fills the space beside them, and each trigger stretches to the list's width with its label at the start. The Vertical demo shows the list on its own.

The Up and Down arrow keys then move between tabs, and React Aria keeps Left and Right working as well.

Is the React Aria Tabs component accessible with the keyboard?

Yes, and without any setup. Tab lands on the selected tab, the arrow keys move between tabs and skip disabled ones, and Home and End jump to the first and last.

Moving focus selects each tab as it goes. When showing a panel is slow, as with one that fetches data, set keyboardActivation to manual on Tabs, and the arrows only move focus until Enter or Space selects.

The Accessibility section lists every key.

API Reference

Tabs

data-slot="tabs"

PropType
children?ChildrenOrFunction<T>

The children of the component. A function may be provided to alter the children based on component state.

Type
ChildrenOrFunction<T>
defaultSelectedKey?Key

The initial selected keys in the collection (uncontrolled).

isDisabled?boolean

Whether the TabList is disabled. Shows that a selection exists, but is not available in that circumstance.

keyboardActivation?"automatic" | "manual"

Whether tabs are activated automatically on focus or manually.

Type
"automatic" | "manual"
Default
"automatic"
onSelectionChange?(key: Key) → void

Handler that is called when the selection changes.

Parameters

key – Key

Returns
void
orientation?Orientation

The orientation of the tabs.

Default
"horizontal"
selectedKey?Key

The currently selected key in the collection (controlled).

slot?string | null

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Type
string | null
Default
tabs
style?StyleOrFunction<T>

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.

Type
StyleOrFunction<T>
render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
className?string

Merged with the part's classes.

AttributeType
data-focus?string

Present when focused is true.

data-orientation?string

Mirrors orientation.

TabsList

data-slot="tabs-list"

PropType
defaultSelectedKey?Key

The initial selected keys in the collection (uncontrolled).

isDisabled?boolean

Whether the TabList is disabled. Shows that a selection exists, but is not available in that circumstance.

onSelectionChange?(key: Key) → void

Handler that is called when the selection changes.

Parameters

key – Key

Returns
void
selectedKey?Key

The currently selected key in the collection (controlled).

variant?"default" | "line"

The visual style.

Type
"default" | "line"
Default
"default"
className?string

Merged with the part's classes.

AttributeType
data-focus?string

Present when focused is true.

data-orientationOrientation

Mirrors the orientation prop.

data-variant?string

Mirrors variant.

TabsTrigger

data-slot="tabs-trigger"

PropType
children?ChildrenOrFunction<T>

The children of the component. A function may be provided to alter the children based on component state.

Type
ChildrenOrFunction<T>
id?Key

The unique id of the tab.

isDisabled?boolean

Whether the tab is disabled.

style?StyleOrFunction<T>

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.

Type
StyleOrFunction<T>
render?(props: DetailedHTMLProps<LinkWithRequiredHref, HTMLAnchorElement> | React.JSX.IntrinsicElements[Fallback], renderProps: T) → ReactElement

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Note: You can check if 'href' in props in order to tell whether to render an <a> element. Requirements: - You must render the expected element type (e.g. if <a> is expected, you cannot render a <button>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
(props: DetailedHTMLProps<LinkWithRequiredHref, HTMLAnchorElement> | React.JSX.IntrinsicElements[Fallback], renderProps: T) → ReactElement
Parameters

props – DetailedHTMLProps<LinkWithRequiredHref

HTMLAnchorElement> | React.JSX.IntrinsicElements[Fallback]

renderProps – T

Returns
ReactElement
className?string

Merged with the part's classes.

AttributeType
data-focus?string

Present when focused is true.

data-orientationOrientation

Mirrors the orientation prop.

TabsContent

data-slot="tabs-content"

PropType
children?ChildrenOrFunction<T>

The children of the component. A function may be provided to alter the children based on component state.

Type
ChildrenOrFunction<T>
id?Key

The unique id of the tab.

shouldForceMount?boolean

Whether to mount the tab panel in the DOM even when it is not currently selected. Inactive tab panels are inert and cannot be interacted with. They must be styled appropriately so this is clear to the user visually.

Default
false
style?StyleOrFunction<T>

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.

Type
StyleOrFunction<T>
render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
className?string

Merged with the part's classes.

AttributeType
data-focus?string

Present when focused is true.

data-orientationOrientation

Mirrors the orientation prop.