Skip to content
Drivn
5 min read

React Tabs Component Examples

Copy-paste React Tabs examples: a basic panel, controlled tabs with useState, underline and compact variants with icons, and lazy panels that mount on demand.

Most React tabs start from the same need: several views, one footprint, and a row of triggers that decides which view is showing. The Drivn Tabs covers that with three dot-notation parts attached to one root through Object.assign — Tabs.List for the trigger row, Tabs.Tab for each button, and Tabs.Panel for the content. A single import { Tabs } brings in all three, and the file depends on nothing beyond React and cn().

The root keeps the active value in React.useState and shares it through a small context, which is why the source opens with 'use client'. It also takes a variant prop typed from the keys of its variantStyles object: default draws a bordered bg-accent pill track, underline underlines the active label, and compact shrinks the triggers for toolbars and panel headers.

The examples below build a basic three-tab panel, a controlled version wired to your own state, icon triggers on the underline style, and lazy panels that only mount when selected, since Tabs.Panel returns null for any value that is not active. Each snippet is TypeScript, matching the .tsx source the Drivn CLI writes to @/components/ui/tabs, and any of them can sit inside a Card when the panels need a bordered surface.

A basic three-tab panel

The starting point is a Tabs root with a defaultValue, a Tabs.List of Tabs.Tab triggers, and one Tabs.Panel per tab. Each Tabs.Tab carries a value, and the panel with the matching value is the one that renders — the active value lives in context, so you never wire click handlers yourself. The list renders as a rounded bg-accent track, and the active tab swaps in bg-background text-foreground while the rest stay muted.

Because Tabs is uncontrolled here, it tracks the active tab internally from defaultValue="overview". This is the right default for most UI — a settings panel, a docs section toggle, a product tab strip — where the component owns its own state and you only care about what is shown. Drop it into any client page and it works with no extra state management; see the full prop list on the Tabs reference.

1import { Tabs } from "@/components/ui/tabs"
2
3export default function Page() {
4 return (
5 <Tabs defaultValue="overview">
6 <Tabs.List>
7 <Tabs.Tab value="overview">Overview</Tabs.Tab>
8 <Tabs.Tab value="usage">Usage</Tabs.Tab>
9 <Tabs.Tab value="api">API</Tabs.Tab>
10 </Tabs.List>
11 <Tabs.Panel value="overview">
12 Overview content here.
13 </Tabs.Panel>
14 <Tabs.Panel value="usage">
15 Usage content here.
16 </Tabs.Panel>
17 <Tabs.Panel value="api">
18 API content here.
19 </Tabs.Panel>
20 </Tabs>
21 )
22}

Controlled tabs with React.useState

When you need to read or set the active tab from outside — syncing it to a URL query param, a parent reducer, or a multi-step form — switch to controlled mode by passing value and onChange. The Drivn source reads const value = controlled ?? uncontrolled, so the moment you pass a value prop it takes over and your onChange becomes the single source of truth. Hold the active tab in React.useState in the parent and feed it back in.

This is the pattern to reach for when a Button elsewhere on the page needs to jump the user to a specific tab, or when the active tab must survive a navigation by living in the URL. The wrapper is marked 'use client' for the useState, and setTab is passed straight to onChange because both share the (value: string) => void signature.

1"use client"
2
3import * as React from "react"
4import { Tabs } from "@/components/ui/tabs"
5
6export default function ControlledTabs() {
7 const [tab, setTab] = React.useState("account")
8
9 return (
10 <Tabs value={tab} onChange={setTab}>
11 <Tabs.List>
12 <Tabs.Tab value="account">Account</Tabs.Tab>
13 <Tabs.Tab value="billing">Billing</Tabs.Tab>
14 </Tabs.List>
15 <Tabs.Panel value="account">Account settings</Tabs.Panel>
16 <Tabs.Panel value="billing">Billing settings</Tabs.Panel>
17 </Tabs>
18 )
19}

Underline and compact variants with icon triggers

The variant prop on the root is typed as keyof typeof variantStyles, so your editor offers exactly three values. underline drops the pill track for a gap-4 row of 16px labels and marks the active one with underline underline-offset-8, which suits page-level navigation. compact switches to text-xs triggers with rounded corners and a bg-muted highlight, sized for the header of a code panel or a sidebar section. The variant travels through context, so Tabs.List and every Tabs.Tab pick it up without a prop of their own.

Icons go in as children. Tabs.Tab spreads any remaining <button> attributes and merges className through cn(), but its base classes set no flex layout, so add flex items-center gap-2 to line a lucide-react icon up with the label. The icon inherits the same text-foreground and text-muted-foreground colours as the text when the active tab changes. The Drivn vs shadcn Tabs comparison covers how this differs from a Radix-backed tab list.

1import { Tabs } from "@/components/ui/tabs"
2import { Code, Eye } from "lucide-react"
3
4export default function PreviewTabs() {
5 return (
6 <Tabs defaultValue="preview" variant="underline">
7 <Tabs.List>
8 <Tabs.Tab value="preview" className="flex items-center gap-2">
9 <Eye className="size-4" />
10 Preview
11 </Tabs.Tab>
12 <Tabs.Tab value="code" className="flex items-center gap-2">
13 <Code className="size-4" />
14 Code
15 </Tabs.Tab>
16 </Tabs.List>
17 <Tabs.Panel value="preview">Rendered component</Tabs.Panel>
18 <Tabs.Panel value="code">Source code</Tabs.Panel>
19 </Tabs>
20 )
21}

Lazy panels that mount only when active

A useful property of the Drivn Tabs is that Tabs.Panel returns null when its value does not match the active tab — inactive panels are unmounted, not hidden with CSS. That means anything inside an inactive panel never renders, so its useEffect hooks, data fetches, and expensive children do not run until the user actually opens that tab. You get lazy mounting for free, with no Suspense boundary or conditional of your own.

This is the pattern for a tab that holds a heavy chart, a map, or a list that fetches on mount. Put the costly component inside the panel — optionally wrapped in a Card — and it stays dormant until selected; switch away and it unmounts again. For a tab that should keep its state across switches, lift that state into the parent instead, since the panel itself is torn down each time it goes inactive.

1import { Tabs } from "@/components/ui/tabs"
2import { HeavyChart } from "@/components/heavy-chart"
3
4export default function LazyTabs() {
5 return (
6 <Tabs defaultValue="summary">
7 <Tabs.List>
8 <Tabs.Tab value="summary">Summary</Tabs.Tab>
9 <Tabs.Tab value="analytics">Analytics</Tabs.Tab>
10 </Tabs.List>
11 <Tabs.Panel value="summary">Quick summary</Tabs.Panel>
12 <Tabs.Panel value="analytics">
13 <HeavyChart />
14 </Tabs.Panel>
15 </Tabs>
16 )
17}
Get started

Install Drivn in one command

Copy the source into your project and own every line. Zero runtime dependencies, pure React + Tailwind.

npx drivn@latest create

Requires Node 18+. Works with npm, pnpm, and yarn.

Enjoying Drivn?
Star the repo on GitHub to follow new component releases.
Star →

Frequently asked questions

Import { Tabs } from "@/components/ui/tabs", give the root a defaultValue, then nest a Tabs.List of Tabs.Tab triggers and one Tabs.Panel per tab. Each Tabs.Tab and Tabs.Panel share a value prop, and the panel whose value matches the active tab renders. The active value lives in context, so you do not wire any click handlers yourself.

Both. Pass defaultValue alone and the component tracks the active tab internally with React.useState. Pass value and onChange and it becomes controlled — the source reads const value = controlled ?? uncontrolled, so a supplied value takes over. Controlled mode is how you sync the active tab to a URL query param, a parent store, or a button elsewhere on the page.

No. Tabs.Panel returns null when its value does not match the active tab, so inactive panels are fully unmounted rather than hidden with CSS. Anything inside an inactive panel — effects, data fetches, heavy children — does not run until that tab is selected. This makes tabs a natural place to lazy-mount expensive content like charts or maps.

Yes. Tabs.Tab accepts children and a className and spreads remaining button attributes, so you render a lucide-react icon as a child next to the label. The base tab styles set no flex layout, so add className="flex items-center gap-2" on the Tab to align the icon with the text — cn() merges it with the component's own classes.

Three, selected with the variant prop on the Tabs root. default renders a bordered bg-accent pill track with a bg-background active tab, underline shows plain labels and underlines the active one, and compact uses small text-xs triggers with a bg-muted highlight. The prop is typed from the variantStyles keys, so a typo fails type-checking instead of rendering unstyled tabs.

It is a client component. The Tabs source starts with the 'use client' directive because it holds the active value in React.useState and shares it through a React context. Any page rendering Tabs includes that client component, but the panels inside can be as light or heavy as you like — and inactive panels stay unmounted until selected.