{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sparks",
  "type": "registry:ui",
  "title": "Sparks",
  "description": "A celebratory spark burst fired imperatively via ref: elongated white, silver, and pale-blue streaks fan outward, fall under gravity, and burn out bright-to-dark, with a fraction bouncing once. Pooled particles, auto-cleanup, offscreen-paused.",
  "dependencies": [],
  "registryDependencies": [
    "https://crucibleui.com/r/use-visibility-pause.json",
    "https://crucibleui.com/r/use-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/default/utility/sparks/sparks.tsx",
      "type": "registry:ui",
      "target": "components/crucible/sparks.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { cn } from \"@/lib/utils\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { useVisibilityPause } from \"@/hooks/use-visibility-pause\";\n\nexport interface SparksHandle {\n  /**\n   * Fire a burst. Coordinates are relative to the component's own top-left\n   * corner (its own bounding box, not the viewport) — omit both to burst\n   * from the component's center.\n   */\n  fire: (x?: number, y?: number) => void;\n}\n\nexport interface SparksProps extends React.ComponentPropsWithoutRef<\"div\"> {\n  /** Particles per burst. Default 36. */\n  count?: number;\n  /** Streak colors as hex strings (`#rgb` / `#rrggbb`), cycled per-particle. Defaults to white, silver, and pale blue. */\n  colors?: string[];\n  /** Base launch direction in degrees (0 = right, 90 = up). Default 90. */\n  angle?: number;\n  /** Fan width around `angle`, in degrees. 360 = fully omnidirectional. Default 130. */\n  spread?: number;\n  /** Initial launch speed, px/s. Default 420. */\n  velocity?: number;\n  /** Downward acceleration, px/s². Default 900. */\n  gravity?: number;\n  /** Pooled particle capacity — bursts recycle the oldest particle once full. Default 240. */\n  maxParticles?: number;\n  /** devicePixelRatio ceiling. Default 2. */\n  dpr?: number;\n}\n\ninterface Particle {\n  active: boolean;\n  x: number;\n  y: number;\n  vx: number;\n  vy: number;\n  life: number;\n  maxLife: number;\n  length: number;\n  width: number;\n  rgb: [number, number, number];\n  bounced: boolean;\n  bounceEligible: boolean;\n  isFlash: boolean;\n}\n\nfunction hexToRgb(hex: string): [number, number, number] {\n  let h = hex.replace(\"#\", \"\").trim();\n  if (h.length === 3) h = h[0] + h[0] + h[1] + h[1] + h[2] + h[2];\n  const int = parseInt(h, 16);\n  if (Number.isNaN(int) || h.length !== 6) return [255, 255, 255];\n  return [(int >> 16) & 255, (int >> 8) & 255, int & 255];\n}\n\nfunction makePool(size: number): Particle[] {\n  const pool: Particle[] = [];\n  for (let i = 0; i < size; i++) {\n    pool.push({\n      active: false,\n      x: 0,\n      y: 0,\n      vx: 0,\n      vy: 0,\n      life: 0,\n      maxLife: 1,\n      length: 0,\n      width: 2,\n      rgb: [255, 255, 255],\n      bounced: false,\n      bounceEligible: false,\n      isFlash: false,\n    });\n  }\n  return pool;\n}\n\n/** Find an inactive slot, or recycle the particle with the least life left. */\nfunction nextSlot(pool: Particle[]): Particle {\n  for (const p of pool) if (!p.active) return p;\n  let oldest = pool[0];\n  for (const p of pool) if (p.life < oldest.life) oldest = p;\n  return oldest;\n}\n\nconst DEFAULT_COLORS = [\"#ffffff\", \"#d9dce3\", \"#b9d2ff\"];\n\n/**\n * Sparks — a celebratory spark shower fired imperatively via ref: elongated\n * glowing streaks fan outward, fall under gravity, and burn out bright-to-dark\n * rather than gently fading, with a fraction bouncing once. Not confetti —\n * every particle is a short streak of white, silver, or pale-blue light.\n * Pooled particles, zero allocation per burst, auto-cleanup on unmount. Under\n * reduced motion a burst becomes a brief, stationary flash instead of a\n * physics-driven shower.\n */\nexport const Sparks = React.forwardRef<SparksHandle, SparksProps>(function Sparks(\n  {\n    count = 36,\n    colors = DEFAULT_COLORS,\n    angle = 90,\n    spread = 130,\n    velocity = 420,\n    gravity = 900,\n    maxParticles = 240,\n    dpr = 2,\n    className,\n    ...props\n  },\n  ref\n) {\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const reducedMotion = useReducedMotion();\n\n  const internalsRef = React.useRef<{\n    ctx: CanvasRenderingContext2D;\n    pool: Particle[];\n    width: number;\n    height: number;\n    lastElapsed: number;\n    /** True while anything may be on the canvas — lets idle frames skip all work. */\n    dirty: boolean;\n  } | null>(null);\n\n  const rgbPalette = React.useMemo(() => colors.map(hexToRgb), [colors]);\n\n  const fire = React.useCallback(\n    (x?: number, y?: number) => {\n      const it = internalsRef.current;\n      if (!it) return;\n      const ox = x ?? it.width / 2;\n      const oy = y ?? it.height / 2;\n      const flash = reducedMotion;\n      it.dirty = true;\n      const burstCount = flash ? Math.min(8, count) : count;\n\n      for (let i = 0; i < burstCount; i++) {\n        const p = nextSlot(it.pool);\n        const rgb = rgbPalette[i % rgbPalette.length];\n        p.active = true;\n        p.bounced = false;\n        p.bounceEligible = Math.random() < 0.15;\n        p.isFlash = flash;\n        p.x = ox;\n        p.y = oy;\n        p.rgb = rgb;\n\n        if (flash) {\n          p.vx = 0;\n          p.vy = 0;\n          p.maxLife = p.life = 0.35 + Math.random() * 0.15;\n          p.length = 3 + Math.random() * 3;\n          p.width = p.length;\n          continue;\n        }\n\n        const spreadRad = (spread * Math.PI) / 180;\n        const baseRad = (angle * Math.PI) / 180;\n        const a = baseRad + (Math.random() - 0.5) * spreadRad;\n        const speed = velocity * (0.5 + Math.random() * 0.7);\n        p.vx = Math.cos(a) * speed;\n        p.vy = -Math.sin(a) * speed;\n        p.maxLife = p.life = 0.5 + Math.random() * 0.6;\n        p.length = 10 + Math.random() * 16;\n        p.width = 1.5 + Math.random();\n      }\n    },\n    [count, angle, spread, velocity, reducedMotion, rgbPalette]\n  );\n\n  React.useImperativeHandle(ref, () => ({ fire }), [fire]);\n\n  const tick = React.useCallback((elapsed: number) => {\n    const it = internalsRef.current;\n    if (!it) return;\n    const dt = Math.max(0, Math.min(elapsed - it.lastElapsed, 0.05));\n    it.lastElapsed = elapsed;\n    // Idle between bursts: the canvas is already clear, so do nothing.\n    if (!it.dirty) return;\n\n    const { ctx, pool, width, height } = it;\n    ctx.clearRect(0, 0, width, height);\n    let anyActive = false;\n    ctx.lineCap = \"round\";\n\n    for (const p of pool) {\n      if (!p.active) continue;\n      p.life -= dt;\n      if (p.life <= 0) {\n        p.active = false;\n        continue;\n      }\n      anyActive = true;\n      const t = Math.max(p.life / p.maxLife, 0);\n      const [r, g, b] = p.rgb;\n      // Burn out bright-to-dark rather than a uniform opacity fade.\n      const burn = t * t;\n      const cr = r * burn;\n      const cg = g * burn;\n      const cb = b * burn;\n      const alpha = Math.min(t * 1.6, 1);\n\n      if (p.isFlash) {\n        ctx.save();\n        ctx.globalAlpha = alpha;\n        ctx.fillStyle = `rgb(${cr}, ${cg}, ${cb})`;\n        ctx.shadowColor = `rgb(${r}, ${g}, ${b})`;\n        ctx.shadowBlur = 10;\n        ctx.beginPath();\n        ctx.arc(p.x, p.y, p.width, 0, Math.PI * 2);\n        ctx.fill();\n        ctx.restore();\n        continue;\n      }\n\n      p.vy += gravity * dt;\n      p.x += p.vx * dt;\n      p.y += p.vy * dt;\n\n      // A fraction of sparks bounce once off an implicit floor at the canvas edge.\n      if (!p.bounced && p.y > height && p.vy > 0) {\n        if (p.bounceEligible) {\n          p.vy *= -0.4;\n          p.vx *= 0.7;\n          p.y = height;\n        }\n        p.bounced = true;\n      }\n\n      const mag = Math.hypot(p.vx, p.vy) || 1;\n      const trail = p.length * (0.4 + t * 0.6);\n      const tailX = p.x - (p.vx / mag) * trail;\n      const tailY = p.y - (p.vy / mag) * trail;\n\n      ctx.save();\n      ctx.globalAlpha = alpha;\n      ctx.strokeStyle = `rgb(${cr}, ${cg}, ${cb})`;\n      ctx.shadowColor = `rgb(${r}, ${g}, ${b})`;\n      ctx.shadowBlur = 6;\n      ctx.lineWidth = p.width;\n      ctx.beginPath();\n      ctx.moveTo(p.x, p.y);\n      ctx.lineTo(tailX, tailY);\n      ctx.stroke();\n      ctx.restore();\n    }\n    // Everything burned out this frame — the clear above was the last draw.\n    if (!anyActive) it.dirty = false;\n  }, [gravity]);\n\n  const containerRef = useVisibilityPause<HTMLDivElement>(tick);\n\n  React.useEffect(() => {\n    const canvas = canvasRef.current;\n    const container = containerRef.current;\n    if (!canvas || !container) return;\n\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n\n    const effectiveDpr = Math.min(typeof window !== \"undefined\" ? window.devicePixelRatio || 1 : 1, dpr);\n\n    internalsRef.current = {\n      ctx,\n      pool: makePool(maxParticles),\n      width: 0,\n      height: 0,\n      lastElapsed: 0,\n      dirty: false,\n    };\n\n    const resize = () => {\n      const it = internalsRef.current;\n      if (!it) return;\n      const w = Math.max(container.clientWidth, 1);\n      const h = Math.max(container.clientHeight, 1);\n      canvas.width = Math.round(w * effectiveDpr);\n      canvas.height = Math.round(h * effectiveDpr);\n      canvas.style.width = \"100%\";\n      canvas.style.height = \"100%\";\n      ctx.setTransform(effectiveDpr, 0, 0, effectiveDpr, 0, 0);\n      it.width = w;\n      it.height = h;\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(container);\n    resize();\n\n    return () => {\n      ro.disconnect();\n      internalsRef.current = null;\n    };\n  }, [dpr, maxParticles]);\n\n  return (\n    <div\n      ref={containerRef}\n      aria-hidden\n      data-crucible=\"sparks\"\n      className={cn(\"pointer-events-none absolute inset-0 z-40 overflow-hidden\", className)}\n      {...props}\n    >\n      <canvas ref={canvasRef} className=\"block h-full w-full\" />\n    </div>\n  );\n});\n"
    }
  ]
}