Components
Tabs
Switch between panels of related content that share one place on the page.
Introduction
Tabs split a lot of content into a few panels and show one at a time: the sections of a profile, the languages of a code sample, the views of a report. The tabs are always visible, so people can see what else there is and jump to it.
- The arrow keys move between the tabs, Home and End jump to the first and the last, and the selected panel follows.
keyboardActivation="manual"waits for Enter or Space instead. - Three ways to mark the selected tab: a line, a soft pill, or a raised segment. The mark slides to the new tab with a spring, animated by Framer Motion, and jumps instead for people who prefer reduced motion.
- Horizontal or vertical, any of three sizes, and the tabs can share the full width. A tab can have an icon or be disabled.
- Uncontrolled by default, or controlled with
selectedKey. Tabs can come from data, and can be links.
For one command in several package managers, see SnippetTabs. To show and hide one section, use Disclosure.
Installation
centerUI has no CLI or registry yet: components are copied into your project. Tabs uses Framer Motion for the sliding indicator.
# bun
bun add react-aria-components framer-motion tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components framer-motion tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components framer-motion tailwind-variants tailwind-merge clsxManual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles and the context
src/types/tabs-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for Tabs. react-aria puts `data-selected`, `data-hovered` and so on on
// each tab. The indicator is a Framer Motion element with a `layoutId`, which
// is what makes it slide from the old tab to the new one: it is only the box,
// and Framer Motion animates it.
export const tabsStyles = tv({
slots: {
root: "flex",
list: "flex",
tab: [
"relative isolate flex cursor-pointer items-center gap-2 whitespace-nowrap font-medium text-muted-fg outline-none transition-colors",
"[&_svg]:size-4 [&_svg]:shrink-0",
"data-hovered:text-fg data-selected:text-fg",
"data-focus-visible:ring-2 data-focus-visible:ring-ring data-focus-visible:ring-inset",
"data-disabled:cursor-not-allowed data-disabled:opacity-50",
],
indicator: "pointer-events-none absolute",
panel:
"flex-1 rounded-md outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-bg",
},
variants: {
orientation: {
horizontal: { root: "flex-col", list: "flex-row", tab: "justify-center" },
vertical: {
root: "flex-row",
list: "flex-col items-stretch self-start",
tab: "justify-start text-left",
},
},
variant: {
// A line under the selected tab, over the line under all of them.
underline: {},
// The selected tab has a soft background.
pills: { list: "gap-1", indicator: "inset-0 -z-10 rounded-md bg-muted" },
// The tabs sit in a tray and the selected one is raised, like a switch.
segmented: {
list: "gap-1 rounded-lg bg-muted p-1",
tab: "rounded-md",
indicator: "inset-0 -z-10 rounded-md bg-bg shadow-sm",
},
},
size: {
sm: { tab: "px-2.5 py-1.5 text-xs" },
md: { tab: "px-3 py-2 text-sm" },
lg: { tab: "px-4 py-2.5 text-base" },
},
// The tabs share the whole width of the list.
isFullWidth: {
true: { list: "w-full", tab: "flex-1" },
false: {},
},
},
compoundVariants: [
{
variant: "underline",
orientation: "horizontal",
class: {
list: "border-b",
indicator: "inset-x-0 -bottom-px h-0.5 rounded-full bg-fg",
},
},
{
variant: "underline",
orientation: "vertical",
class: {
list: "border-l",
indicator: "inset-y-0 -left-px w-0.5 rounded-full bg-fg",
},
},
{ orientation: "horizontal", class: { panel: "mt-4" } },
{ orientation: "vertical", class: { panel: "ml-6" } },
],
defaultVariants: {
orientation: "horizontal",
variant: "underline",
size: "md",
isFullWidth: false,
},
});src/types/tabs-context.tsShow code
import { createContext, useContext } from "react";
import type { VariantProps } from "tailwind-variants";
import type { tabsStyles } from "./tabs-styles";
/** What Tabs tells the list, the tabs and the panels inside it about the look. */
export type TabsContextValue = Required<
Pick<
VariantProps<typeof tabsStyles>,
"orientation" | "variant" | "size" | "isFullWidth"
>
>;
const TabsContext = createContext<TabsContextValue | null>(null);
export const TabsProvider = TabsContext.Provider;
/** The enclosing Tabs' look, or an error outside of one. */
export function useTabsLook() {
const context = useContext(TabsContext);
if (!context) throw new Error("Tabs parts must be used inside <Tabs>.");
return context;
}2. Add the Tabs file
src/components/ui/tabs/tabs.tsxShow code
"use client";
import { LayoutGroup, motion, useReducedMotion } from "framer-motion";
import { type ReactNode, useId } from "react";
import {
Tab as AriaTab,
TabList as AriaTabList,
type TabListProps as AriaTabListProps,
TabPanel as AriaTabPanel,
Tabs as AriaTabs,
type TabsProps as AriaTabsProps,
} from "react-aria-components";
import { cn } from "@/lib/cn";
import { TabsProvider, useTabsLook } from "@/types/tabs-context";
import { tabsStyles } from "@/types/tabs-styles";
export interface TabsProps
extends Omit<AriaTabsProps, "className" | "style" | "children"> {
/** A `TabList` and a `TabPanel` for each of its tabs. */
children: ReactNode;
/** How the selected tab is marked. */
variant?: "underline" | "pills" | "segmented";
size?: "sm" | "md" | "lg";
/** Tabs side by side (`horizontal`), or stacked at the side of the panel. */
orientation?: "horizontal" | "vertical";
/** The tabs share the whole width of the list. */
isFullWidth?: boolean;
className?: string;
}
/**
* Tabs switch between panels of related content that share one place on the
* page. Built on react-aria's Tabs, so the arrow keys move between the tabs,
* Home and End jump to the first and last, and screen readers hear the tab
* pattern. Compose it from `TabList`, `Tab` and `TabPanel`, and tie each tab
* to its panel with the same `id`.
*
* Uncontrolled by default, starting on the first tab. Start elsewhere with
* `defaultSelectedKey`, or control it with `selectedKey` and
* `onSelectionChange`.
*
* @example
* <Tabs>
* <TabList aria-label="Account">
* <Tab id="profile">Profile</Tab>
* <Tab id="security">Security</Tab>
* </TabList>
* <TabPanel id="profile">Your name and photo.</TabPanel>
* <TabPanel id="security">Password and sessions.</TabPanel>
* </Tabs>
*/
export function Tabs({
children,
variant = "underline",
size = "md",
orientation = "horizontal",
isFullWidth = false,
className,
...props
}: TabsProps) {
const look = { variant, size, orientation, isFullWidth };
const styles = tabsStyles(look);
// Scopes the indicator to this Tabs, so several on a page do not share one.
const group = useId();
return (
<TabsProvider value={look}>
<LayoutGroup id={group}>
<AriaTabs
{...props}
orientation={orientation}
className={cn(styles.root(), className)}
>
{children}
</AriaTabs>
</LayoutGroup>
</TabsProvider>
);
}
export interface TabListProps<T extends object>
extends Omit<AriaTabListProps<T>, "className" | "style"> {
/** Names the list of tabs for screen readers, for example "Account". */
"aria-label"?: string;
className?: string;
}
/**
* The row (or column) of tabs. Give it an `aria-label`. For tabs that come
* from data, pass `items` and a function that renders one `Tab`.
*/
export function TabList<T extends object>({
className,
...props
}: TabListProps<T>) {
const styles = tabsStyles(useTabsLook());
return <AriaTabList {...props} className={cn(styles.list(), className)} />;
}
export interface TabProps {
/** Ties the tab to its `TabPanel`. */
id: string;
children?: ReactNode;
/** An icon before the text. */
icon?: ReactNode;
isDisabled?: boolean;
/** Make the tab a link: it goes to this address when it is chosen. */
href?: string;
className?: string;
}
// The indicator moves like a spring. `bounce` is small, so it settles without
// wobbling.
const slide = { type: "spring", duration: 0.35, bounce: 0.15 } as const;
/** One tab. Its `id` is the `id` of the panel it shows. */
export function Tab({ icon, children, className, ...props }: TabProps) {
const look = useTabsLook();
const styles = tabsStyles(look);
const reduceMotion = useReducedMotion();
return (
<AriaTab {...props} className={cn(styles.tab(), className)}>
{({ isSelected }) => (
<>
{icon}
{children}
{/* Only the selected tab has it. The same layoutId in another tab
makes Framer Motion move it there instead of drawing a new one. */}
{isSelected ? (
<motion.span
layoutId="tabs-indicator"
aria-hidden="true"
transition={reduceMotion ? { duration: 0 } : slide}
// Set here so Framer Motion keeps the corners round while it
// stretches the box.
style={{ borderRadius: look.variant === "underline" ? 9999 : 6 }}
className={styles.indicator()}
/>
) : null}
</>
)}
</AriaTab>
);
}
export interface TabPanelProps {
/** The `id` of the tab that shows it. */
id: string;
children?: ReactNode;
/**
* Keep the panel in the page while its tab is not selected, hidden. What
* the user typed in it and its scroll position are kept, at the price of
* rendering every panel.
*/
shouldForceMount?: boolean;
className?: string;
}
/** The content of one tab. Only the selected panel is shown. */
export function TabPanel({ className, ...props }: TabPanelProps) {
const styles = tabsStyles(useTabsLook());
return <AriaTabPanel {...props} className={cn(styles.panel(), className)} />;
}3. Use it
import { Tab, TabList, TabPanel, Tabs } from "@/components/ui/tabs/tabs";Anatomy
A list of tabs, and a panel for each of them.
<Tabs>
<TabList aria-label="Account">
<Tab id="profile">Profile</Tab>
<Tab id="security">Security</Tab>
</TabList>
<TabPanel id="profile">Your name and photo.</TabPanel>
<TabPanel id="security">Password and sessions.</TabPanel>
</Tabs>| Part | Rendered as | Notes |
|---|---|---|
| Tabs | div | Holds which tab is selected. Sets the look of everything inside it. |
| TabList | tablist | The row of tabs. It needs an aria-label. The arrow keys move between the tabs, and Home and End jump to the first and the last. |
| Tab | tab (button) | One tab, tied to its panel by id. It has an optional icon. |
| Indicator | div inside the tab | The line, pill or raised segment that marks the selected tab. Framer Motion slides it from the old tab to the new one, with a spring. |
| TabPanel | tabpanel | The content of one tab. Only the selected panel is in the page, unless it is kept mounted. |
Examples
Click a tab, or focus the list and use the arrow keys.
Usage
Variants
Underline is a line under the selected tab. Pills is a soft background. Segmented puts the tabs in a tray with the selected one raised.
With icons
An icon before the text.
Sizes
Full width
isFullWidth shares the width of the list between the tabs, which suits a segmented control or a narrow screen.
Vertical
The tabs stack at the side of the panel, and the Up and Down arrows move between them.
Start tab
defaultSelectedKey picks the tab to start on. The first one is selected without it.
Disabled
Disable one tab with isDisabled on it (or a set of them with disabledKeys), or all of them on Tabs. The arrow keys skip a disabled tab.
Manual activation
By default a tab is selected as soon as it gets focus. With manual activation the arrow keys only move focus, and Enter or Space select. Use it when showing a panel is slow.
Controlled
Keep the selected tab in your own state with selectedKey and onSelectionChange, to change it from elsewhere.
selectedKey: security
Tabs from data
Pass items to TabList and a function that renders one Tab, then render a panel for each item.
export * from './button';Keep panels mounted
A panel that is not selected is removed from the page, and what was in it is lost. shouldForceMount keeps it, hidden. Type in the box, switch tabs and come back.
Tabs as links
Give a tab an href and choosing it goes to that address, for tabs that are pages of their own. Then the address decides the selected tab: pass it as selectedKey.
{/* A tab that is a link: the selected one follows the current address */}
<Tabs selectedKey={pathname}>
<TabList aria-label="Sections">
<Tab id="/settings/profile" href="/settings/profile">Profile</Tab>
<Tab id="/settings/billing" href="/settings/billing">Billing</Tab>
</TabList>
</Tabs>Component API
Tabs is made of the parts below.
Tabs
| Prop | Type | Default |
|---|---|---|
| children | TabList + TabPanel | - (required) |
| variant | "underline" | "pills" | "segmented" | "underline" |
| size | "sm" | "md" | "lg" | "md" |
| orientation | "horizontal" | "vertical" | "horizontal" |
| isFullWidth | boolean | false |
| selectedKey | Key | - |
| defaultSelectedKey | Key | the first tab |
| onSelectionChange | (key: Key) => void | - |
| keyboardActivation | "automatic" | "manual" | "automatic" |
| isDisabled | boolean | false |
| disabledKeys | Iterable<Key> | - |
| className | string | - |
TabList, Tab, TabPanel
| Part | Prop | Type |
|---|---|---|
| TabList | aria-label | string |
| TabList | items | Iterable<T>, with a function as children |
| Tab | id | string (required) |
| Tab | icon | ReactNode |
| Tab | isDisabled | boolean |
| Tab | href | string |
| TabPanel | id | string (required) |
| TabPanel | shouldForceMount | boolean |
| All | className | string |