Switch
A knob that leans into the move.
Preferences
Sound effects
Little clicks when things happen
Show previews
Peek at files when you hover them
Weekly digest
One email on Mondays
Beta features
Not available on your plan
Installation
Terminal
npx shadcn@latest add https://fabrickit.org/r/switch.jsonTerminal
pnpm dlx shadcn@latest add https://fabrickit.org/r/switch.jsonTerminal
yarn shadcn@latest add https://fabrickit.org/r/switch.jsonTerminal
bunx --bun shadcn@latest add https://fabrickit.org/r/switch.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();Step 3: Copy the Switch files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/switch.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { Switch as SwitchEngine } from "@/lib/fabricui/switch-engine"; import "./switch.css"; // each switch's engine, for its row to ask whether a press was a drag const engines = new WeakMap<HTMLButtonElement, SwitchEngine>(); const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect; export type SwitchProps = Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onChange" | "defaultChecked"> & { /** On or off (controlled). Leave out to let the switch keep its own state. */ checked?: boolean; /** The starting state when uncontrolled. */components/fabricui/switch.css/* * Switch — shared by every page that has one (Switch, Tabs, Popover). * * On is the text colour (like every primary control), off is the raised grey. The knob: * - stretches toward where it's going while you press (leaning into the move) * - slides over on a soft spring with a hair of overshoot * - can be dragged, following the pointer 1:1, and settles by where you let go * * Markup: * <button class="switch" type="button" role="switch" aria-checked="false" * aria-labelledby="…"><span class="switch__knob"></span></button> * Put data-switch-row on a row to make clicking its text toggle the switch. * switch.js wires up every .switch on the page. */ .switch:where(.fui-switch, .fui-switch *) { --w: 40px; --h: 24px; --pad: 3px; --knob: 18px; --stretch: 5px;lib/fabricui/switch-engine.js/* * switch-engine.js — the Switch engine (components/fabricui/switch.tsx renders the * markup and starts it; see switch.css for the markup). * Each switch fires a bubbling "change" event when it flips. */ /** * Switch: a button with role="switch". Click (or Space/Enter, which buttons * already turn into clicks) toggles it. Pressing stretches the knob; a * sideways drag moves it 1:1 and lets go wherever it is closer to. * Clicking the row (its label) toggles it as well. */ export class Switch { /** * @param {HTMLButtonElement} el * @param {{ onChange?: (on: boolean) => void }} [options] */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/switch. The Usage example below shows it in a page.
Code
components/fabricui/switch.tsx
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { Switch as SwitchEngine } from "@/lib/fabricui/switch-engine";
import "./switch.css";
// each switch's engine, for its row to ask whether a press was a drag
const engines = new WeakMap<HTMLButtonElement, SwitchEngine>();
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
export type SwitchProps = Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "onChange" | "defaultChecked"> & {
/** On or off (controlled). Leave out to let the switch keep its own state. */
checked?: boolean;
/** The starting state when uncontrolled. */components/fabricui/switch.css
/*
* Switch — shared by every page that has one (Switch, Tabs, Popover).
*
* On is the text colour (like every primary control), off is the raised grey. The knob:
* - stretches toward where it's going while you press (leaning into the move)
* - slides over on a soft spring with a hair of overshoot
* - can be dragged, following the pointer 1:1, and settles by where you let go
*
* Markup:
* <button class="switch" type="button" role="switch" aria-checked="false"
* aria-labelledby="…"><span class="switch__knob"></span></button>
* Put data-switch-row on a row to make clicking its text toggle the switch.
* switch.js wires up every .switch on the page.
*/
.switch:where(.fui-switch, .fui-switch *) {
--w: 40px; --h: 24px; --pad: 3px; --knob: 18px; --stretch: 5px;lib/fabricui/switch-engine.js
/*
* switch-engine.js — the Switch engine (components/fabricui/switch.tsx renders the
* markup and starts it; see switch.css for the markup).
* Each switch fires a bubbling "change" event when it flips.
*/
/**
* Switch: a button with role="switch". Click (or Space/Enter, which buttons
* already turn into clicks) toggles it. Pressing stretches the knob; a
* sideways drag moves it 1:1 and lets go wherever it is closer to.
* Clicking the row (its label) toggles it as well.
*/
export class Switch {
/**
* @param {HTMLButtonElement} el
* @param {{ onChange?: (on: boolean) => void }} [options]
*/Usage
Usage
"use client";
import * as React from "react";
import { Switch, SwitchRow } from "@/components/fabricui/switch";
export function Settings() {
const [sound, setSound] = React.useState(true);
return (
<SwitchRow>
<span id="sound">Sound effects</span>
<Switch checked={sound} onCheckedChange={setSound} aria-labelledby="sound" />
</SwitchRow>
);
}Props
| Prop | Type | Default |
|---|---|---|
| checked On or off, if you control it. | boolean | — |
| defaultChecked The starting state when you don't. | boolean | false |
| onCheckedChange Called when it flips. | (checked: boolean) => void | — |
| disabled Greys it out; it can't be pressed. | boolean | false |
| SwitchRow A row that toggles the Switch inside it when any of it is clicked. | component | — |