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.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/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

PropTypeDefault
checked

On or off, if you control it.

boolean—
defaultChecked

The starting state when you don't.

booleanfalse
onCheckedChange

Called when it flips.

(checked: boolean) => void—
disabled

Greys it out; it can't be pressed.

booleanfalse
SwitchRow

A row that toggles the Switch inside it when any of it is clicked.

component—