Flips light and dark in one press, in three forms
Click any switch, or press D with focus inside, to flip its card
pnpm dlx shadcn@latest add https://notchset.dev/r/theme-switch.jsonLight or dark, in one press, and the surface flips.
Every shadcn style · one file
Import it and pass your content. The props that matter most are listed next.
"use client";
import * as React from "react";
import { Monitor, Moon, Sun } from "@/lib/notchset/icons";
import { SegmentedControl } from "@/components/ui/notchset/segmented-control";
import { useLessMotion } from "@/lib/notchset/motion";
import { ThemeSwitch, type ThemeChoice } from "@/components/ui/notchset/theme-switch";
import { useSelfPlay } from "./self-play";
/*
* The three forms of one switch, stacked and sharing one state: the icon (its sun carved into a moon, stars by night),
* the pill (the knob glides carrying the same turn, the track inks to midnight) and the labelled button (its icon and
* words roll). A press flips the card they sit on, crossfading its colours, and never the page; at rest it follows the
* page, and System gives it back to the page. D flips it while focus is inside. With System draws the three choices
* side by side in the labelled button's row. Each row is the Large button's height, so neither Size nor With System
* moves the stage. Stars off keeps the icon's night plain. Shown once, the stage flips the card once and rests there.
*/
type Intent = "ghost" | "secondary";
type Size = "sm" | "md" | "lg";
/* the theme on the page now, read again whenever the root's data-theme changes */
function subscribe(change: () => void) {
const mo = new MutationObserver(change);
mo.observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] });
return () => mo.disconnect();
}
const pageTheme = () => (document.documentElement.dataset.theme === "dark" ? "dark" : "light");
export default function ThemeSwitchDemo() {
const intent = "ghost" as Intent;
const size = "md" as Size;
const system = false;
const stars = true;
const said = { name: "{theme} mode", light: "Light", dark: "Dark", system: "System" };
const words = said;
const page = React.useSyncExternalStore(subscribe, pageTheme, () => "light" as const);
/* the card's own theme once a switch is used; null follows the page */
const [own, setOwn] = React.useState<"light" | "dark" | null>(null);
const theme = own ?? page;
const device = own == null;
const pick = (v: ThemeChoice) => setOwn(v === "system" ? null : v);
const p = { value: theme as ThemeChoice, onValueChange: pick, labels: words };
const row = "flex h-[var(--ns-button-lg)] items-center justify-center";
const root = React.useRef<HTMLDivElement>(null);
const play = useSelfPlay(root, { beats: [0], off: useLessMotion() });
React.useEffect(() => {
if (play.mine || play.step !== 0) return;
/* the icon switch, pressed as a hand would */
root.current?.querySelector<HTMLElement>("button")?.click();
}, [play.step, play.mine]);
return (
<div
ref={root}
data-theme={theme}
className="flex w-[360px] max-w-full flex-col items-center gap-12 rounded-[20px] bg-[var(--ns-canvas)] px-8 py-10 text-[var(--ns-ink)] shadow-[inset_0_0_0_1px_var(--ns-line)] transition-[background-color,color,box-shadow] duration-[320ms] ease-[var(--ns-ease)]"
onPointerDownCapture={play.handOver}
onKeyDownCapture={play.handOver}
onKeyDown={(e) => {
if (e.key.toLowerCase() !== "d" || e.metaKey || e.ctrlKey || e.altKey) return;
e.preventDefault();
pick(theme === "dark" ? "light" : "dark");
}}
>
<div className={row}>
<ThemeSwitch variant="icon" intent={intent} size={size} stars={stars} {...p} />
</div>
<div className={row}>
<ThemeSwitch variant="pill" {...p} />
</div>
<div className={row}>
{system ? (
<SegmentedControl
label="Theme"
value={device ? "system" : theme}
onValueChange={pick}
options={[
{ value: "light", label: words.light, icon: <Sun aria-hidden /> },
{ value: "dark", label: words.dark, icon: <Moon aria-hidden /> },
{ value: "system", label: words.system, icon: <Monitor aria-hidden /> },
]}
/>
) : (
<ThemeSwitch variant="label" size={size} {...p} />
)}
</div>
<span className="sr-only" role="status">
{theme === "dark" ? `${words.dark} mode on` : `${words.light} mode on`}
</span>
</div>
);
}"use client";
import * as React from "react";
import { Monitor, Moon, Sun } from "@/lib/notchset/icons";
import { SegmentedControl } from "@/components/ui/notchset/segmented-control";
import { useLessMotion } from "@/lib/notchset/motion";
import { ThemeSwitch, type ThemeChoice } from "@/components/ui/notchset/theme-switch";
import { useSelfPlay } from "./self-play";
/*
* The three forms of one switch, stacked and sharing one state: the icon (its sun carved into a moon, stars by night),
* the pill (the knob glides carrying the same turn, the track inks to midnight) and the labelled button (its icon and
* words roll). A press flips the card they sit on, crossfading its colours, and never the page; at rest it follows the
* page, and System gives it back to the page. D flips it while focus is inside. With System draws the three choices
* side by side in the labelled button's row. Each row is the Large button's height, so neither Size nor With System
* moves the stage. Stars off keeps the icon's night plain. Shown once, the stage flips the card once and rests there.
*/
type Intent = "ghost" | "secondary";
type Size = "sm" | "md" | "lg";
/* the theme on the page now, read again whenever the root's data-theme changes */
function subscribe(change: () => void) {
const mo = new MutationObserver(change);
mo.observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] });
return () => mo.disconnect();
}
const pageTheme = () => (document.documentElement.dataset.theme === "dark" ? "dark" : "light");
export default function ThemeSwitchDemo() {
const intent = "ghost" as Intent;
const size = "md" as Size;
const system = false;
const stars = true;
const said = { name: "{theme} mode", light: "Light", dark: "Dark", system: "System" };
const words = said;
const page = React.useSyncExternalStore(subscribe, pageTheme, () => "light" as const);
/* the card's own theme once a switch is used; null follows the page */
const [own, setOwn] = React.useState<"light" | "dark" | null>(null);
const theme = own ?? page;
const device = own == null;
const pick = (v: ThemeChoice) => setOwn(v === "system" ? null : v);
const p = { value: theme as ThemeChoice, onValueChange: pick, labels: words };
const row = "flex h-[var(--ns-button-lg)] items-center justify-center";
const root = React.useRef<HTMLDivElement>(null);
const play = useSelfPlay(root, { beats: [0], off: useLessMotion() });
React.useEffect(() => {
if (play.mine || play.step !== 0) return;
/* the icon switch, pressed as a hand would */
root.current?.querySelector<HTMLElement>("button")?.click();
}, [play.step, play.mine]);
return (
<div
ref={root}
data-theme={theme}
className="flex w-[360px] max-w-full flex-col items-center gap-12 rounded-[20px] bg-[var(--ns-canvas)] px-8 py-10 text-[var(--ns-ink)] shadow-[inset_0_0_0_1px_var(--ns-line)] transition-[background-color,color,box-shadow] duration-[320ms] ease-[var(--ns-ease)]"
onPointerDownCapture={play.handOver}
onKeyDownCapture={play.handOver}
onKeyDown={(e) => {
if (e.key.toLowerCase() !== "d" || e.metaKey || e.ctrlKey || e.altKey) return;
e.preventDefault();
pick(theme === "dark" ? "light" : "dark");
}}
>
<div className={row}>
<ThemeSwitch variant="icon" intent={intent} size={size} stars={stars} {...p} />
</div>
<div className={row}>
<ThemeSwitch variant="pill" {...p} />
</div>
<div className={row}>
{system ? (
<SegmentedControl
label="Theme"
value={device ? "system" : theme}
onValueChange={pick}
options={[
{ value: "light", label: words.light, icon: <Sun aria-hidden /> },
{ value: "dark", label: words.dark, icon: <Moon aria-hidden /> },
{ value: "system", label: words.system, icon: <Monitor aria-hidden /> },
]}
/>
) : (
<ThemeSwitch variant="label" size={size} {...p} />
)}
</div>
<span className="sr-only" role="status">
{theme === "dark" ? `${words.dark} mode on` : `${words.light} mode on`}
</span>
</div>
);
}"use client";
import * as React from "react";
/*
* A demo that plays itself until the first touch: the script lives in the demo, never in the piece, which only gets
* the props a person would give it. It moves 0.4 s after the piece is first seen, then takes each step on its beat;
* off screen or paused it waits. The first press, key or focus on the piece (bind `handOver` to it; a scroll over it never counts) makes it the
* visitor's for good and drops every queued step; with less motion (`off`) it never plays and rests in its first frame.
* A host can hand it over from outside the piece too (a settings panel beside it the visitor has used): under
* <HandedOver value>, it never plays, a remount included.
*
* const play = useSelfPlay(root, { beats: [2800, 2800], loop: true, off: less });
* <div ref={root} onPointerDownCapture={play.handOver} onKeyDownCapture={play.handOver} onFocusCapture={play.handOver}>
* play.step // the step it is at, -1 before its first
* play.control // "pause" while it plays, "play" once paused (a new take), "replay" once it is over or yours
*
* Each step presses the piece's own controls (pressIn), so the script shows exactly what a hand can do.
*
* A host that shows the piece once (<PlayOnce value={{ settle }}>) changes three things: the script starts when the
* piece is first half in view and never again on coming back, it never loops, and with less motion (or `settle`) it
* takes every step at once, so the piece rests on the END of its moment. `hold` is a reason not to play at all that is
* not motion (a mode with nothing to show): it holds in every host. Under <PlayOnce value={{ active: false }}> (a look
* that is mounted but not shown) it waits until it is shown.
*/
type SelfPlayOptions = {
/** how long each step waits after the one before it, in ms; the first step waits 0.4 s after the piece is seen */
beats: readonly number[];
/** starts over after the last step; without it the script plays once and rests */
loop?: boolean | undefined;
/** no self-play at all (less motion): the piece is the visitor's from the start */
off: boolean;
/** no self-play for a reason other than motion (a mode with nothing to show), in every host */
hold?: boolean | undefined;
};
/** How a host shows the piece: once on arrival (see useSelfPlay). */
type PlayOnceHost = {
/** rest on the end at once, without playing (less motion, or a reset) */
settle?: boolean | undefined;
/** false while the piece is mounted but not shown: it waits */
active?: boolean | undefined;
};
const START = 400;
/**
* Whether a demo plays itself at all. Off: every piece rests in its first frame and is
* the visitor's from the start; nothing presses its controls for them.
*/
const SELF_PLAY: boolean = false;
/** True where the visitor has already taken the piece from outside it: nothing under it plays itself. */
const HandedOver = React.createContext(false);
/** A host that shows the piece once: play once on arrival, rest on the end (null: play as the demo asks). */
const PlayOnce = React.createContext<PlayOnceHost | null>(null);
function useSelfPlay(root: React.RefObject<Element | null>, { beats, loop: looped = false, off: still, hold = false }: SelfPlayOptions) {
const once = React.useContext(PlayOnce);
const active = once?.active !== false;
/* shown once: less motion (or a settled host) takes every step at once and rests on the end */
const quick = once != null && (still || Boolean(once.settle));
const loop = once == null && looped;
/* the first touch hands it over for good */
const [mine, setMine] = React.useState(false);
const handOver = React.useCallback(() => setMine(true), []);
const handedOver = React.useContext(HandedOver);
const off = !SELF_PLAY || hold || (quick ? false : still) || mine || (handedOver && !quick);
const [seen, setSeen] = React.useState(false);
const [paused, setPaused] = React.useState(false);
const [taken, setTaken] = React.useState(0);
const n = Math.max(1, beats.length);
const done = !loop && taken >= n;
React.useEffect(() => {
const el = root.current;
if (!active) return;
/* without an observer (an old browser, a test) it counts as seen */
if (!el || typeof IntersectionObserver === "undefined" || quick) {
const t = window.setTimeout(() => setSeen(true));
return () => window.clearTimeout(t);
}
/* shown once: first half in view starts it, and leaving never stops or restarts it */
const io = once
? new IntersectionObserver((es) => {
if (es.some((e) => e.isIntersecting && e.intersectionRatio >= 0.5)) {
setSeen(true);
io.disconnect();
}
}, { threshold: 0.5 })
: new IntersectionObserver((es) => setSeen(es.some((e) => e.isIntersecting)));
io.observe(el);
return () => io.disconnect();
}, [root, active, once, quick]);
const playing = !off && seen && active && !paused && !done;
const delay = quick ? 0 : taken === 0 ? START : (beats[taken % n] ?? START);
React.useEffect(() => {
if (!playing) return;
const t = window.setTimeout(() => setTaken((k) => k + 1), delay);
return () => window.clearTimeout(t);
}, [playing, taken, delay]);
/* one control in one place: Pause while it plays, Play (a new take) once paused, Replay once it is over or yours */
/* self-play off: never "over", so a demo rests in its FIRST frame, not its end frame */
const control: "pause" | "play" | "replay" = paused || !SELF_PLAY ? "play" : off || done ? "replay" : "pause";
return { step: taken === 0 ? -1 : (taken - 1) % n, taken, playing, control, mine, handOver, pause: () => setPaused(true) };
}
/**
* Presses a real control inside `root`, as the visitor would: the first match of `selector`, or the one whose text is
* `text`. A click only, so it never counts as the visitor's touch.
*/
function pressIn(root: Element | null, selector: string, text?: string) {
const all = [...(root?.querySelectorAll<HTMLElement>(selector) ?? [])];
(text == null ? all[0] : all.find((el) => el.textContent?.trim() === text))?.click();
}
export { HandedOver, PlayOnce, pressIn, useSelfPlay, type PlayOnceHost, type SelfPlayOptions };This example also uses Segmented control. This command installs them with it, so the example pastes as it is:
pnpm dlx shadcn@latest add https://notchset.dev/r/theme-switch.json https://notchset.dev/r/segmented-control.jsoncomponents/ui/notchset/theme-switch.tsx
LevelOrganism: made of molecules or blocks.
Made ofButton,Icon button
Free, no account or key. One command for every shadcn style. With the @notchset registry added, @notchset/<name> works too.
Works with React 18.3 or 19 on Tailwind CSS v4, in any shadcn project. What's tested
pnpm add lucide-react@^1The CLI adds the styles to the CSS file named in components.json (tailwind.css). Check that it points at your global stylesheet, that the project uses Tailwind CSS v4, and that the file is imported in your root layout. Installed by hand: paste styles.css into that file.
value–
ThemeChoicethe choice (controlled, with onValueChange)
defaultValue"system"
ThemeChoicethe choice at first (uncontrolled); system by default, shown as the theme in use
onValueChange–
(value: ThemeChoice) => voidcalled with the choice: light or dark, and system when `system` is on
variant"icon"
"icon" | "pill" | "label"icon: an icon button (a top bar); pill: a switch (a settings row); label: a button that names where a press goes (a menu, a settings card)
starstrue
booleanthe icon form's stars by night (on by default); off where the switch sits in a dense bar
intent–
"ghost" | "secondary"ghost (a top bar, the default) or secondary (outlined, on its own)
size"md"
"sm" | "md" | "lg"sm, md or lg: the height of the buttons beside it
systemfalse
booleanadds the device's own theme as a third step: light, dark, system
labels–
Partial<ThemeSwitchLabels>its built-in words, English by default; {theme} in `name` is where a press goes
Three usesan app's top bar · an account menu's header · a docs page's header
Give your coding assistant the Markdown guide instead of screenshots.
https://notchset.dev/api/mcp (setup)v2.0.0 · 2026-11-17breaking
No longer ships button-look.ts (ButtonBin, ButtonBusy, ButtonPlane, ButtonPuff, ButtonRoll, ButtonSparks, ButtonTick, EdgeBurst, QUIET_PILL, buttonBase, buttonIntents, buttonResults, buttonRollLayer, buttonSizes, buttonSquare, play, ButtonRollState, useButtonRoll), which it stopped using; a copy that imported them through this item imports them from button-class.ts and button-move.ts.ts. Ships button-move.ts (ButtonRoll, buttonRollLayer, play, ButtonRollState, useButtonRoll), which it now uses. Internal files split; installs smaller; no change to the piece's own API. Renamed: . recheck: copies that import button-look.ts through theme-switch
v1.1.0 · 2026-11-17
New props: stars. Ships button-look.ts (ButtonBin, ButtonBusy, ButtonPlane, ButtonPuff, ButtonRoll, ButtonSparks, ButtonTick, EdgeBurst, QUIET_PILL, buttonBase, buttonIntents, buttonResults, buttonRollLayer, buttonSizes, buttonSquare, play, ButtonRollState, useButtonRoll), which it now uses. Ships hit-area.ts (HIT_AREA), which it now uses. Ships motion.ts (EASE), which it now uses.
v1.0.0 · 2026-11-09
Released free with the Notchset system.