Updated
Theme Switch
Customize
Circle
Reveal duration0.50s
Auto
Size40px
Radius12px
Installation
npx shadcn@latest add https://uibrik.com/r/theme-switch.jsonInstall the packages
npm install motionAdd the cn helper
Imports
cnfrom@/lib/utils, whichshadcn initcreates. Add it by hand without shadcn.Copy the source into your project
components/uibrik/theme-switch/theme-switch.tsx"use client"; import { motion } from "motion/react"; import { useId, useRef, useSyncExternalStore } from "react"; import { flushSync } from "react-dom"; import { cn } from "@/lib/utils"; const EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1]; export type Theme = "light" | "dark"; /** * How the new theme takes over the page. * - circle: grows out from the button * - expand: the button's rounded box grows to fill the page * - split: a band opens out from the button, either across or down the page * - wipe: a straight edge sweeps across the page * - diagonal: sweeps in from a corner * - blinds: slats open across the page, together or one after another * - blur: the old page blurs away as the new one comes into focus * - zoom: the old page sinks back as the new one settles forward * - none: switches instantly */ export type RevealMode = "circle" | "expand" | "split" | "wipe" | "diagonal" | "blinds" | "blur" | "zoom" | "none";
Adds the files to
components/uibrik/theme-switch and installs motion. With the @uibrik namespace set up: npx shadcn@latest add @uibrik/theme-switch.Usage
import { ThemeSwitch } from "@/components/uibrik/theme-switch/theme-switch";<ThemeSwitch reveal="circle" revealDuration={0.5} easing="auto" size={40} radius={12} storageKey="theme" onChange={(theme) => {}} // theme="light" // setTheme={(theme, origin) => {}}/>Where to use Theme Switch
Light and dark toggles
The sun eclipsing into a moon while the new theme spreads from the button makes switching delightful.
Pairs with Tactile Toggle
Site headers
A compact icon button fits any navbar.
Pairs with Floating Navbar
Docs and blogs
Readers who switch themes often get a clear, quick toggle.
Props
ThemeSwitch- reveal
- Type "circle" | "expand" | "split" | "wipe" | "diagonal" | "blinds" | "blur" | "zoom" | "none"
- "circle"
- How the new theme takes over the page.
- revealDuration
- Type number
- 0.5
- How long the reveal takes.
- easing
- Type "auto" | "out" | "in-out" | "linear"
- "auto"
- Auto uses each mode's own curve: ease-out for reveals from a point, ease-in-out for sweeps.
- wipeFrom
- Type "left" | "right" | "top" | "bottom"
- "left"
- Which edge of the page the wipe starts from.
- corner
- Type "top-left" | "top-right" | "bottom-left" | "bottom-right"
- "top-left"
- Which corner the diagonal sweeps in from.
- orientation
- Type "horizontal" | "vertical"
- "horizontal"
- Split: a band opening up and down, or sideways. Blinds: rows or columns.
- slats
- Type number
- 8
- How many slats the blinds have.
- cascade
- Type number
- 0
- 0 opens every slat together; higher opens them one after another.
- blurAmount
- Type number
- 8
- How far the pages blur.
- depth
- Type number
- 0.04
- How far the pages scale (0.04 is 4%).
- size
- Type number
- 40
- Width and height.
- radius
- Type number
- 12
- Corner radius.
- theme
- Type "light" | "dark"
- –
- Controlled theme. Leave unset and the button follows the page's own theme (the dark class on <html>).
- onChange
- Type (theme: "light" | "dark") => void
- –
- Called with the theme being switched to.
- setTheme
- Type (theme: "light" | "dark", origin: { x: number; y: number }) => void
- –
- Applies the theme in place of the built-in page switch, e.g. a theme provider's setter. Gets the button's centre.
- storageKey
- Type string
- "theme"
- localStorage key the choice is saved under.
- className
- Type string
- –
- Classes for the button.