主题选择器
跟随系统、浅色、深色三张卡片,每张都是缩小的真实界面,跟随系统是左深右浅各一半。
适合使用的场景
- 设置页或页头的外观切换
- 想让用户选择前就看到效果,而不是读“浅色”“深色”两个字
- ThemeSwitch:页头一键切换浅色深色,或作为落地页上的主视觉
注意事项
- 把 themeScript() 内联到 <head>,否则刷新时会先闪一下错误的主题
- Tailwind 的 dark 变体要改成识别 .dark class,与 shadcn 默认一致
- ThemeSwitch 只有浅色和深色,value 请传 useThemeMode().resolved,跟随系统时才会停在正确的一边
交给你的 agent 安装
一次复制就包含安装命令、使用时机、props 与设计规则。粘贴到 Claude Code、Cursor 或 Codex 即可。
用 CLI 安装
使用 shadcn CLI。源码会复制到你的项目中,每一行都由你掌控。
$ npx shadcn@latest add https://sunui.synvize.com/r/theme-picker.json或手动安装
- 1. 安装依赖。
$ npm install clsx tailwind-merge framer-motion - 2. 如果还没有,在
lib/utils.ts添加cn辅助函数。没有用 shadcn/ui?再加上 sunui-theme.css 提供颜色变量(做法)。然后复制组件代码。theme-picker.tsx"use client"; import { AnimatePresence, motion, useReducedMotion } from "framer-motion"; import { useCallback, useEffect, useRef, useState, type KeyboardEvent } from "react"; import { flushSync } from "react-dom"; import { cn } from "@/lib/utils"; export type ThemeMode = "system" | "light" | "dark"; const MODES: ThemeMode[] = ["system", "light", "dark"]; const defaultLabels: Record<ThemeMode, string> = { system: "System", light: "Light", dark: "Dark" }; /* ------------------------------------------------------------------ */ /* Theme plumbing: same convention as next-themes and shadcn/ui. */ /* <html class="dark"> or class="light" forces a theme, no class means */ /* follow the OS. Your Tailwind `dark:` variant should match it: */ /* @custom-variant dark (&:where(.dark, .dark *)); */ /* Inline themeScript() from theme-script.ts in <head> to avoid a */ /* flash of the wrong theme on reload. */ /* ------------------------------------------------------------------ */ /** Put the theme class on <html>. "system" removes it so media queries decide. */ export function applyThemeMode(mode: ThemeMode) { const root = document.documentElement; root.classList.remove("light", "dark"); if (mode !== "system") root.classList.add(mode); root.style.colorScheme = mode === "system" ? "" : mode; } function readStored(storageKey: string): ThemeMode { try { const v = localStorage.getItem(storageKey); return v === "light" || v === "dark" ? v : "system"; } catch { return "system"; } } export interface UseThemeModeOptions { storageKey?: string; /** Crossfade the whole page with the View Transitions API. Skipped for reduced motion. */ crossfade?: boolean; } /** Read and set the theme mode. `resolved` is what is actually showing: "light" or "dark". */ export function useThemeMode({ storageKey = "theme", crossfade = true }: UseThemeModeOptions = {}) { const [mode, setModeState] = useState<ThemeMode>("system"); const [systemDark, setSystemDark] = useState(false); useEffect(() => { setModeState(readStored(storageKey)); const media = window.matchMedia("(prefers-color-scheme: dark)"); setSystemDark(media.matches); const onMedia = () => setSystemDark(media.matches); const onStorage = (e: StorageEvent) => { if (e.key === storageKey) setModeState(readStored(storageKey)); }; media.addEventListener("change", onMedia); window.addEventListener("storage", onStorage); return () => { media.removeEventListener("change", onMedia); window.removeEventListener("storage", onStorage); }; }, [storageKey]); const setMode = useCallback( (next: ThemeMode) => { const commit = () => { applyThemeMode(next); try { if (next === "system") localStorage.removeItem(storageKey); else localStorage.setItem(storageKey, next); } catch { /* private mode: the class still applies for this page */ } flushSync(() => setModeState(next)); }; const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches; if (crossfade && !reduce && typeof document.startViewTransition === "function") document.startViewTransition(commit); else commit(); }, [storageKey, crossfade], ); const resolved: "light" | "dark" = mode === "system" ? (systemDark ? "dark" : "light") : mode; return { mode, setMode, resolved }; } /* ------------------------------------------------------------------ */ /* Picker: each option is a tiny version of the real interface, so */ /* people see the result before they choose. System is split in half. */ /* ------------------------------------------------------------------ */ // The previews show a specific theme, so their colors are fixed on purpose. const SWATCH = { light: { bg: "#f4f4f4", bar: "#ffffff", edge: "#e4e4e4", ink: "#a3a3a3" }, dark: { bg: "#262626", bar: "#3a3a3a", edge: "#444444", ink: "#8f8f8f" }, } as const; function MiniUi({ tone }: { tone: "light" | "dark" }) { const c = SWATCH[tone]; return ( <div className="absolute inset-0 flex items-center px-[14%]" style={{ background: c.bg }}> <div className="flex h-[38%] w-full items-center gap-[8%] rounded-full px-[10%]" style={{ background: c.bar, boxShadow: `inset 0 0 0 1px ${c.edge}` }}> <svg viewBox="0 0 12 12" className="h-[55%] shrink-0" fill="none" stroke={c.ink} strokeWidth={1.6} aria-hidden="true"> <circle cx="5.2" cy="5.2" r="3.4" /> <path d="m7.8 7.8 2.6 2.6" strokeLinecap="round" /> </svg> <span className="h-[18%] w-[45%] rounded-full" style={{ background: c.edge }} /> </div> </div> ); } function Preview({ mode }: { mode: ThemeMode }) { if (mode !== "system") return <MiniUi tone={mode} />; return ( <> <MiniUi tone="light" /> <div className="absolute inset-0" style={{ clipPath: "inset(0 50% 0 0)" }}> <MiniUi tone="dark" /> </div> </> ); } export interface ThemePickerProps { value: ThemeMode; onValueChange: (mode: ThemeMode) => void; labels?: Partial<Record<ThemeMode, string>>; /** sm fits a header popover, md fits a settings page */ size?: "sm" | "md"; "aria-label"?: string; className?: string; } export function ThemePicker({ value, onValueChange, labels, size = "md", "aria-label": ariaLabel = "Theme", className }: ThemePickerProps) { const reduce = useReducedMotion(); const refs = useRef<(HTMLButtonElement | null)[]>([]); const text = { ...defaultLabels, ...labels }; const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => { const step = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 }[e.key]; if (!step) return; e.preventDefault(); const next = MODES[(MODES.indexOf(value) + step + MODES.length) % MODES.length]; onValueChange(next); refs.current[MODES.indexOf(next)]?.focus(); }; return ( <div role="radiogroup" aria-label={ariaLabel} onKeyDown={onKeyDown} className={cn("flex", size === "sm" ? "gap-2" : "gap-3", className)}> {MODES.map((mode, i) => { const on = value === mode; return ( <button key={mode} ref={(el) => { refs.current[i] = el; }} type="button" role="radio" aria-checked={on} tabIndex={on ? 0 : -1} onClick={() => onValueChange(mode)} className="group flex flex-col items-center gap-1.5 rounded-lg outline-none" > <span className={cn( "relative block overflow-hidden rounded-md transition-[box-shadow,transform] duration-150 group-hover:-translate-y-px group-focus-visible:ring-2 group-focus-visible:ring-ring group-focus-visible:ring-offset-2 group-focus-visible:ring-offset-background", size === "sm" ? "h-10 w-16" : "h-14 w-24", on ? "shadow-[0_0_0_2px_var(--foreground)]" : "shadow-[0_0_0_1px_var(--border)] group-hover:shadow-[0_0_0_1px_var(--muted-foreground)]", )} > <Preview mode={mode} /> <AnimatePresence initial={false}> {on && ( <motion.span key="check" initial={reduce ? false : { scale: 0.4, opacity: 0 }} animate={{ scale: 1, opacity: 1 }} exit={reduce ? { opacity: 0 } : { scale: 0.4, opacity: 0, transition: { duration: 0.1 } }} transition={{ type: "spring", stiffness: 520, damping: 30 }} className="absolute bottom-1 right-1 grid size-4 place-items-center rounded-full bg-foreground text-background shadow-[0_0_0_2px_var(--background)]" > <svg viewBox="0 0 12 12" className="size-2.5" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"> <path d="m2.5 6.2 2.3 2.3 4.7-5" /> </svg> </motion.span> )} </AnimatePresence> </span> <span className={cn("text-xs transition-colors", on ? "font-medium text-foreground" : "text-muted-foreground group-hover:text-foreground")}>{text[mode]}</span> </button> ); })} </div> ); }theme-script.ts// Plain module with no client directive: it runs on the server, so a root layout can inline it. // <head><script dangerouslySetInnerHTML={{ __html: themeScript("theme") }} /></head> // It applies the saved theme before the first paint, and follows changes made in // other tabs and in same-origin iframes. Use the same storageKey as useThemeMode(). export function themeScript(storageKey = "theme") { const k = JSON.stringify(storageKey); return `(()=>{const a=m=>{const r=document.documentElement;r.classList.remove("light","dark");if(m==="light"||m==="dark"){r.classList.add(m);r.style.colorScheme=m}else{r.style.colorScheme=""}};try{a(localStorage.getItem(${k}))}catch(e){}addEventListener("storage",e=>{if(e.key===${k})a(e.newValue)})})()`; }theme-switch.tsx"use client"; import { motion, useMotionTemplate, useReducedMotion, useSpring, useTransform, type MotionValue } from "framer-motion"; import { useEffect, useRef } from "react"; import { cn } from "@/lib/utils"; /* ------------------------------------------------------------------ */ /* Theme switch: a glass key on a recessed track. A rim of light runs */ /* around the key as it slides, and its pixel sun breaks apart and */ /* lands as a pixel moon. Pair it with useThemeMode() from */ /* theme-picker.tsx: */ /* const { resolved, setMode } = useThemeMode(); */ /* <ThemeSwitch value={resolved} onValueChange={setMode} /> */ /* ------------------------------------------------------------------ */ export type ThemeSwitchValue = "light" | "dark"; type Pixel = { x: number; y: number; a: number }; /** The key is this many pixel cells across. The glyphs use about half of it. */ const CELLS = 32; /** Glyph pixels in cell units, centered on the key. a is pixel opacity. */ function buildGlyphs() { const sun: Pixel[] = []; for (let y = -3.5; y <= 3.5; y++) { for (let x = -3.5; x <= 3.5; x++) { if (Math.abs(x) === 3.5 && Math.abs(y) === 3.5) continue; sun.push({ x, y, a: 1 }); } } for (const d of [7, 8, 9]) { for (const [dx, dy] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) sun.push({ x: dx * d, y: dy * d, a: 1 }); } for (const d of [5, 6]) { for (const [dx, dy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) sun.push({ x: dx * d, y: dy * d, a: 1 }); } const moon: Pixel[] = []; const push = (x: number, y: number, a: number) => moon.push({ x: x - 1, y: y + 1, a }); for (let y = -6; y <= 6; y++) { for (let x = -6; x <= 6; x++) { const inDisc = x * x + y * y <= 6.3 ** 2; const inShadow = (x - 2.6) ** 2 + (y + 2.2) ** 2 <= 5.4 ** 2; if (inDisc && !inShadow) push(x, y, 1); else if (inDisc && (x + y) % 2 === 0) push(x, y, 0.3); // dithered night side of the disc } } // A twinkle star and two specks push(7, -7, 1); for (const [dx, dy] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) push(7 + dx, -7 + dy, 0.5); push(3, -8, 0.7); push(7, -4, 0.5); return { sun, moon }; } /** Seeded random, so the server and every render agree. */ function rng(seed: number) { return () => { seed = (seed * 16807) % 2147483647; return (seed - 1) / 2147483646; }; } type Particle = { light: Pixel; dark: Pixel; /** how far it trails behind, and how far it floats, in cells */ trail: number; float: number; sparkle: number; /** current state, so a second click mid-flight starts from here */ x: number; y: number; a: number; }; function buildParticles(): Particle[] { const { sun, moon } = buildGlyphs(); const rand = rng(11); const n = Math.max(sun.length, moon.length); const shuffle = (list: Pixel[]) => { const out = [...list]; for (let i = out.length - 1; i > 0; i--) { const j = Math.floor(rand() * (i + 1)); [out[i], out[j]] = [out[j], out[i]]; } return out; }; // The smaller glyph gets invisible extras on its own pixels, so every particle has a home in both. const pad = (list: Pixel[]) => { const out = shuffle(list); while (out.length < n) { const p = list[Math.floor(rand() * list.length)]; out.push({ x: p.x, y: p.y, a: 0 }); } return out; }; const light = pad(sun); const dark = pad(moon); return light.map((l, i) => ({ light: l, dark: dark[i], trail: 3 + rand() * 7, float: -5 + rand() * 7, sparkle: rand(), x: l.x, y: l.y, a: l.a, })); } // The switch shows a specific theme, so its colors are fixed on purpose. const INK = { light: [92, 86, 80], dark: [246, 243, 236] } as const; const easeInOut = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2); const clamp01 = (t: number) => Math.min(Math.max(t, 0), 1); function PixelGlyph({ dark, px, reduce }: { dark: boolean; px: number; reduce: boolean }) { const canvasRef = useRef<HTMLCanvasElement>(null); const particles = useRef<Particle[] | null>(null); const first = useRef(true); useEffect(() => { const canvas = canvasRef.current; const ctx = canvas?.getContext("2d"); if (!canvas || !ctx) return; particles.current ??= buildParticles(); const list = particles.current; const dpr = Math.min(window.devicePixelRatio || 1, 3); canvas.width = Math.round(px * dpr); canvas.height = Math.round(px * dpr); const cell = px / CELLS; const dot = cell * 0.78; const snap = (v: number) => Math.round(v * dpr) / dpr; const from = list.map((p) => ({ x: p.x, y: p.y, a: p.a })); // The key slides left into dark and right into light. The leading side lands first, // the trailing side is left behind as loose sparks. const dir = dark ? -1 : 1; const inkFrom = dark ? INK.light : INK.dark; const inkTo = dark ? INK.dark : INK.light; const duration = reduce || first.current ? 0 : 950; first.current = false; const SPREAD = 0.4; let raf = 0; const start = performance.now(); const draw = (now: number) => { const p = duration ? Math.min((now - start) / duration, 1) : 1; ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.clearRect(0, 0, px, px); const glow = dark ? p : 1 - p; ctx.shadowColor = `rgba(205, 210, 255, ${0.9 * glow})`; ctx.shadowBlur = glow > 0.05 ? cell * 1.6 : 0; list.forEach((q, i) => { const to = dark ? q.dark : q.light; const o = from[i]; const lead = clamp01((to.x * dir + 10) / 20); // 1 on the leading side const t = clamp01((p - (1 - lead) * SPREAD) / (1 - SPREAD)); const e = easeInOut(t); const lift = Math.sin(Math.PI * t); q.x = o.x + (to.x - o.x) * e - dir * q.trail * lift; q.y = o.y + (to.y - o.y) * e + q.float * lift; q.a = o.a + (to.a - o.a) * e; const alpha = Math.min(q.a * (1 - lift * 0.4) + lift * 0.35 * q.sparkle, 1); if (alpha <= 0.01) return; // Ink follows the glyph; loose sparks catch the light and flash pale. const mix = easeInOut(t); // On the pale day key a white spark would vanish, so only the night side flashes. const flash = dark ? lift * (q.sparkle > 0.55 ? 0.7 : 0.2) : 0; const ch = inkTo.map((v, k) => { const base = inkFrom[k] + (v - inkFrom[k]) * mix; return Math.round(base + (255 - base) * flash); }); ctx.fillStyle = `rgb(${ch[0]} ${ch[1]} ${ch[2]})`; ctx.globalAlpha = alpha; const s = dot * (1 - lift * 0.45); const cx = px / 2 + q.x * cell; const cy = px / 2 + q.y * cell; ctx.fillRect(snap(cx - s / 2), snap(cy - s / 2), snap(s), snap(s)); }); ctx.globalAlpha = 1; if (p < 1) raf = requestAnimationFrame(draw); }; raf = requestAnimationFrame(draw); return () => cancelAnimationFrame(raf); }, [dark, px, reduce]); return <canvas ref={canvasRef} aria-hidden="true" className="absolute inset-0" style={{ width: px, height: px }} />; } const SIZES = { sm: { w: 96, h: 50, pad: 4, key: 42, trackRadius: 17, keyRadius: 12, ring: 2, stroke: 1.25 }, md: { w: 200, h: 104, pad: 7, key: 90, trackRadius: 36, keyRadius: 24, ring: 3, stroke: 2 }, } as const; /** The rim light sits bottom left at night and top right by day, in conic degrees. */ const RIM_DARK = 205; const RIM_LIGHT = 50; const RIM_SPAN = 150; function Ring({ start, color, inset, stroke, radius, blur, opacity }: { start: MotionValue<number>; color: MotionValue<string>; inset: number; stroke: number; radius: number; blur: number; opacity: number }) { return ( <motion.span aria-hidden="true" className="pointer-events-none absolute" style={{ inset: -inset, padding: stroke, borderRadius: radius + inset, opacity, filter: blur ? `blur(${blur}px)` : undefined, background: useMotionTemplate`conic-gradient(from ${start}deg, transparent 0deg, ${color} 28deg, ${color} ${RIM_SPAN - 28}deg, transparent ${RIM_SPAN}deg)`, // Only the padding shows, so the gradient draws a ring around the key. WebkitMask: "linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)", WebkitMaskComposite: "xor", mask: "linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)", maskComposite: "exclude", }} /> ); } export interface ThemeSwitchProps { value: ThemeSwitchValue; onValueChange: (value: ThemeSwitchValue) => void; /** sm fits a header, md is the showpiece size */ size?: "sm" | "md"; /** Read out with the state, for example "Dark mode" */ "aria-label"?: string; className?: string; } export function ThemeSwitch({ value, onValueChange, size = "md", "aria-label": ariaLabel = "Dark mode", className }: ThemeSwitchProps) { const reduce = useReducedMotion() ?? false; const dark = value === "dark"; const s = SIZES[size]; const travel = s.w - s.pad * 2 - s.key; // One spring drives the slide, the rim light and the materials: 0 is night, 1 is day. const day = useSpring(dark ? 0 : 1, reduce ? { duration: 0 } : { stiffness: 140, damping: 21, mass: 1 }); useEffect(() => { day.set(dark ? 0 : 1); }, [dark, day]); const x = useTransform(day, [0, 1], [s.pad, s.pad + travel]); const night = useTransform(day, [0, 1], [1, 0]); const rimStart = useTransform(day, [0, 1], [RIM_DARK - RIM_SPAN / 2, RIM_LIGHT - RIM_SPAN / 2]); const rimColor = useTransform(day, [0, 0.5, 1], ["#e6e8ff", "#ffffff", "#ffcf45"]); const bloomColor = useTransform(day, [0, 0.5, 1], ["#6a78ff", "#d9ccff", "#ff9410"]); return ( <button type="button" role="switch" aria-checked={dark} aria-label={ariaLabel} onClick={() => onValueChange(dark ? "light" : "dark")} className={cn("relative isolate shrink-0 outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background", className)} style={{ width: s.w, height: s.h, borderRadius: s.trackRadius }} > {/* Track: recessed, pale by day and nearly black at night */} <span aria-hidden="true" className="absolute inset-0" style={{ borderRadius: s.trackRadius, background: "linear-gradient(180deg, #e4e3e0, #efeeeb)", boxShadow: "inset 0 3px 8px rgba(0,0,0,0.14), inset 0 0 0 1px rgba(0,0,0,0.05), 0 1px 0 rgba(255,255,255,0.9), 0 0 0 1px rgba(255,255,255,0.5)", }} /> <motion.span aria-hidden="true" className="absolute inset-0" style={{ opacity: night, borderRadius: s.trackRadius, background: "linear-gradient(180deg, #050507, #0e0e14)", boxShadow: "inset 0 3px 12px rgba(0,0,0,0.95), inset 0 0 0 1px rgba(255,255,255,0.04), 0 1px 0 rgba(255,255,255,0.07), 0 0 0 1px rgba(0,0,0,0.4)", }} /> <motion.span className="absolute left-0 block" initial={false} style={{ x, top: (s.h - s.key) / 2, width: s.key, height: s.key }}> <motion.span className="absolute inset-0 block" whileTap={reduce ? undefined : { scale: 0.96 }}> {/* Key by day: warm white, lit from the top right */} <motion.span className="absolute inset-0 overflow-hidden" style={{ opacity: day, borderRadius: s.keyRadius, background: "radial-gradient(90% 90% at 100% 15%, rgba(255,196,90,0.38), transparent 60%), linear-gradient(160deg, #ffffff 30%, #f5f0e6)", boxShadow: "inset 0 1px 0 #ffffff, inset 0 0 0 1px rgba(0,0,0,0.035), 0 8px 18px rgba(60,40,10,0.16), 0 2px 4px rgba(0,0,0,0.06)", }} /> {/* Key at night: frosted glass, lit from below */} <motion.span className="absolute inset-0 overflow-hidden" style={{ opacity: night, borderRadius: s.keyRadius, background: "radial-gradient(110% 70% at 30% 110%, rgba(120,132,255,0.45), transparent 65%), linear-gradient(165deg, rgba(128,128,146,0.62), rgba(52,52,66,0.7) 75%)", boxShadow: "inset 0 1px 0 rgba(255,255,255,0.32), inset 0 0 0 1px rgba(255,255,255,0.12), inset 0 -1px 0 rgba(200,205,255,0.5), 0 10px 22px rgba(0,0,0,0.7)", backdropFilter: "blur(6px)", }} /> <span className="absolute inset-0 overflow-hidden" style={{ borderRadius: s.keyRadius }}> <PixelGlyph dark={dark} px={s.key} reduce={reduce} /> </span> {/* Rim light, drawn over the key's shadow: a wide bloom, a soft halo and the thin bright line */} <Ring start={rimStart} color={bloomColor} inset={s.ring + s.stroke * 3} stroke={s.stroke * 5} radius={s.keyRadius} blur={s.key / 8} opacity={0.6} /> <Ring start={rimStart} color={rimColor} inset={s.ring + s.stroke * 0.5} stroke={s.stroke * 1.8} radius={s.keyRadius} blur={s.key / 45} opacity={0.5} /> <Ring start={rimStart} color={rimColor} inset={s.ring} stroke={s.stroke} radius={s.keyRadius} blur={0} opacity={1} /> </motion.span> </motion.span> </button> ); }





