Tabs

Switch views with panels, or a value with one compact control.

Account

Update your name and how people find you.

Password

Use at least 12 characters. You'll stay signed in here.

Notifications

Choose what reaches your inbox.

Comments and mentions
When someone replies or tags you
Product updates
A short note when something ships
Email digest

One email every Monday morning.

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/tabs.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/tabs.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { Tabs as Engine } from "@/lib/fabricui/tabs-engine";
import { GooSlide } from "@/lib/fabricui/goo-slide";
import "./tabs.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
const cx = (...c: (string | false | undefined)[]) => c.filter(Boolean).join(" ");

export type TabItem = { value: string; label: React.ReactNode; content: React.ReactNode };

export type TabsProps = {
  items: TabItem[];
  /** The open tab (controlled). */
components/fabricui/tabs.css
/*
 * tabs.css — Tabs (tabs.js). Moved out of tabs.html.
 */
/*
 * Tabs. The highlight slides to the chosen tab; the panel stays put and
 * its new content ripples in from the side it came from (cloth-drop.js
 * swapIn(): the content fades in, settling a few px across, while a wave
 * runs through the fabric). Panels share one height, so nothing jumps.
 */

/* ---------- Segmented: the same tabs, as one compact control ---------- */
.seg-screen:where(.fui-tabs, .fui-tabs *) { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; }
.seg-label:where(.fui-tabs, .fui-tabs *) { font-size: 12.5px; line-height: 16px; color: var(--fabric-text-mid); }
.segmented:where(.fui-tabs, .fui-tabs *) {
  position: relative;
  display: inline-flex;
lib/fabricui/tabs-engine.js
/*
 * tabs-engine.js — Tabs (tabs.css). From tabs.js.
 * Changes for React: panels are found inside the root (not by document id),
 * onChange reports the new tab, a `start` tab can be given, and a value set
 * from code goes through select().
 */
import { ClothDrop } from "./cloth-drop";
import { GooSlide } from "./goo-slide";

/**
 * Tabs: automatic activation (arrow keys move and select, wrapping; Home and
 * End jump). The highlight slides under the chosen tab; the new panel's
 * content ripples in from that side. Typed values are written back into the
 * markup when you leave a tab, so its cloth shows what the fields hold.
 */
export class Tabs {

Usage

Usage
"use client";

import * as React from "react";
import { Tabs, Segmented } from "@/components/fabricui/tabs";

export function Settings() {
  return (
    <Tabs
      label="Settings"
      items={[
        { value: "account", label: "Account", content: <AccountForm /> },
        { value: "password", label: "Password", content: <PasswordForm /> },
      ]}
    />
  );
}

export function Digest() {
  const [freq, setFreq] = React.useState("weekly");
  return (
    <Segmented
      aria-label="Email digest"
      value={freq}
      onValueChange={setFreq}
      options={[{ value: "daily", label: "Daily" }, { value: "weekly", label: "Weekly" }, { value: "never", label: "Never" }]}
    />
  );
}

Props

PropTypeDefault
items

Tabs: the tabs and their panels. Panels share one height (the tallest), so nothing jumps.

{ value, label, content }[]—
label

Tabs: the tab list's name, for screen readers.

string—
value

The open tab (Segmented: the choice), if you control it.

string—
defaultValue

The starting one when you don't (the first if left out).

string—
onValueChange

Called when another is chosen.

(value: string) => void—
Segmented

One compact control for a value: native radios under a white pill that slides like a drop. Takes options, value, defaultValue, onValueChange, name.

component—