Skip to content
opsinjs
Edit this page

Components

Tabs

A row of tabs that switches between panels sharing one region. The list is one tab stop with a roving focus, and choosing a tab swaps the panel.

Usage

pnpm dlx shadcn@latest add @opsinjs/tabs

The @opsinjs namespace is declared in your components.json. Everything it installs is code you then own. There is no runtime package to keep in step.

import { Tabs } from "@/components/ui/tabs"
<Tabs
  label="Reading detail"
  value={panel}
  onValueChange={setPanel}
  items={[
    { value: "overview", label: "Overview", panel: <Overview /> },
    { value: "history", label: "History", panel: <History /> },
    { value: "notes", label: "Notes", panel: <Notes /> },
  ]}
/>

Examples

Theme
Density
Text
Status
Accent theme
Accent
tabs · base base · style base-lyraOpen under the product theme

Switching between panels

Three panels share one region, from items. Choosing a tab swaps the whole panel, not one setting of a view.

Theme
Density
Text
Status
Accent theme
Accent
tabs-switching-panels · base base · style base-lyraOpen under the product theme
tabs-switching-panels.tsx
"use client"

import { useState } from "react"

import { Tabs } from "@/components/ui/tabs"

const ITEMS = [
  {
    value: "summary",
    label: "Summary",
    panel:
      "A summary panel would gather the headline points here in a sentence or two. Choosing another tab hides this panel and shows a different one in the same space.",
  },
  {
    value: "detail",
    label: "Detail",
    panel:
      "A detail panel would carry the longer explanation, the kind a reader opens only when they want it. It is its own body of content, not a rearrangement of the summary above.",
  },
  {
    value: "sources",
    label: "Sources",
    panel:
      "A sources panel would list where the words came from. Nothing on any of these tabs is a measurement, a threshold or a unit, so none of it is a reading.",
  },
]

export default function TabsSwitchingPanels() {
  const [panel, setPanel] = useState("summary")

  return (
    <div className="w-full max-w-md">
      <Tabs label="Example panels" value={panel} onValueChange={setPanel} items={ITEMS} />
    </div>
  )
}

Tabs in a card

The card header names the subject and items fills its body, so a tab swaps the body without the card moving.

Theme
Density
Text
Status
Accent theme
Accent
tabs-in-a-card · base base · style base-lyraOpen under the product theme
tabs-in-a-card.tsx
"use client"

import { useState } from "react"

import { Tabs } from "@/components/ui/tabs"

const ITEMS = [
  {
    value: "about",
    label: "About",
    panel:
      "An about panel would describe the subject of this card in a short paragraph. It is the view a reader meets first.",
  },
  {
    value: "activity",
    label: "Activity",
    panel:
      "An activity panel would list what has happened recently. It replaces the about panel entirely rather than adding to it.",
  },
]

export default function TabsInACard() {
  const [panel, setPanel] = useState("about")

  return (
    <div className="w-full max-w-md rounded-opsin-lg border border-border bg-card px-opsin-4 py-opsin-3">
      <p className="m-0 mb-opsin-2 text-opsin-headline font-medium [color:var(--foreground)]">
        Example subject
      </p>
      <Tabs label="Example card views" value={panel} onValueChange={setPanel} items={ITEMS} />
    </div>
  )
}

When to use it

Use it when

  • Two or three distinct panels share one region and only one is worth showing at a time.
  • Folding secondary views of one subject, such as an overview, a history and notes, behind their own tabs.
  • Views a reader moves between often and expects to find in a familiar bar.

Do not use it when

  • The choice sets one parameter of a view that stays on the surface, such as a chart's week window.Use segmented-control instead.
  • The choice is a small set of exclusive options taken as a form input, submitted as a value.Use radio-group instead.
  • The panels are steps in a sequence, where progress and a next action matter most.Use stepper instead.

Accessibility

Base UI's Tabs carries the tested WAI-ARIA pattern and the wrapper overrides none of it. Activation is manual, three sighted carriers plus aria-selected mark the active tab, and the row draws neutral chrome with no status or category axis. The indicator glides to the chosen tab on spring-snap, and under prefers-reduced-motion it moves in one frame. Rendered, it shows no axe violation, target-size breach, focus gap or 320px reflow, and the audit changed nothing.

Residual items.

  • Each tab floors its height at --opsin-target-minimum with padding but sets no min-w, the house treatment for a word-labelled target under SC 2.5.8.
  • The label prop applies only as the tablist's aria-label, which is right here because every tab shows its own text.
  • Contrast pairs for the active ink, the rail and the indicator stay unmeasured.
KeyActionNotes
TabMoves focus into the tab list, onto the active tabOne tab stop for the list. The active panel is the next stop.
Shift+TabMoves focus back out of the tab listOne stop in reverse too, so focus leaves the row.
Arrow Right, Arrow DownMoves the highlight to the next tabNo panel swap. Wraps around, skips disabled tabs, mirrored under right-to-left.
Arrow Left, Arrow UpMoves the highlight to the previous tabWraps to the last tab, skips disabled tabs, swaps no panel.
Home, EndMoves the highlight to the first or last tabBoth skip disabled tabs and neither swaps the panel.
Enter, SpaceActivates the highlighted tab and shows its panelThe commit step of manual activation.
Contrast: 36 measured token pairs, 15 below floor
PairThemeAPCA LcWCAG 2.2Floor
body text on the pagelight101.617.18:1Pass
body text on the pagedark-100.518.00:1Pass
body text on a cardlight104.717.96:1Pass
body text on a carddark-99.616.32:1Pass
body text on the muted groundlight98.116.32:1Pass
body text on the muted grounddark-97.513.77:1Pass
secondary text on the pagelight83.97.42:1Pass
secondary text on the pagedark-80.713.44:1Pass
secondary text on a cardlight87.07.76:1Pass
secondary text on a carddark-79.812.19:1Pass
secondary text on the muted groundlight80.47.05:1Pass
secondary text on the muted grounddark-77.810.28:1Pass
a hairline boundary on the pagelight19.31.41:1Below floor
a hairline boundary on the pagedark-8.21.90:1Below floor
a hairline boundary on a cardlight22.41.47:1Below floor
a hairline boundary on a carddark-7.31.72:1Below floor
a hairline boundary on the muted groundlight15.81.34:1Below floor
a hairline boundary on the muted grounddark0.01.45:1Below floor
a placeholder boundary on the pagelight69.84.52:1Pass
a placeholder boundary on the pagedark-51.07.62:1Pass
a placeholder boundary on a cardlight72.84.72:1Pass
a placeholder boundary on a carddark-50.16.91:1Pass
a placeholder boundary on the muted groundlight66.24.29:1Pass
a placeholder boundary on the muted grounddark-48.15.83:1Pass
a placeholder fill on the pagelight8.31.18:1Below floor
a placeholder fill on the pagedark-8.21.90:1Below floor
a placeholder fill on a cardlight11.41.23:1Below floor
a placeholder fill on a carddark-7.31.72:1Below floor
a placeholder fill on the muted groundlight0.01.12:1Below floor
a placeholder fill on the muted grounddark0.01.45:1Below floor
a placeholder boundary on its own filllight58.93.84:1Pass
a placeholder boundary on its own filldark-41.54.01:1Below floor
the card hairline on the pagelight69.84.52:1Pass
the card hairline on the pagedark-28.74.19:1Below floor
the card hairline on a cardlight72.84.72:1Pass
the card hairline on a carddark-27.93.80:1Below floor

These are the measured token pairs this component draws colour from, not a measurement of the component itself.

Props interface

Prop

Type

Generated from TabsProps in registry/bases/base/tabs.tsx.

value and onValueChange make this controlled, with no internal selection state. A value matching no item renders no active tab and warns in development. items holds each tab's value, label and panel together.