{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "theme-toggle",
  "title": "Theme toggle",
  "description": "Light/dark toggle that reveals the new theme as a circle from the click point with the View Transitions API while the sun morphs into a moon.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json"
  ],
  "files": [
    {
      "path": "registry/components/buttons/theme-toggle.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { motion, useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { GooduiTokens, spring } from \"@/lib/goodui-motion\"\n\n// While our transition runs, turn off the default cross-fade so the new page is revealed by the clip-path circle.\nconst CSS = `\nhtml.pc-theme-toggle-vt::view-transition-old(root),\nhtml.pc-theme-toggle-vt::view-transition-new(root) { animation: none; mix-blend-mode: normal }\nhtml.pc-theme-toggle-vt::view-transition-old(root) { z-index: 1 }\nhtml.pc-theme-toggle-vt::view-transition-new(root) { z-index: 2147483646 }\n`\n\ntype ViewTransitionLike = { ready: Promise<void>; finished: Promise<void> }\ntype DocWithVT = Document & { startViewTransition?: (cb: () => void) => ViewTransitionLike }\n\nexport type Theme = \"light\" | \"dark\"\n\nexport type ThemeToggleLabels = { toLight: string; toDark: string; darkTheme: string }\nconst DEFAULT_LABELS: ThemeToggleLabels = { toLight: \"Switch to light theme\", toDark: \"Switch to dark theme\", darkTheme: \"Dark theme\" }\n\nexport type ThemeToggleProps = Omit<React.ComponentProps<\"button\">, \"onAnimationStart\" | \"onDrag\" | \"onDragStart\" | \"onDragEnd\"> & {\n  /** Called after the `dark` class on <html> changes. */\n  onThemeChange?: (theme: Theme) => void\n  /** Milliseconds for the circular reveal. */\n  duration?: number\n  /** \"circle\" uses the View Transitions API when available; \"none\" switches instantly. */\n  transition?: \"circle\" | \"none\"\n  /** Save the choice in localStorage under this key and apply the saved choice on mount (off by default). Add a head script to avoid a flash before hydration. */\n  storageKey?: string\n  /** \"icon\" is a round icon button; \"pill\" a two-icon switch. */\n  variant?: \"icon\" | \"pill\"\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<ThemeToggleLabels>\n}\n\n/** Sun ↔ moon: the sun's core grows into a moon as a mask circle slides in, and the rays spin away. */\nfunction SunMoon({ dark, reduce }: { dark: boolean; reduce: boolean }) {\n  const raw = React.useId()\n  const id = `pc-tt-${raw.replace(/[^a-zA-Z0-9_-]/g, \"\")}`\n  const t = reduce ? { duration: 0 } : spring.morph\n  return (\n    <svg viewBox=\"0 0 24 24\" fill=\"none\" aria-hidden=\"true\" className=\"size-[1.15rem] overflow-visible\">\n      <mask id={id}>\n        <rect width=\"24\" height=\"24\" fill=\"white\" />\n        <motion.circle r=\"9\" fill=\"black\" initial={false} animate={dark ? { cx: 18.5, cy: 5.5 } : { cx: 30, cy: -6 }} transition={t} />\n      </mask>\n      <motion.circle cx=\"12\" cy=\"12\" fill=\"currentColor\" mask={`url(#${id})`} initial={false} animate={{ r: dark ? 8.5 : 4.6 }} transition={t} />\n      <motion.g\n        stroke=\"currentColor\"\n        strokeWidth=\"2\"\n        strokeLinecap=\"round\"\n        initial={false}\n        animate={dark ? { opacity: 0, scale: 0.4, rotate: -90 } : { opacity: 1, scale: 1, rotate: 0 }}\n        transition={t}\n        style={{ originX: \"12px\", originY: \"12px\" }}\n      >\n        {[0, 45, 90, 135, 180, 225, 270, 315].map((a) => {\n          const r = (a * Math.PI) / 180\n          return (\n            <line\n              key={a}\n              x1={12 + Math.cos(r) * 7.6}\n              y1={12 + Math.sin(r) * 7.6}\n              x2={12 + Math.cos(r) * 9.8}\n              y2={12 + Math.sin(r) * 9.8}\n            />\n          )\n        })}\n      </motion.g>\n    </svg>\n  )\n}\n\n/**\n * Light/dark toggle that flips the `dark` class on <html>. With the View Transitions API the new theme spreads as a\n * circle from the click point; otherwise (or with reduced motion) it switches instantly.\n */\nexport function ThemeToggle({\n  onThemeChange,\n  duration = 550,\n  transition = \"circle\",\n  storageKey,\n  variant = \"icon\",\n  className,\n  onClick,\n  labels,\n  ref,\n  ...props\n}: ThemeToggleProps) {\n  const l = useLabels(\"theme-toggle\", DEFAULT_LABELS, labels)\n  const reduce = useReducedMotion() ?? false\n  const [dark, setDark] = React.useState(false)\n  const [ready, setReady] = React.useState(false)\n  // Snap (no animation) to the theme found on mount, animate only real changes afterwards.\n  const [instant, setInstant] = React.useState(true)\n  const still = reduce || instant\n  // The pill thumb slides toward the inline end, so it moves left under dir=\"rtl\".\n  const [rtl, setRtl] = React.useState(false)\n  const btnRef = React.useRef<HTMLButtonElement | null>(null)\n  const setRefs = (el: HTMLButtonElement | null) => {\n    btnRef.current = el\n    if (typeof ref === \"function\") ref(el)\n    else if (ref) (ref as React.RefObject<HTMLButtonElement | null>).current = el\n  }\n\n  // Mirror the <html> class, so every toggle on the page (and outside changes) stays in sync.\n  React.useEffect(() => {\n    const root = document.documentElement\n    if (btnRef.current) setRtl(getComputedStyle(btnRef.current).direction === \"rtl\")\n    // Apply a saved choice (a head script should do this before paint; this is the fallback).\n    if (storageKey) {\n      try {\n        const saved = localStorage.getItem(storageKey)\n        if ((saved === \"dark\" || saved === \"light\") && root.classList.contains(\"dark\") !== (saved === \"dark\")) {\n          root.classList.toggle(\"dark\", saved === \"dark\")\n          root.style.colorScheme = saved\n        }\n      } catch {\n        /* private mode */\n      }\n    }\n    const sync = () => setDark(root.classList.contains(\"dark\"))\n    sync()\n    setReady(true)\n    const raf = requestAnimationFrame(() => requestAnimationFrame(() => setInstant(false)))\n    const mo = new MutationObserver(sync)\n    mo.observe(root, { attributes: true, attributeFilter: [\"class\"] })\n    return () => {\n      cancelAnimationFrame(raf)\n      mo.disconnect()\n    }\n  }, [storageKey])\n\n  const toggle = (e: React.MouseEvent<HTMLButtonElement>) => {\n    const root = document.documentElement\n    const next = !root.classList.contains(\"dark\")\n    const apply = () => {\n      root.classList.toggle(\"dark\", next)\n      root.style.colorScheme = next ? \"dark\" : \"light\"\n      if (storageKey) {\n        try {\n          localStorage.setItem(storageKey, next ? \"dark\" : \"light\")\n        } catch {\n          /* private mode */\n        }\n      }\n    }\n    const doc = document as DocWithVT\n    if (transition === \"none\" || reduce || typeof doc.startViewTransition !== \"function\") {\n      apply()\n      onThemeChange?.(next ? \"dark\" : \"light\")\n      return\n    }\n    // Keyboard clicks have no coordinates (detail 0): start from the button center.\n    const r = e.currentTarget.getBoundingClientRect()\n    const x = e.detail === 0 ? r.left + r.width / 2 : e.clientX\n    const y = e.detail === 0 ? r.top + r.height / 2 : e.clientY\n    const radius = Math.hypot(Math.max(x, window.innerWidth - x), Math.max(y, window.innerHeight - y))\n    root.classList.add(\"pc-theme-toggle-vt\")\n    const vt = doc.startViewTransition(apply)\n    vt.ready\n      .then(() => {\n        root.animate(\n          { clipPath: [`circle(0px at ${x}px ${y}px)`, `circle(${radius}px at ${x}px ${y}px)`] },\n          { duration, easing: \"cubic-bezier(.77,0,.175,1)\", pseudoElement: \"::view-transition-new(root)\" },\n        )\n      })\n      .catch(() => {})\n    vt.finished.finally(() => {\n      root.classList.remove(\"pc-theme-toggle-vt\")\n      onThemeChange?.(next ? \"dark\" : \"light\")\n    })\n  }\n\n  const label = dark ? l.toLight : l.toDark\n  const common = {\n    type: \"button\" as const,\n    \"aria-label\": label,\n    onClick: (e: React.MouseEvent<HTMLButtonElement>) => {\n      onClick?.(e)\n      if (!e.defaultPrevented) toggle(e)\n    },\n  }\n\n  if (variant === \"pill\") {\n    return (\n      <>\n        <GooduiTokens />\n        <style href=\"pc-theme-toggle\" precedence=\"default\">\n          {CSS}\n        </style>\n        <button\n          {...common}\n          ref={setRefs}\n          role=\"switch\"\n          aria-checked={dark}\n          aria-label={l.darkTheme}\n          className={cn(\n            \"pc-press relative inline-flex h-9 w-[4.25rem] shrink-0 items-center rounded-full border bg-muted p-1 shadow-inner outline-none focus-visible:ring-3 focus-visible:ring-ring/50\",\n            !ready && \"opacity-0\",\n            className,\n          )}\n          {...props}\n        >\n          <motion.span\n            aria-hidden=\"true\"\n            className=\"absolute start-1 top-1 size-7 rounded-full bg-background shadow-[var(--pc-e1)] ring-1 ring-black/5 dark:ring-white/10\"\n            initial={false}\n            animate={{ x: dark ? (rtl ? -32 : 32) : 0 }}\n            transition={still ? { duration: 0 } : spring.ui}\n          />\n          <span className={cn(\"relative grid size-7 place-items-center transition-colors\", dark ? \"text-muted-foreground\" : \"text-amber-500\")}>\n            <SunMoon dark={false} reduce={reduce} />\n          </span>\n          <span className={cn(\"relative ms-1 grid size-7 place-items-center transition-colors\", dark ? \"text-sky-300\" : \"text-muted-foreground\")}>\n            <SunMoon dark reduce={reduce} />\n          </span>\n        </button>\n      </>\n    )\n  }\n\n  return (\n    <>\n      <style href=\"pc-theme-toggle\" precedence=\"default\">\n        {CSS}\n      </style>\n      <motion.button\n        {...common}\n        ref={setRefs}\n        whileTap={reduce ? undefined : { scale: 0.9 }}\n        className={cn(\n          \"relative inline-grid size-9 shrink-0 place-items-center rounded-full text-foreground transition-[background-color,opacity] outline-none hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50\",\n          !ready && \"opacity-0\",\n          className,\n        )}\n        {...props}\n      >\n        <motion.span\n          className=\"grid place-items-center\"\n          initial={false}\n          animate={{ rotate: dark && !reduce ? 360 : 0 }}\n          transition={still ? { duration: 0 } : spring.morph}\n        >\n          <SunMoon dark={dark} reduce={still} />\n        </motion.span>\n      </motion.button>\n    </>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/theme-toggle.tsx"
    }
  ],
  "categories": [
    "components",
    "buttons"
  ],
  "type": "registry:component"
}