{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "micro-horizon-toggle",
  "type": "registry:component",
  "title": "Horizon Toggle",
  "description": "A dark mode switch drawn as a tiny landscape: the sun sets behind the hills through a dusk sky while the moon and stars rise. Drag across it to scrub the time of day; release snaps to day or night.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "micro",
    "theme",
    "dark mode",
    "toggle",
    "sunset",
    "drag",
    "micro"
  ],
  "docs": "https://hyperblur.dev/effects/micro-horizon-toggle",
  "files": [
    {
      "path": "registry/hyperblur/micro-horizon-toggle.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/micro-horizon-toggle.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface MicroHorizonToggleProps\n  extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, \"onChange\" | \"role\" | \"children\"> {\n  checked?: boolean;\n  defaultChecked?: boolean;\n  size?: number;\n  label?: string;\n  onChange?: (dark: boolean) => void;\n}\n\nconst css = `\n.hb-wf8 {\n  --hb-wf8-s: 48px;\n  position: relative;\n  display: inline-block;\n  width: calc(var(--hb-wf8-s) * 2.25);\n  height: var(--hb-wf8-s);\n  margin: 0;\n  padding: 0;\n  box-sizing: border-box;\n  border: 0;\n  border-radius: 999px;\n  background: #0a0a0a;\n  cursor: grab;\n  overflow: hidden;\n  touch-action: pan-y;\n  -webkit-tap-highlight-color: transparent;\n  box-shadow: 0 0 0 1px rgba(255,255,255,0.08), 0 10px 24px -10px rgba(0,0,0,0.9);\n  transition: transform 0.25s cubic-bezier(0.3, 1.6, 0.5, 1), box-shadow 0.2s ease;\n}\n.hb-wf8 *, .hb-wf8 *::before, .hb-wf8 *::after { box-sizing: border-box; }\n.hb-wf8:active { cursor: grabbing; transform: scale(0.97); }\n.hb-wf8:focus-visible { outline: none; box-shadow: 0 0 0 2px #0a0a0a, 0 0 0 4px #7fb3ff; }\n.hb-wf8:disabled { opacity: 0.45; cursor: not-allowed; }\n.hb-wf8-svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }\n.hb-wf8::after {\n  content: \"\";\n  position: absolute;\n  inset: 0;\n  border-radius: inherit;\n  pointer-events: none;\n  box-shadow: inset 0 2px 5px rgba(0,0,0,0.45), inset 0 -1px 0 rgba(255,255,255,0.08);\n}\n.hb-wf8-tw { animation: hb-wf8-tw 2.8s ease-in-out infinite; }\n@keyframes hb-wf8-tw { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }\n.hb-wf8-wv { animation: hb-wf8-wv 3.6s ease-in-out infinite; }\n@keyframes hb-wf8-wv { 0%, 100% { transform: translateX(0); } 50% { transform: translateX(3px); } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-wf8 { transition: none; }\n  .hb-wf8-tw, .hb-wf8-wv { animation: none; }\n}\n`;\n\ntype RGB = [number, number, number];\n\nconst hex = (h: string): RGB => [parseInt(h.slice(1, 3), 16), parseInt(h.slice(3, 5), 16), parseInt(h.slice(5, 7), 16)];\nconst mix = (a: RGB, b: RGB, t: number) =>\n  `rgb(${a.map((v, i) => Math.round(v + (b[i] - v) * t)).join(\",\")})`;\nconst ramp = (stops: string[], t: number) => {\n  const c = stops.map(hex);\n  const x = Math.max(0, Math.min(1, t)) * (c.length - 1);\n  const i = Math.min(c.length - 2, Math.floor(x));\n  return mix(c[i], c[i + 1], x - i);\n};\n\nconst stars = Array.from({ length: 14 }, (_, i) => {\n  const h = (Math.imul(i + 7, 2654435761) >>> 0) / 4294967296;\n  const k = (Math.imul(i + 31, 2246822519) >>> 0) / 4294967296;\n  return { x: (6 + h * 96).toFixed(1), y: (3 + k * 22).toFixed(1), r: (0.35 + ((i * 7) % 5) * 0.12).toFixed(2), d: `${((i * 0.37) % 2.8).toFixed(2)}s` };\n});\n\nconst ease = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2);\n\nexport function MicroHorizonToggle({\n  checked,\n  defaultChecked = false,\n  size = 48,\n  label = \"Dark mode\",\n  disabled,\n  onChange,\n  className = \"\",\n  style,\n  onClick,\n  ...rest\n}: MicroHorizonToggleProps) {\n  const [inner, setInner] = React.useState(defaultChecked);\n  const dark = checked ?? inner;\n  const [p, setP] = React.useState(dark ? 1 : 0);\n  const [scrub, setScrub] = React.useState(false);\n  const anim = React.useRef({ raf: 0, from: 0, to: 0, t0: 0 });\n  const drag = React.useRef({ on: false, x: 0, base: 0, moved: false, w: 100 });\n  const pRef = React.useRef(p);\n  const uid = React.useId().replace(/:/g, \"\");\n\n  const animateTo = React.useCallback((to: number) => {\n    const a = anim.current;\n    cancelAnimationFrame(a.raf);\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) {\n      pRef.current = to;\n      setP(to);\n      return;\n    }\n    a.from = pRef.current;\n    a.to = to;\n    a.t0 = performance.now();\n    const dur = 720 * Math.max(0.35, Math.abs(to - a.from));\n    const step = (t: number) => {\n      const k = Math.min(1, (t - a.t0) / dur);\n      const v = a.from + (a.to - a.from) * ease(k);\n      pRef.current = v;\n      setP(v);\n      if (k < 1) a.raf = requestAnimationFrame(step);\n    };\n    a.raf = requestAnimationFrame(step);\n  }, []);\n\n  React.useEffect(() => {\n    if (drag.current.on) return;\n    const target = dark ? 1 : 0;\n    if (Math.abs(pRef.current - target) < 0.001) return;\n    const id = requestAnimationFrame(() => animateTo(target));\n    return () => cancelAnimationFrame(id);\n  }, [dark, animateTo]);\n\n  React.useEffect(() => () => cancelAnimationFrame(anim.current.raf), []);\n\n  const set = (next: boolean) => {\n    if (disabled) return;\n    if (next === dark) {\n      animateTo(next ? 1 : 0);\n      return;\n    }\n    if (checked === undefined) setInner(next);\n    onChange?.(next);\n  };\n\n  const skyTop = ramp([\"#3d8bff\", \"#4a62c9\", \"#2b2266\", \"#060a1c\"], p);\n  const skyBot = ramp([\"#a9d6ff\", \"#ffb08a\", \"#6a3a7a\", \"#16204a\"], p);\n  const hillB = ramp([\"#3f8a63\", \"#5a4a5e\", \"#211a33\", \"#0b0f1d\"], p);\n  const hillF = ramp([\"#2c6e4c\", \"#3e2f45\", \"#150f22\", \"#05070f\"], p);\n  const water = ramp([\"#5aa4e8\", \"#c97a6a\", \"#2a1f4a\", \"#0a1230\"], p);\n  const sx = 24 + 52 * p;\n  const sy = 15 + 34 * p * p;\n  const my = 58 - 44 * ease(Math.max(0, (p - 0.35) / 0.65));\n  const glow = Math.sin(Math.PI * Math.min(1, p * 1.15));\n  const starOp = Math.max(0, (p - 0.55) / 0.45);\n\n  return (\n    <button\n      type=\"button\"\n      role=\"switch\"\n      aria-checked={dark}\n      aria-label={label}\n      disabled={disabled}\n      data-scrub={scrub}\n      className={`hb-wf8 ${className}`}\n      style={{ ...style, [\"--hb-wf8-s\" as string]: `${size}px` }}\n      onPointerDown={(e) => {\n        if (disabled) return;\n        cancelAnimationFrame(anim.current.raf);\n        drag.current = { on: true, x: e.clientX, base: pRef.current, moved: false, w: e.currentTarget.clientWidth };\n        e.currentTarget.setPointerCapture(e.pointerId);\n      }}\n      onPointerMove={(e) => {\n        const d = drag.current;\n        if (!d.on) return;\n        const dx = e.clientX - d.x;\n        if (!d.moved && Math.abs(dx) < 4) return;\n        d.moved = true;\n        setScrub(true);\n        const v = Math.max(0, Math.min(1, d.base + dx / (d.w * 0.7)));\n        pRef.current = v;\n        setP(v);\n      }}\n      onPointerUp={() => {\n        const d = drag.current;\n        if (!d.on) return;\n        d.on = false;\n        setScrub(false);\n        if (d.moved) set(pRef.current > 0.5);\n      }}\n      onPointerCancel={() => {\n        drag.current.on = false;\n        setScrub(false);\n        animateTo(dark ? 1 : 0);\n      }}\n      onClick={(e) => {\n        onClick?.(e);\n        if (drag.current.moved) {\n          drag.current.moved = false;\n          return;\n        }\n        set(!dark);\n      }}\n      {...rest}\n    >\n      <style href=\"hb-micro-horizon-toggle\" precedence=\"default\">\n        {css}\n      </style>\n      <svg className=\"hb-wf8-svg\" viewBox=\"0 0 108 48\" preserveAspectRatio=\"xMidYMid slice\" aria-hidden=\"true\">\n        <defs>\n          <linearGradient id={`s${uid}`} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n            <stop offset=\"0\" stopColor={skyTop} />\n            <stop offset=\"1\" stopColor={skyBot} />\n          </linearGradient>\n          <radialGradient id={`g${uid}`}>\n            <stop offset=\"0\" stopColor=\"#ffb36b\" stopOpacity={(0.85 * glow).toFixed(3)} />\n            <stop offset=\"1\" stopColor=\"#ff7a4a\" stopOpacity=\"0\" />\n          </radialGradient>\n          <radialGradient id={`u${uid}`} cx=\"40%\" cy=\"35%\">\n            <stop offset=\"0\" stopColor=\"#fffbe6\" />\n            <stop offset=\"1\" stopColor={ramp([\"#ffd84a\", \"#ff9a3c\", \"#ff6a3c\"], p * 1.4)} />\n          </radialGradient>\n          <mask id={`m${uid}`}>\n            <rect width=\"108\" height=\"48\" fill=\"#fff\" />\n            <circle cx=\"89\" cy={(my - 3).toFixed(2)} r=\"6.2\" fill=\"#000\" />\n          </mask>\n          <clipPath id={`c${uid}`}>\n            <rect width=\"108\" height=\"34\" />\n          </clipPath>\n        </defs>\n        <rect width=\"108\" height=\"48\" fill={`url(#s${uid})`} />\n        <g opacity={starOp.toFixed(3)}>\n          {stars.map((s, i) => (\n            <circle key={i} className=\"hb-wf8-tw\" cx={s.x} cy={s.y} r={s.r} fill=\"#fff\" style={{ animationDelay: s.d }} />\n          ))}\n        </g>\n        <circle cx={sx.toFixed(2)} cy=\"32\" r=\"34\" fill={`url(#g${uid})`} />\n        <g clipPath={`url(#c${uid})`}>\n          <circle cx={sx.toFixed(2)} cy={sy.toFixed(2)} r=\"7.5\" fill={`url(#u${uid})`} />\n          <circle cx=\"85\" cy={my.toFixed(2)} r=\"7\" fill=\"#eef1ff\" mask={`url(#m${uid})`} />\n        </g>\n        <path d=\"M0 30c10-6 20-8 30-4s16 2 24-3 20-6 30 0 16 6 24 4v7H0z\" fill={hillB} />\n        <path d=\"M0 33c8-3 16-4 26-1s20 3 30 0 22-2 32 1 14 2 20 0v4H0z\" fill={hillF} />\n        <rect y=\"34\" width=\"108\" height=\"14\" fill={water} />\n        <g opacity={(0.55 * (1 - p) + 0.35 * starOp).toFixed(3)}>\n          <rect className=\"hb-wf8-wv\" x={(sx - 6).toFixed(2)} y=\"37\" width=\"12\" height=\"1\" rx=\"0.5\" fill=\"#fff\" />\n          <rect className=\"hb-wf8-wv\" x={(sx - 4).toFixed(2)} y=\"40.5\" width=\"8\" height=\"1\" rx=\"0.5\" fill=\"#fff\" style={{ animationDelay: \"-1.2s\" }} />\n          <rect className=\"hb-wf8-wv\" x={(sx - 2.5).toFixed(2)} y=\"44\" width=\"5\" height=\"1\" rx=\"0.5\" fill=\"#fff\" style={{ animationDelay: \"-2.4s\" }} />\n        </g>\n      </svg>\n    </button>\n  );\n}\n"
    }
  ]
}