{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "theme-switcher",
  "type": "registry:ui",
  "title": "Theme switcher",
  "description": "A visual Light, Dark, and System theme selector with textured previews, an animated selection outline, and keyboard navigation.",
  "author": "Aditya Ojha <https://akoder.xyz>",
  "categories": [
    "theming",
    "dark-mode",
    "animation"
  ],
  "dependencies": [
    "motion",
    "next-themes"
  ],
  "registryDependencies": [],
  "meta": {
    "docs": "https://akoder.xyz/components/theme-switcher",
    "source": "https://github.com/AdityaKodez/adityaojha/blob/main/components/ui/theme-switcher.tsx"
  },
  "files": [
    {
      "path": "components/ui/theme-switcher.tsx",
      "content": "\"use client\";\n\nimport { motion, useReducedMotion } from \"motion/react\";\nimport { useTheme } from \"next-themes\";\nimport { useId, useRef, useState, useSyncExternalStore } from \"react\";\nimport type { KeyboardEvent } from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\nconst OPTIONS = [\n  { value: \"light\", label: \"Light\" },\n  { value: \"dark\", label: \"Dark\" },\n  { value: \"system\", label: \"System\" },\n] as const;\n\ntype ThemeValue = (typeof OPTIONS)[number][\"value\"];\n\nexport interface ThemeSwitcherProps {\n  /** Extra classes applied to the three-column selector. */\n  className?: string;\n  /** Disable all theme changes. Forced themes also disable the selector. */\n  disabled?: boolean;\n}\n\nconst subscribe = () => () => {};\nconst getSnapshot = () => true;\nconst getServerSnapshot = () => false;\n\n/** A visual Light / Dark / System selector backed by next-themes. */\nexport function ThemeSwitcher({ className, disabled = false }: ThemeSwitcherProps) {\n  const { theme, setTheme, forcedTheme } = useTheme();\n  const mounted = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);\n  const reduceMotion = useReducedMotion();\n  const id = useId();\n  const buttons = useRef<Array<HTMLButtonElement | null>>([]);\n  const [focused, setFocused] = useState<ThemeValue | null>(null);\n  const activeTheme = mounted ? forcedTheme || theme : undefined;\n  const activeIndex = OPTIONS.findIndex((option) => option.value === activeTheme);\n  const unavailable = disabled || !mounted || Boolean(forcedTheme);\n  const transition = reduceMotion\n    ? { duration: 0 }\n    : { type: \"spring\" as const, duration: 0.26, bounce: 0.12 };\n\n  function handleKeyDown(event: KeyboardEvent<HTMLButtonElement>, index: number) {\n    let nextIndex: number;\n\n    switch (event.key) {\n      case \"ArrowRight\":\n      case \"ArrowDown\":\n        nextIndex = (index + 1) % OPTIONS.length;\n        break;\n      case \"ArrowLeft\":\n      case \"ArrowUp\":\n        nextIndex = (index - 1 + OPTIONS.length) % OPTIONS.length;\n        break;\n      case \"Home\":\n        nextIndex = 0;\n        break;\n      case \"End\":\n        nextIndex = OPTIONS.length - 1;\n        break;\n      default:\n        return;\n    }\n\n    event.preventDefault();\n    setTheme(OPTIONS[nextIndex].value);\n    buttons.current[nextIndex]?.focus();\n  }\n\n  return (\n    <div\n      role=\"radiogroup\"\n      aria-label=\"Theme\"\n      aria-disabled={unavailable}\n      className={cn(\"grid w-full max-w-[26rem] grid-cols-3 gap-3\", className)}\n    >\n      {OPTIONS.map((option, index) => {\n        const selected = activeTheme === option.value;\n\n        return (\n          <motion.button\n            key={option.value}\n            ref={(button) => { buttons.current[index] = button; }}\n            type=\"button\"\n            role=\"radio\"\n            aria-checked={selected}\n            disabled={unavailable}\n            tabIndex={index === (activeIndex < 0 ? 0 : activeIndex) ? 0 : -1}\n            onClick={() => setTheme(option.value)}\n            onKeyDown={(event) => handleKeyDown(event, index)}\n            onFocus={() => setFocused(option.value)}\n            onBlur={() => setFocused(null)}\n            animate={{ y: focused === option.value && !reduceMotion ? -2 : 0 }}\n            whileHover={unavailable || reduceMotion ? undefined : { y: -2 }}\n            whileTap={unavailable || reduceMotion ? undefined : { scale: 0.97 }}\n            transition={transition}\n            className=\"group min-w-0 cursor-pointer rounded-xl outline-none disabled:cursor-not-allowed disabled:opacity-50\"\n          >\n            <span\n              className={cn(\n                \"relative block rounded-xl group-focus-visible:outline-2 group-focus-visible:outline-offset-4 group-focus-visible:outline-ring\",\n                !selected && \"ring-1 ring-inset ring-border\",\n              )}\n            >\n              <ThemePreview theme={option.value} id={`${id}-${option.value}`} />\n              {selected && (\n                <motion.span\n                  layoutId={`${id}-selection`}\n                  initial={false}\n                  transition={transition}\n                  className=\"pointer-events-none absolute inset-0 rounded-xl ring-1 ring-inset ring-foreground/70\"\n                />\n              )}\n            </span>\n            <span\n              className={cn(\n                \"mt-2.5 block text-sm leading-none\",\n                selected ? \"font-medium text-foreground\" : \"text-muted-foreground\",\n              )}\n            >\n              {option.label}\n            </span>\n          </motion.button>\n        );\n      })}\n    </div>\n  );\n}\n\n/** Static SVG artwork keeps every preview independent of the active site theme. */\nfunction ThemePreview({ theme, id }: { theme: ThemeValue; id: string }) {\n  const light = theme === \"light\";\n  const system = theme === \"system\";\n  const page = light ? \"#fafafa\" : \"#080808\";\n  const ink = light ? \"#111111\" : \"#f5f5f5\";\n  const line = light ? \"#ececec\" : \"#262626\";\n\n  return (\n    <svg aria-hidden=\"true\" viewBox=\"0 0 124 80\" fill=\"none\" className=\"block w-full overflow-hidden rounded-xl\">\n      <defs>\n        <linearGradient id={`${id}-backdrop`} x1=\"62\" y1=\"0\" x2=\"62\" y2=\"80\" gradientUnits=\"userSpaceOnUse\">\n          <stop stopColor={light ? \"#252525\" : \"#080808\"} />\n          <stop offset=\"1\" stopColor={light ? \"#818181\" : \"#191919\"} />\n        </linearGradient>\n        <linearGradient id={`${id}-beam`} x1=\"38\" y1=\"0\" x2=\"83\" y2=\"80\" gradientUnits=\"userSpaceOnUse\">\n          <stop stopColor=\"white\" stopOpacity=\"0\" />\n          <stop offset=\"0.42\" stopColor=\"white\" stopOpacity=\"0.02\" />\n          <stop offset=\"0.5\" stopColor=\"white\" stopOpacity=\"0.24\" />\n          <stop offset=\"0.58\" stopColor=\"white\" stopOpacity=\"0.02\" />\n          <stop offset=\"1\" stopColor=\"white\" stopOpacity=\"0\" />\n        </linearGradient>\n        <linearGradient id={`${id}-page`}>\n          <stop offset=\"50%\" stopColor={page} />\n          <stop offset=\"50%\" stopColor={system ? \"#fafafa\" : page} />\n        </linearGradient>\n        <linearGradient id={`${id}-lines`} x1=\"20\" x2=\"104\" gradientUnits=\"userSpaceOnUse\">\n          <stop offset=\"50%\" stopColor={line} />\n          <stop offset=\"50%\" stopColor={system ? \"#ececec\" : line} />\n        </linearGradient>\n        <filter id={`${id}-grain`} x=\"0\" y=\"0\" width=\"100%\" height=\"100%\">\n          <feTurbulence type=\"fractalNoise\" baseFrequency=\"0.85\" numOctaves=\"3\" stitchTiles=\"stitch\" />\n          <feColorMatrix type=\"saturate\" values=\"0\" />\n        </filter>\n      </defs>\n      <path fill={`url(#${id}-backdrop)`} d=\"M0 0h124v80H0z\" />\n      <path fill={`url(#${id}-beam)`} d=\"M0 0h124v80H0z\" />\n      <path fill=\"white\" opacity={light ? 0.13 : 0.07} filter={`url(#${id}-grain)`} d=\"M0 0h124v80H0z\" />\n      <rect x=\"20\" y=\"16\" width=\"84\" height=\"55\" rx=\"4\" fill=\"black\" opacity=\"0.15\" />\n      <rect x=\"20\" y=\"13\" width=\"84\" height=\"55\" rx=\"4\" fill={`url(#${id}-page)`} />\n      <path d=\"M35 13v55\" stroke={line} strokeOpacity=\"0.6\" />\n      <path d=\"M25 22v-4h4m-4 4 4-4\" stroke={ink} strokeWidth=\"0.7\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n      <g fill={`url(#${id}-lines)`}>\n        <rect x=\"67\" y=\"19\" width=\"24\" height=\"5\" rx=\"2.5\" />\n        <rect x=\"47\" y=\"29\" width=\"27\" height=\"3\" rx=\"1\" />\n        <rect x=\"47\" y=\"34\" width=\"36\" height=\"3\" rx=\"1\" />\n        <rect x=\"47\" y=\"39\" width=\"20\" height=\"3\" rx=\"1\" />\n        <rect x=\"47\" y=\"44\" width=\"28\" height=\"3\" rx=\"1\" />\n        <rect x=\"47\" y=\"59\" width=\"43\" height=\"4\" rx=\"2\" />\n      </g>\n      <circle cx=\"88\" cy=\"61\" r=\"1.8\" fill={system ? \"#111111\" : ink} />\n    </svg>\n  );\n}\n",
      "type": "registry:ui"
    }
  ],
  "docs": "## Installation\n\n```bash\nnpx shadcn add @akoder/theme-switcher\n```\n\n## Usage\n\n```tsx\nimport { ThemeSwitcher } from \"@/components/ui/theme-switcher\";\n\nexport function AppearanceSettings() {\n  return <ThemeSwitcher />;\n}\n```\n\nWrap your app in a next-themes ThemeProvider with attribute=\"class\" and enableSystem. Reuse an existing provider when present. Add suppressHydrationWarning to your root html element."
}