{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "theme-toggle",
  "title": "Theme Toggle",
  "description": "A light / dark switch that morphs a sun into a moon, or a Light · System · Dark control with a sliding pill; it remembers the choice, follows the OS in System mode, writes the theme to <html>, and can grow the new theme out of the button in a circle.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/theme-toggle/theme-toggle.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type ThemeChoice = \"light\" | \"dark\" | \"system\";\n\nexport interface ThemeToggleProps {\n  /** icon: one button that flips light / dark. segmented: Light · System · Dark. */\n  variant?: \"icon\" | \"segmented\";\n  value?: ThemeChoice;\n  defaultValue?: ThemeChoice;\n  onValueChange?: (value: ThemeChoice, resolved: \"light\" | \"dark\") => void;\n  /** Write the result to <html> (class or data-theme) and remember it. Turn off to only report changes. */\n  apply?: boolean;\n  attribute?: \"class\" | \"data-theme\";\n  storageKey?: string;\n  /** Grow the new theme out of the button in a circle (View Transitions, where supported). */\n  reveal?: boolean;\n  /** Colours of the control itself. */\n  appearance?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.55)\", bg: \"rgba(255,255,255,0.05)\", border: \"rgba(255,255,255,0.12)\", pill: \"#F5F4F1\", pillText: \"#0A0A0A\", focus: \"rgba(245,244,241,0.85)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.55)\", bg: \"rgba(10,10,10,0.04)\", border: \"rgba(10,10,10,0.12)\", pill: \"#0A0A0A\", pillText: \"#FFFFFF\", focus: \"rgba(10,10,10,0.85)\" },\n};\n\n// The OS preference, read through an external store so server and client HTML match.\nconst subscribeScheme = (cb: () => void) => {\n  const m = window.matchMedia(\"(prefers-color-scheme: dark)\");\n  m.addEventListener(\"change\", cb);\n  return () => m.removeEventListener(\"change\", cb);\n};\nconst useSystemDark = () => React.useSyncExternalStore(subscribeScheme, () => window.matchMedia(\"(prefers-color-scheme: dark)\").matches, () => true);\n\nfunction SunMoon({ dark, reduce, size = 18 }: { dark: boolean; reduce: boolean; size?: number }) {\n  const id = React.useId();\n  const spring = reduce ? { duration: 0 } : { type: \"spring\" as const, stiffness: 260, damping: 20 };\n  return (\n    <motion.svg width={size} height={size} viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" aria-hidden=\"true\" initial={false} animate={{ rotate: dark ? 40 : 90 }} transition={spring}>\n      <mask id={id}>\n        <rect width=\"24\" height=\"24\" fill=\"#fff\" />\n        <motion.circle r=\"6\" fill=\"#000\" initial={false} animate={{ cx: dark ? 16 : 30, cy: dark ? 7 : -6 }} transition={spring} />\n      </mask>\n      <motion.circle cx=\"12\" cy=\"12\" fill=\"currentColor\" stroke=\"none\" mask={`url(#${id})`} initial={false} animate={{ r: dark ? 8 : 4.5 }} transition={spring} />\n      <motion.g initial={false} animate={{ opacity: dark ? 0 : 1, scale: dark ? 0.4 : 1 }} style={{ originX: \"50%\", originY: \"50%\" }} transition={spring}>\n        {[0, 45, 90, 135, 180, 225, 270, 315].map((a) => {\n          const r = (a * Math.PI) / 180;\n          return <line key={a} x1={12 + Math.cos(r) * 7.2} y1={12 + Math.sin(r) * 7.2} x2={12 + Math.cos(r) * 9.4} y2={12 + Math.sin(r) * 9.4} />;\n        })}\n      </motion.g>\n    </motion.svg>\n  );\n}\n\nfunction Monitor() {\n  return (\n    <svg width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      <rect x=\"3\" y=\"4\" width=\"18\" height=\"12\" rx=\"2\" />\n      <path d=\"M8 20h8M12 16v4\" />\n    </svg>\n  );\n}\n\nexport function ThemeToggle({ variant = \"icon\", value, defaultValue = \"system\", onValueChange, apply = true, attribute = \"class\", storageKey = \"theme\", reveal = true, appearance = \"dark\", className }: ThemeToggleProps) {\n  const p = PALETTES[appearance];\n  const uid = React.useId();\n  const reduce = Boolean(useReducedMotion());\n  const systemDark = useSystemDark();\n  const [internal, setInternal] = React.useState<ThemeChoice>(defaultValue);\n  const choice = value ?? internal;\n  const resolved: \"light\" | \"dark\" = choice === \"system\" ? (systemDark ? \"dark\" : \"light\") : choice;\n  const btnRef = React.useRef<HTMLButtonElement>(null);\n\n  // Pick up a stored choice once on the client.\n  React.useEffect(() => {\n    if (!apply || value !== undefined) return;\n    try {\n      const saved = localStorage.getItem(storageKey) as ThemeChoice | null;\n      if (saved === \"light\" || saved === \"dark\" || saved === \"system\") {\n        const id = window.setTimeout(() => setInternal(saved), 0);\n        return () => window.clearTimeout(id);\n      }\n    } catch {\n      // Storage can be unavailable; fall back to defaultValue.\n    }\n  }, [apply, storageKey, value]);\n\n  // Keep <html> in sync with the resolved theme.\n  React.useEffect(() => {\n    if (!apply) return;\n    const root = document.documentElement;\n    if (attribute === \"class\") {\n      root.classList.toggle(\"dark\", resolved === \"dark\");\n      root.classList.toggle(\"light\", resolved === \"light\");\n    } else root.setAttribute(\"data-theme\", resolved);\n    root.style.colorScheme = resolved;\n  }, [apply, attribute, resolved]);\n\n  const set = (next: ThemeChoice, origin?: HTMLElement | null) => {\n    const nextResolved: \"light\" | \"dark\" = next === \"system\" ? (systemDark ? \"dark\" : \"light\") : next;\n    const commit = () => {\n      if (value === undefined) setInternal(next);\n      onValueChange?.(next, nextResolved);\n      if (apply) {\n        try {\n          localStorage.setItem(storageKey, next);\n        } catch {\n          // Not persisted this time.\n        }\n      }\n    };\n    const doc = document as Document & { startViewTransition?: (cb: () => void) => { ready: Promise<void> } };\n    if (!apply || !reveal || reduce || !doc.startViewTransition || nextResolved === resolved || !origin) return commit();\n    const r = origin.getBoundingClientRect();\n    const x = r.left + r.width / 2;\n    const y = r.top + r.height / 2;\n    const radius = Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y));\n    const t = doc.startViewTransition(commit);\n    t.ready.then(() => {\n      document.documentElement.animate({ clipPath: [`circle(0px at ${x}px ${y}px)`, `circle(${radius}px at ${x}px ${y}px)`] }, { duration: 520, easing: \"cubic-bezier(0.65, 0, 0.35, 1)\", pseudoElement: \"::view-transition-new(root)\" });\n    });\n  };\n\n  if (variant === \"segmented\") {\n    const opts: { v: ThemeChoice; label: string; icon: React.ReactNode }[] = [\n      { v: \"light\", label: \"Light\", icon: <SunMoon dark={false} reduce size={16} /> },\n      { v: \"system\", label: \"System\", icon: <Monitor /> },\n      { v: \"dark\", label: \"Dark\", icon: <SunMoon dark reduce size={16} /> },\n    ];\n    return (\n      <div role=\"radiogroup\" aria-label=\"Theme\" className={cn(\"inline-flex items-center rounded-full\", className)} style={{ padding: 3, gap: 2, background: p.bg, border: `1px solid ${p.border}`, fontFamily: \"Inter, sans-serif\" }}>\n        {opts.map((o) => {\n          const on = choice === o.v;\n          return (\n            <button\n              key={o.v}\n              type=\"button\"\n              role=\"radio\"\n              aria-checked={on}\n              onClick={(e) => set(o.v, e.currentTarget)}\n              className=\"relative flex cursor-pointer items-center rounded-full border-none bg-transparent text-[13px] font-medium outline-none focus-visible:ring-2\"\n              style={{ gap: 6, height: 30, padding: \"0 12px\", color: on ? p.pillText : p.muted, transition: \"color 0.2s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n            >\n              {on && <motion.span layoutId={`${uid}-pill`} className=\"absolute inset-0 rounded-full\" style={{ background: p.pill }} transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 500, damping: 36 }} />}\n              <span className=\"relative flex\">{o.icon}</span>\n              <span className=\"relative\">{o.label}</span>\n            </button>\n          );\n        })}\n      </div>\n    );\n  }\n\n  return (\n    <motion.button\n      ref={btnRef}\n      type=\"button\"\n      aria-label={resolved === \"dark\" ? \"Switch to light theme\" : \"Switch to dark theme\"}\n      aria-pressed={resolved === \"dark\"}\n      onClick={() => set(resolved === \"dark\" ? \"light\" : \"dark\", btnRef.current)}\n      className={cn(\"flex cursor-pointer items-center justify-center rounded-full outline-none focus-visible:ring-2\", className)}\n      style={{ width: 40, height: 40, background: p.bg, border: `1px solid ${p.border}`, color: p.text, [\"--tw-ring-color\" as string]: p.focus }}\n      whileHover={reduce ? undefined : { scale: 1.06 }}\n      whileTap={{ scale: 0.92 }}\n    >\n      <SunMoon dark={resolved === \"dark\"} reduce={reduce} />\n    </motion.button>\n  );\n}\n\nexport default ThemeToggle;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}