{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "click-spark",
  "title": "Click Spark",
  "description": "A wrapper that answers a press with a small burst from that point: lines, falling bits, one soft ring or a little confetti, on one canvas that only draws while something is alive.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "utils"
  ],
  "files": [
    {
      "path": "registry/miseui/ui/click-spark.tsx",
      "content": "import type * as React from \"react\"\nimport { useEffect, useRef } from \"react\"\nimport { useReducedMotion } from \"motion/react\"\nimport { createSparkEngine, type SparkEngine } from \"@/registry/miseui/ui/click-spark-engine\"\nimport { DEFAULT_COUNT, type ClickSparkVariant } from \"@/registry/miseui/ui/click-spark-math\"\nimport { cn } from \"@/registry/miseui/lib/utils\"\n\ntype ClickSparkProps = React.ComponentProps<\"div\"> & {\n  /** `spark` short lines radiating, `dots` round bits with light gravity, `ring` one soft band spreading, `confetti` small falling rects. */\n  variant?: ClickSparkVariant\n  /** How many pieces. `ring` is always one band. */\n  count?: number\n}\n\n/** A keyboard press has no position: it reports detail 0 and bursts from the middle of what was pressed. */\nfunction pressPoint(event: React.MouseEvent<HTMLElement>) {\n  if (event.detail !== 0) return { x: event.clientX, y: event.clientY }\n  const target = event.target instanceof Element ? event.target : event.currentTarget\n  const box = target.getBoundingClientRect()\n  return { x: box.left + box.width / 2, y: box.top + box.height / 2 }\n}\n\nfunction ClickSpark({ variant = \"spark\", count, className, children, onClick, ...props }: ClickSparkProps) {\n  const rootRef = useRef<HTMLDivElement>(null)\n  const engineRef = useRef<SparkEngine | null>(null)\n  const reduceMotion = useReducedMotion() ?? false\n\n  useEffect(\n    () => () => {\n      engineRef.current?.dispose()\n      engineRef.current = null\n    },\n    [],\n  )\n\n  return (\n    <div\n      ref={rootRef}\n      data-slot=\"click-spark\"\n      data-variant={variant}\n      className={cn(\"relative min-w-0\", className)}\n      onClick={(event) => {\n        // Reads the press and nothing else: no preventDefault, no stopPropagation, so links and forms behave as they would without it.\n        onClick?.(event)\n        const root = rootRef.current\n        if (reduceMotion || !root) return\n        engineRef.current ??= createSparkEngine(root)\n        const { x, y } = pressPoint(event)\n        engineRef.current.burst(x, y, variant, count ?? DEFAULT_COUNT[variant])\n      }}\n      {...props}\n    >\n      {children}\n    </div>\n  )\n}\n\nexport { ClickSpark, type ClickSparkProps, type ClickSparkVariant }\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/click-spark-engine.ts",
      "content": "import {\r\n  burstProgress,\r\n  canvasRect,\r\n  confettiPiece,\r\n  createBurst,\r\n  dotPosition,\r\n  ringBand,\r\n  sparkLine,\r\n  type Burst,\r\n  type ClickSparkVariant,\r\n} from \"@/registry/miseui/ui/click-spark-math\"\r\n\r\n/** Rapid pressing should not stack up cost. */\r\nconst MAX_BURSTS = 8\r\nconst MAX_DPR = 2\r\n/** Wide to narrow, each adding a little: a soft band with no hard line, redrawn at each radius so it never breaks into dashes. */\r\nconst RING_STROKES: readonly (readonly [number, number])[] = [\r\n  [16, 0.1],\r\n  [11, 0.14],\r\n  [7, 0.18],\r\n  [3, 0.24],\r\n]\r\n\r\n/** A near-black accent would vanish on a dark page, so the light is kept in a lightness band per theme. Neighbours walk away from hue 100. */\r\nconst CANVAS_CLASS = [\r\n  \"pointer-events-none absolute z-10\",\r\n  \"[--mise-spark-0:var(--mise)] [--mise-spark-1:var(--mise)] [--mise-spark-2:var(--mise)]\",\r\n  \"supports-[color:oklch(from_red_l_c_h)]:[--mise-spark-0:oklch(from_var(--mise)_clamp(0.55,l,0.76)_c_h)]\",\r\n  \"supports-[color:oklch(from_red_l_c_h)]:[--mise-spark-1:oklch(from_var(--mise)_clamp(0.55,l,0.76)_c_calc(h_+_36_*_sin((h_-_100)_*_1deg)))]\",\r\n  \"supports-[color:oklch(from_red_l_c_h)]:[--mise-spark-2:oklch(from_var(--mise)_clamp(0.6,l,0.8)_c_calc(h_+_72_*_sin((h_-_100)_*_1deg)))]\",\r\n  \"dark:supports-[color:oklch(from_red_l_c_h)]:[--mise-spark-0:oklch(from_var(--mise)_clamp(0.66,l,0.84)_c_h)]\",\r\n  \"dark:supports-[color:oklch(from_red_l_c_h)]:[--mise-spark-1:oklch(from_var(--mise)_clamp(0.66,l,0.84)_c_calc(h_+_36_*_sin((h_-_100)_*_1deg)))]\",\r\n  \"dark:supports-[color:oklch(from_red_l_c_h)]:[--mise-spark-2:oklch(from_var(--mise)_clamp(0.7,l,0.86)_c_calc(h_+_72_*_sin((h_-_100)_*_1deg)))]\",\r\n].join(\" \")\r\n\r\nfunction resolveTones(canvas: HTMLCanvasElement): readonly string[] {\r\n  return [0, 1, 2].map((tone) => {\r\n    canvas.style.color = `var(--mise-spark-${tone})`\r\n    return getComputedStyle(canvas).color\r\n  })\r\n}\r\n\r\nfunction drawBurst(context: CanvasRenderingContext2D, burst: Burst, progress: number, tones: readonly string[]) {\r\n  const tone = tones[0] ?? \"currentColor\"\r\n  context.fillStyle = tone\r\n  context.strokeStyle = tone\r\n  if (burst.variant === \"ring\") {\r\n    const { radius, alpha } = ringBand(progress)\r\n    RING_STROKES.forEach(([width, strength]) => {\r\n      context.globalAlpha = strength * alpha\r\n      context.lineWidth = width\r\n      context.beginPath()\r\n      context.arc(burst.x, burst.y, radius, 0, Math.PI * 2)\r\n      context.stroke()\r\n    })\r\n    return\r\n  }\r\n  burst.particles.forEach((particle) => drawParticle(context, burst, particle, progress, tones))\r\n}\r\n\r\nfunction drawParticle(context: CanvasRenderingContext2D, burst: Burst, particle: Burst[\"particles\"][number], progress: number, tones: readonly string[]) {\r\n  if (burst.variant === \"spark\") {\r\n    const { from, to } = sparkLine(particle, progress)\r\n    const cos = Math.cos(particle.angle)\r\n    const sin = Math.sin(particle.angle)\r\n    context.globalAlpha = 1\r\n    context.lineWidth = 1.75\r\n    context.lineCap = \"round\"\r\n    context.beginPath()\r\n    context.moveTo(burst.x + cos * from, burst.y + sin * from)\r\n    context.lineTo(burst.x + cos * to, burst.y + sin * to)\r\n    context.stroke()\r\n  } else if (burst.variant === \"dots\") {\r\n    const { x, y, radius, alpha } = dotPosition(particle, progress)\r\n    context.globalAlpha = alpha\r\n    context.beginPath()\r\n    context.arc(burst.x + x, burst.y + y, radius, 0, Math.PI * 2)\r\n    context.fill()\r\n  } else {\r\n    const { x, y, rotation, alpha } = confettiPiece(particle, progress)\r\n    const width = 5 * particle.size\r\n    const height = 3 * particle.size\r\n    context.globalAlpha = alpha\r\n    context.fillStyle = tones[particle.tone] ?? tones[0] ?? \"currentColor\"\r\n    context.save()\r\n    context.translate(burst.x + x, burst.y + y)\r\n    context.rotate(rotation)\r\n    context.fillRect(-width / 2, -height / 2, width, height)\r\n    context.restore()\r\n  }\r\n}\r\n\r\nexport interface SparkEngine {\r\n  burst: (clientX: number, clientY: number, variant: ClickSparkVariant, count: number) => void\r\n  dispose: () => void\r\n}\r\n\r\nfunction createCanvas(root: HTMLElement) {\r\n  const canvas = document.createElement(\"canvas\")\r\n  canvas.setAttribute(\"aria-hidden\", \"true\")\r\n  canvas.dataset.slot = \"click-spark-canvas\"\r\n  canvas.className = CANVAS_CLASS\r\n  root.appendChild(canvas)\r\n  return canvas\r\n}\r\n\r\n/** Returns the device pixel scale the bitmap was sized to. */\r\nfunction fitCanvas(canvas: HTMLCanvasElement, root: HTMLElement) {\r\n  const box = root.getBoundingClientRect()\r\n  const page = document.documentElement\r\n  const rect = canvasRect(box.width, box.height, page.clientWidth - box.right, page.scrollHeight - window.scrollY - box.bottom)\r\n  const scale = Math.min(window.devicePixelRatio || 1, MAX_DPR)\r\n  canvas.style.left = `${rect.left}px`\r\n  canvas.style.top = `${rect.top}px`\r\n  canvas.style.width = `${rect.width}px`\r\n  canvas.style.height = `${rect.height}px`\r\n  canvas.width = Math.round(rect.width * scale)\r\n  canvas.height = Math.round(rect.height * scale)\r\n  return scale\r\n}\r\n\r\n/** The canvas is created on the first press and only ever draws while something is alive. */\r\nexport function createSparkEngine(root: HTMLElement): SparkEngine {\r\n  const state = {\r\n    canvas: null as HTMLCanvasElement | null,\r\n    context: null as CanvasRenderingContext2D | null,\r\n    observer: null as ResizeObserver | null,\r\n    tones: [] as readonly string[],\r\n    bursts: [] as readonly Burst[],\r\n    frame: 0,\r\n    seed: 1,\r\n    scale: 1,\r\n  }\r\n\r\n  const fit = () => {\r\n    if (state.canvas) state.scale = fitCanvas(state.canvas, root)\r\n  }\r\n\r\n  const draw = (now: number) => {\r\n    const { context, canvas } = state\r\n    state.frame = 0\r\n    if (!context || !canvas) return\r\n    state.bursts = state.bursts.filter((burst) => now - burst.startedAt < burst.life)\r\n    context.setTransform(state.scale, 0, 0, state.scale, 0, 0)\r\n    context.clearRect(0, 0, canvas.width, canvas.height)\r\n    state.bursts.forEach((burst) => drawBurst(context, burst, burstProgress(burst, now), state.tones))\r\n    if (state.bursts.length > 0) {\r\n      state.frame = requestAnimationFrame(draw)\r\n      return\r\n    }\r\n    // Nothing alive: no loop and no bitmap held for a wrapper that may cover a whole section.\r\n    canvas.width = 0\r\n    canvas.height = 0\r\n  }\r\n\r\n  const burst = (clientX: number, clientY: number, variant: ClickSparkVariant, count: number) => {\r\n    if (!state.canvas) {\r\n      state.canvas = createCanvas(root)\r\n      state.context = state.canvas.getContext(\"2d\")\r\n      // Only while something is on it. At rest the backing store is released, and the next press measures again.\r\n      state.observer = typeof ResizeObserver === \"undefined\" ? null : new ResizeObserver(() => state.bursts.length > 0 && fit())\r\n      state.observer?.observe(root)\r\n    }\r\n    if (!state.context) return\r\n    if (state.bursts.length === 0) fit()\r\n    state.tones = resolveTones(state.canvas)\r\n    const box = state.canvas.getBoundingClientRect()\r\n    state.bursts = [...state.bursts, createBurst(variant, clientX - box.left, clientY - box.top, performance.now(), count, state.seed)].slice(-MAX_BURSTS)\r\n    state.seed += 1\r\n    if (state.frame === 0) state.frame = requestAnimationFrame(draw)\r\n  }\r\n\r\n  const dispose = () => {\r\n    if (state.frame !== 0) cancelAnimationFrame(state.frame)\r\n    state.frame = 0\r\n    state.observer?.disconnect()\r\n    state.canvas?.remove()\r\n    state.canvas = null\r\n    state.context = null\r\n    state.bursts = []\r\n  }\r\n\r\n  return { burst, dispose }\r\n}\r\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/click-spark-math.ts",
      "content": "import { seededRandom } from \"@/registry/miseui/ui/background-seed\"\n\nexport type ClickSparkVariant = \"spark\" | \"dots\" | \"ring\" | \"confetti\"\n\nexport interface Particle {\n  angle: number\n  speed: number\n  size: number\n  /** 0, 1 or 2: which of the three accent neighbours it is. */\n  tone: number\n  spin: number\n}\n\nexport interface Burst {\n  variant: ClickSparkVariant\n  x: number\n  y: number\n  startedAt: number\n  life: number\n  particles: readonly Particle[]\n}\n\nexport const DEFAULT_COUNT: Record<ClickSparkVariant, number> = { spark: 8, dots: 10, ring: 1, confetti: 10 }\n/** Long enough to read as a burst, short enough to be gone by the next press. */\nexport const LIFE_MS: Record<ClickSparkVariant, number> = { spark: 380, dots: 600, ring: 520, confetti: 900 }\n/** The furthest anything travels from the press, so the canvas knows how much room to keep. */\nexport const REACH_PX = 72\n\n/** A ring is one band however many are asked for. */\nexport function createBurst(variant: ClickSparkVariant, x: number, y: number, startedAt: number, count: number, seed: number): Burst {\n  const random = seededRandom(seed)\n  const total = variant === \"ring\" ? 1 : Math.max(1, Math.round(count))\n  const particles = Array.from({ length: total }, (_, index) => ({\n    // Evenly round the circle with a little jitter, so a small count never leaves a visible gap.\n    angle: (index / total) * Math.PI * 2 + (random() - 0.5) * 0.5,\n    speed: 0.75 + random() * 0.25,\n    size: 0.8 + random() * 0.4,\n    tone: Math.floor(random() * 3),\n    spin: (random() - 0.5) * 8,\n  }))\n  return { variant, x, y, startedAt, life: LIFE_MS[variant], particles }\n}\n\nconst easeOutCubic = (value: number) => 1 - (1 - value) ** 3\nconst clampUnit = (value: number) => Math.min(1, Math.max(0, value))\n\nexport function burstProgress(burst: Burst, now: number) {\n  return clampUnit((now - burst.startedAt) / burst.life)\n}\n\n/** Both ends of the line in closed form: a line that flies out and shortens, rather than one that fades. */\nexport function sparkLine(particle: Particle, progress: number) {\n  const eased = easeOutCubic(progress)\n  const head = (10 + 26 * eased) * particle.speed\n  const length = 12 * (1 - progress) * particle.size\n  return { from: head, to: head + length }\n}\n\n/** Thrown outward, then pulled down a little. Light gravity is what makes the bits feel like they have weight. */\nexport function dotPosition(particle: Particle, progress: number) {\n  const distance = 36 * easeOutCubic(progress) * particle.speed\n  return {\n    x: Math.cos(particle.angle) * distance,\n    y: Math.sin(particle.angle) * distance + 22 * progress * progress,\n    radius: 2.5 * particle.size * (1 - progress * 0.6),\n    alpha: 1 - progress ** 3,\n  }\n}\n\nexport function ringBand(progress: number) {\n  const eased = easeOutCubic(progress)\n  return { radius: 8 + 40 * eased, alpha: (1 - progress) ** 2 }\n}\n\n/** Up and out at first, then falling, tumbling as it goes. */\nexport function confettiPiece(particle: Particle, progress: number) {\n  const eased = easeOutCubic(Math.min(1, progress * 1.6))\n  const distance = 34 * eased * particle.speed\n  return {\n    x: Math.cos(particle.angle) * distance,\n    y: Math.sin(particle.angle) * distance - 10 * eased + 46 * progress * progress,\n    rotation: particle.spin * progress,\n    alpha: progress < 0.7 ? 1 : (1 - progress) / 0.3,\n  }\n}\n\nexport interface CanvasRect {\n  left: number\n  top: number\n  width: number\n  height: number\n}\n\n/**\n * The room around the wrapper, kept inside the page: a canvas sticking out past the right or bottom edge of the\n * document would widen it or add a scrollbar. Left and top cannot do that.\n */\nexport function canvasRect(width: number, height: number, roomRight: number, roomBottom: number, reach = REACH_PX): CanvasRect {\n  const right = Math.min(reach, Math.max(0, roomRight))\n  const bottom = Math.min(reach, Math.max(0, roomBottom))\n  return { left: -reach, top: -reach, width: width + reach + right, height: height + reach + bottom }\n}\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/miseui/ui/background-seed.ts",
      "content": "/** Mulberry32. Server and client walk the same numbers, so a seeded layout hydrates without a mismatch. */\nexport function seededRandom(seed: number) {\n  let state = seed >>> 0\n  return () => {\n    state = (state + 0x6d2b79f5) >>> 0\n    let mixed = state\n    mixed = Math.imul(mixed ^ (mixed >>> 15), mixed | 1)\n    mixed ^= mixed + Math.imul(mixed ^ (mixed >>> 7), mixed | 61)\n    return ((mixed ^ (mixed >>> 14)) >>> 0) / 4294967296\n  }\n}\n\n/** Two decimals: enough for a percent or a second, and short in the inline style. */\nexport function between(random: () => number, min: number, max: number) {\n  return Math.round((min + random() * (max - min)) * 100) / 100\n}\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "interactions"
  ],
  "type": "registry:ui"
}