Tabs

Shows one panel at a time from a row of tabs.

Arrow keys move between tabs, and Home and End jump to the first and last. Name the list with aria-label.

Preview
basic.tsx
import { Tabs, TabsList, TabsPanel, TabsTab } from "~/components/ui/tabs"
import { Text } from "~/components/ui/text"

export default function Example() {
  return (
    <Tabs defaultValue="account" className="w-full max-w-md">
      <TabsList aria-label="Settings">
        <TabsTab value="account">Account</TabsTab>
        <TabsTab value="password">Password</TabsTab>
        <TabsTab value="billing" disabled>
          Billing
        </TabsTab>
      </TabsList>
      <TabsPanel value="account">
        <div className="rounded-lg border border-border p-lg">
          <Text size="sm">Change your name, photo and contact details.</Text>
        </div>
      </TabsPanel>
      <TabsPanel value="password">
        <div className="rounded-lg border border-border p-lg">
          <Text size="sm">Choose a new password. You'll be signed out on other devices.</Text>
        </div>
      </TabsPanel>
    </Tabs>
  )
}

Dependencies

What it's built from and what's built from it in Mocha, read from the imports in its source.

UsesTextprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20

Installation

Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/mocha/tabs.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/tabs.tsx
import {
  Tabs as TabsPrimitive,
  TabsList as TabsListPrimitive,
  TabsPanel as TabsPanelPrimitive,
  TabsTab as TabsTabPrimitive,
} from "@ui-sdk/react/tabs"
import type * as React from "react"

import { textVariants } from "~/components/ui/text"
import { cnState } from "~/lib/utils"

/** Holds a list of tabs and the panels they show. */
function Tabs({ className, ...props }: React.ComponentProps<typeof TabsPrimitive>) {
  return (
    <TabsPrimitive
      className={cnState("flex flex-col gap-sm data-[orientation=vertical]:flex-row", className)}
      {...props}
    />
  )
}

/** The row of tabs. Name it with `aria-label`. */
function TabsList({ className, ...props }: React.ComponentProps<typeof TabsListPrimitive>) {
  return (
    <TabsListPrimitive
      className={cnState(
        "inline-flex w-fit items-center gap-2xs rounded-lg bg-muted p-2xs text-muted-foreground data-[orientation=vertical]:flex-col data-[orientation=vertical]:items-stretch",
        className,
      )}
      {...props}
    />
  )
}

/** One tab. Its `value` matches the panel it shows. */
function TabsTab({ className, ...props }: React.ComponentProps<typeof TabsTabPrimitive>) {
  return (
    <TabsTabPrimitive
      className={cnState(
        [
          textVariants({ size: "xs", weight: "bold" }),
          "inline-flex h-7 cursor-pointer items-center justify-center gap-xs whitespace-nowrap rounded-md px-md",
          "transition-[color,background-color,box-shadow] duration-150",
          "hover:text-foreground",
          "outline-none focus-visible:ring-4 focus-visible:ring-ring",
          "data-selected:bg-background data-selected:text-foreground data-selected:shadow-sm",
          "data-disabled:cursor-not-allowed data-disabled:opacity-50",
        ].join(" "),
        className,
      )}
      {...props}
    />
  )
}

/** The content a tab shows. */
function TabsPanel({ className, ...props }: React.ComponentProps<typeof TabsPanelPrimitive>) {
  return (
    <TabsPanelPrimitive
      className={cnState(
        "flex-1 rounded-md outline-none focus-visible:ring-4 focus-visible:ring-ring",
        className,
      )}
      {...props}
    />
  )
}

export { Tabs, TabsList, TabsPanel, TabsTab }

API reference

Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Tabs

Holds a list of tabs and the panels they show.

activation

Type "manual" | "automatic"

className

Type string | ((state: TabsState<string>) => string)

controller

Type { getValue: () => string | null; setValue: (value: string | null, options?: TabsChangeOptions) => boolean; select: (value: string, options?: TabsChangeOptions) => boolean; focus: () => void; } | undefined

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultValue

Type string | null

hiddenUntilFound

Type boolean

onValueChange

Type ((value: string | null, details: { reason: "imperative"; event?: undefined; source: ChangeSource; cancel: () => void; readonly canceled: boolean; } | { reason: "item-press"; event?: MouseEvent | KeyboardEvent; source: ChangeSource; cancel: () => void; readonly canceled: boolean; } | { ...; } | { ...; } | { ...; }) =...

orientation

Type TabsOrientation

render

Type RenderProp<TabsState<string>>

style

Type CSSProperties | ((state: TabsState<string>) => CSSProperties)

value

Type string | null

TabsList

The row of tabs. Name it with aria-label.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<TabsListState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<TabsListState>) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<TabsListState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<TabsListState>) => CSSProperties)

TabsPanel

The content a tab shows.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<TabsPanelState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<TabsPanelState>) => string)

hiddenUntilFound

Whether the panel stays findable with hidden="until-found" while its tab isn't selected. Overrides the tabs'.

Type boolean

keepMounted

Whether the panel stays in the page, hidden, while its tab isn't selected.

Type boolean · Default false

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<TabsPanelState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<TabsPanelState>) => CSSProperties)

value*

The value of the tab this panel belongs to.

Type string

TabsTab

One tab. Its value matches the panel it shows.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<TabsTabState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<TabsTabState>) => string)

disabled

Keeps the tab focusable but stops it from being selected, as the tabs pattern asks.

Type boolean

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<TabsTabState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<TabsTabState>) => CSSProperties)

value*

Identifies the tab and pairs it with the panel of the same value.

Type string