{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"twinkle-card","type":"registry:ui","title":"Twinkle Card","description":"A card whose background ripples into a field of twinkling canvas pixels on hover or focus, with built-in color themes.","dependencies":[],"registryDependencies":["utils"],"files":[{"path":"registry/ui/twinkle-card.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef, type ReactNode } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport type TwinkleCardVariant = \"lime\" | \"ocean\" | \"ember\" | \"candy\" | \"mono\";\n\nexport interface TwinkleCardProps {\n  children?: ReactNode;\n  /** Built-in color theme. */\n  variant?: TwinkleCardVariant;\n  /** Custom palette (hex colors). Overrides `variant`. */\n  colors?: string[];\n  /** Distance between pixels in px. */\n  gap?: number;\n  /** Twinkle speed multiplier. */\n  speed?: number;\n  /** Largest pixel size in px. */\n  pixelSize?: number;\n  className?: string;\n}\n\nconst PALETTES: Record<TwinkleCardVariant, string[]> = {\n  lime: [\"#c6ff3d\", \"#8ee000\", \"#e9ffb0\", \"#3dffa8\"],\n  ocean: [\"#38bdf8\", \"#0ea5e9\", \"#a5f3fc\", \"#6366f1\"],\n  ember: [\"#fb923c\", \"#f97316\", \"#fde047\", \"#ef4444\"],\n  candy: [\"#f472b6\", \"#e879f9\", \"#fbcfe8\", \"#a78bfa\"],\n  mono: [\"#fafafa\", \"#d4d4d4\", \"#a3a3a3\", \"#737373\"],\n};\n\ninterface Pixel {\n  x: number;\n  y: number;\n  color: string;\n  size: number;\n  max: number;\n  delay: number;\n  phase: number;\n  rate: number;\n}\n\nexport function TwinkleCard({\n  children,\n  variant = \"lime\",\n  colors,\n  gap = 6,\n  speed = 1,\n  pixelSize = 2.5,\n  className,\n}: TwinkleCardProps) {\n  const box = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const palette = (colors?.length ? colors : PALETTES[variant]).join(\",\");\n\n  useEffect(() => {\n    const el = box.current!;\n    const cv = canvas.current!;\n    const ctx = cv.getContext(\"2d\")!;\n    const reduce = matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const cols = palette.split(\",\");\n    let pixels: Pixel[] = [];\n    let w = 0;\n    let h = 0;\n    let raf = 0;\n    let mode: \"in\" | \"out\" = \"out\";\n    let start = 0;\n    let last = 0;\n\n    const build = () => {\n      const r = el.getBoundingClientRect();\n      const dpr = Math.min(devicePixelRatio || 1, 2);\n      w = r.width;\n      h = r.height;\n      cv.width = Math.round(w * dpr);\n      cv.height = Math.round(h * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      const step = Math.max(3, gap);\n      const cx = w / 2;\n      const cy = h / 2;\n      const far = Math.hypot(cx, cy);\n      const old = pixels;\n      pixels = [];\n      for (let x = step / 2; x < w; x += step) {\n        for (let y = step / 2; y < h; y += step) {\n          const d = Math.hypot(x - cx, y - cy) / far;\n          pixels.push({\n            x,\n            y,\n            color: cols[Math.floor(Math.random() * cols.length)],\n            size: 0,\n            max: pixelSize * (0.45 + Math.random() * 0.55),\n            // Ripple outward from the center, with a little noise so the edge feels organic.\n            delay: d * 520 + Math.random() * 140,\n            phase: Math.random() * Math.PI * 2,\n            rate: 0.6 + Math.random() * 1.4,\n          });\n        }\n      }\n      if (old.length && mode === \"in\") for (const p of pixels) p.size = p.max;\n      draw();\n    };\n\n    const draw = () => {\n      ctx.clearRect(0, 0, w, h);\n      for (const p of pixels) {\n        if (p.size <= 0.05) continue;\n        ctx.fillStyle = p.color;\n        ctx.fillRect(p.x - p.size / 2, p.y - p.size / 2, p.size, p.size);\n      }\n    };\n\n    const tick = (now: number) => {\n      const dt = Math.min(48, now - (last || now)) / 16.67;\n      last = now;\n      const t = (now - start) * speed;\n      let alive = false;\n      for (const p of pixels) {\n        if (mode === \"in\") {\n          if (t < p.delay) {\n            alive = true;\n            continue;\n          }\n          // Grow in, then twinkle between ~35% and 100% of the pixel's size.\n          const tw = 0.675 + 0.325 * Math.sin(p.phase + (now / 1000) * p.rate * 4 * speed);\n          const target = p.max * tw;\n          p.size += (target - p.size) * Math.min(1, 0.18 * dt * Math.max(0.3, speed));\n          alive = true;\n        } else if (p.size > 0) {\n          p.size = Math.max(0, p.size - 0.08 * p.rate * dt * Math.max(0.3, speed));\n          alive = true;\n        }\n      }\n      draw();\n      raf = alive ? requestAnimationFrame(tick) : 0;\n    };\n\n    const run = (next: \"in\" | \"out\") => {\n      mode = next;\n      if (reduce) {\n        for (const p of pixels) p.size = next === \"in\" ? p.max : 0;\n        draw();\n        return;\n      }\n      start = performance.now();\n      last = 0;\n      if (!raf) raf = requestAnimationFrame(tick);\n    };\n\n    const enter = () => run(\"in\");\n    const leave = () => {\n      if (!el.contains(document.activeElement)) run(\"out\");\n    };\n    const blur = (e: FocusEvent) => {\n      if (!el.contains(e.relatedTarget as Node) && !el.matches(\":hover\")) run(\"out\");\n    };\n\n    // The loop only runs while hovered or fading out; pause it if the card scrolls away mid-hover.\n    const io = new IntersectionObserver(([e]) => {\n      if (!e.isIntersecting && raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      } else if (e.isIntersecting && !raf && mode === \"in\" && !reduce) raf = requestAnimationFrame(tick);\n    });\n    const ro = new ResizeObserver(build);\n    ro.observe(el);\n    io.observe(el);\n    el.addEventListener(\"pointerenter\", enter);\n    el.addEventListener(\"pointerleave\", leave);\n    el.addEventListener(\"focusin\", enter);\n    el.addEventListener(\"focusout\", blur);\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      el.removeEventListener(\"pointerenter\", enter);\n      el.removeEventListener(\"pointerleave\", leave);\n      el.removeEventListener(\"focusin\", enter);\n      el.removeEventListener(\"focusout\", blur);\n    };\n  }, [palette, gap, speed, pixelSize]);\n\n  return (\n    <div\n      ref={box}\n      tabIndex={0}\n      className={cn(\n        \"group relative isolate grid aspect-[4/5] w-72 place-items-center overflow-hidden rounded-[1.75rem] border border-border bg-card outline-none transition-[border-color,transform] duration-500 hover:border-foreground/20 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background\",\n        className,\n      )}\n    >\n      <canvas ref={canvas} aria-hidden className=\"pointer-events-none absolute inset-0 -z-10 size-full\" />\n      {/* Soft vignette keeps the content readable over the pixel field. */}\n      <div\n        aria-hidden\n        className=\"pointer-events-none absolute inset-0 -z-10 opacity-0 transition-opacity duration-700 group-hover:opacity-100 group-focus-visible:opacity-100\"\n        style={{ background: \"radial-gradient(closest-side, var(--card) 30%, transparent 100%)\" }}\n      />\n      {children}\n    </div>\n  );\n}\n"}]}