Tabs
Switch views with panels, or a value with one compact control.
Installation
Terminal
npx shadcn@latest add https://fabrickit.org/r/tabs.jsonTerminal
pnpm dlx shadcn@latest add https://fabrickit.org/r/tabs.jsonTerminal
yarn shadcn@latest add https://fabrickit.org/r/tabs.jsonTerminal
bunx --bun shadcn@latest add https://fabrickit.org/r/tabs.jsonThis 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.
Step 1: Add the theme (once)
Every colour, radius and curve the components use, as --fabric-* variables. Save it as fabric.css next to your global CSS and import it there. If you've added another Fabric Kit component before, skip this.globals.css@import "./fabric.css";fabric.css/* * fabric.css — Fabric Kit's theme: every colour, radius, ring and curve the * components use, as --fabric-* variables (prefixed, so they never clash * with your own shadcn theme). Installed by `shadcn add @fabrickit/theme` * (it lands in your global CSS) — or import this file once, globally. * Dark by default. For light, put data-theme="light" on <html> (or on any * element: the tokens follow it down the tree; data-theme="dark" turns back). */ :root, [data-theme="dark"] { --fabric-page: #111111; --fabric-surface: #191919; /* triggers, recessed tiles */ --fabric-panel: #202020; /* open panels */ --fabric-raised: #2a2a2a; /* hover, range band */ --fabric-raised-hi: #343434; /* hover inside a band */ --fabric-preview: #252525; /* soft preview fills */ --fabric-text: #e0e0e0;Step 2: Copy the shared engines
Plain JavaScript that does the motion, shared between components. Create each file at the same path in your project; if a file is already there from another component, keep it.lib/fabricui/mount.js/* * mount.js — runs a Fabric Kit engine for a React component and cleans up * after it. The engines are plain JavaScript that wire themselves to the page * (listeners on the document, resize observers, a few elements of their own); * mount() records everything an engine attaches and changes while it's being * set up, and dispose() takes it all back, so a component can come and go (or * be set up twice in a row, as React's Strict Mode does) without leaving * anything behind. * * const m = mount(() => new Engine(root, options)); * m.instance; // the engine * m.dispose(); // on unmount: listeners and observers off, the DOM as it was, then instance.destroy() * // (and destroy() on anything it owns that registered itself, like each ClothDrop) */ export function mount(create) { const ac = new AbortController();lib/fabricui/cloth-drop.jsimport { Dither } from "./dither"; /** * cloth-drop.js — the library's open/close motion, shared by every component * that drops open (calendar, accordion, …). * * ClothDrop: the open/close motion. * * Open is a dropped cloth: the panel falls under gravity from behind the * trigger (the calendar rides its bottom edge), the top edge catches, the * fabric stretches a touch and springs back, a wave runs up from the hem and * the drape folds and sway it was thrown into settle. * * The falling cloth is one finely subdivided WebGL mesh, textured with the * browser's own rendering of the calendar (the real HTML + CSS + Geist, drawn * through an SVG foreignObject), so its text is pixel-identical to the page.lib/fabricui/dither.js/* * dither.js — Fabric Kit's dither, in one place. Every dithered thing (cloth * fold shade, shimmer, weaves, chart fills) uses this 4×4 ordered (Bayer) * pattern, so dots line up and read as one texture across components. * * Dither.B4 the 4×4 matrix, 0–15 * Dither.T4 its thresholds, centred: (v + 0.5) / 16 * Dither.at(x, y) the threshold for cell (x, y) * Dither.GLSL the same pattern for shaders: bayer4(cell) → 0–1 */ const B4 = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5]; const T4 = B4.map(v => v / 16 + 1 / 32); export const Dither = { B4, T4,lib/fabricui/goo-slide.js/* * goo-slide.js — the library's sliding highlight (Tabs, Radio cards). * * The highlight moves like a drop of liquid: its two edges are separate * springs. The edge on the side it's heading to is quick and the other is * lazier, so on the way the pill stretches toward its target, then the back * catches up and it settles to exactly the new option's size. While it's * stretched it squashes a touch the other way, like jelly. No blur, no * filters: just two springs, worked out every frame. * * const slide = new GooSlide(el, { axis: "x" }); // "x": side to side, "y": up and down * slide.to(optionEl); // animate there * slide.to(optionEl, true); // jump there (load, resize) * * The element is positioned with left/top 0 inside the options' container * (the option's offsetLeft/Top are used); the script sets its transform andStep 3: Copy the Tabs files
The component itself. Create each one at the same path in your project. It's your code from here on.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 {Step 4: Check the import paths
The files import each other through the @/ alias (the shadcn default, there in a new Next.js app). If your project doesn't have it, add it to tsconfig.json, or change the imports to relative paths.tsconfig.json{ "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["./*"] } } }Step 5: Use it
Import it from @/components/fabricui/tabs. The Usage example below shows it in a page.
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
| Prop | Type | Default |
|---|---|---|
| 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 | — |