{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "particle-hover-button",
  "title": "Particle Hover Button",
  "description": "A wrapper that emits glowing, pooled particles from the cursor while hovering any button or link.",
  "dependencies": ["motion"],
  "registryDependencies": ["@soralabs/hooks-use-prefers-reduced-motion"],
  "files": [
    {
      "path": "registry/primitives/buttons/particle-hover-button/index.tsx",
      "content": "/* oxlint-disable react/no-array-index-key, react-doctor/no-array-index-as-key -- fixed-size particle pool, never reordered/resized. */\n\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport { animate } from \"motion\";\nimport { useEffect, useRef } from \"react\";\nimport type {\n  ReactNode,\n  PointerEvent as ReactPointerEvent,\n  Ref,\n  RefCallback,\n} from \"react\";\n\nimport { usePrefersReducedMotion } from \"@/hooks/use-prefers-reduced-motion\";\n\nconst DEFAULT_PARTICLE = (\n  <span className=\"block h-1.5 w-1.5 rounded-full bg-current\" />\n);\n\nconst PARTICLE_CLASS =\n  \"-translate-x-1/2 -translate-y-1/2 absolute top-0 left-0 opacity-0 [filter:drop-shadow(0_0_3px_currentColor)] dark:[mix-blend-mode:plus-lighter]\";\n\nexport interface ParticleHoverButtonClassNames {\n  /** The wrapper around `children` that pulses on hover. */\n  content?: string;\n  /** Each cloned particle node. */\n  particle?: string;\n}\n\nexport interface ParticleHoverButtonProps {\n  /** The interactive element (or plain label) this wraps — its own attributes/behavior are untouched. */\n  children: ReactNode;\n  className?: string;\n  /** Per-slot class overrides: `content`, `particle`. */\n  classNames?: ParticleHoverButtonClassNames;\n  /**\n   * Particle lifetime, in milliseconds.\n   * @default 1200\n   */\n  duration?: number;\n  /**\n   * Milliseconds between each particle emission while hovering.\n   * @default 100\n   */\n  interval?: number;\n  /** The node cloned for each emitted particle. @default a small filled dot */\n  particle?: ReactNode;\n  /**\n   * Number of pooled particle nodes reused across emissions — caps how\n   * many can be on screen at once, regardless of how long the pointer\n   * lingers.\n   * @default 24\n   */\n  poolSize?: number;\n  ref?: Ref<HTMLSpanElement>;\n  /**\n   * Horizontal drift jitter per particle, in pixels.\n   * @default 16\n   */\n  spread?: number;\n}\n\nfunction mergeRefs<T>(...refs: (Ref<T> | undefined)[]): RefCallback<T> {\n  return (node) => {\n    for (const ref of refs) {\n      if (!ref) {\n        continue;\n      }\n      if (typeof ref === \"function\") {\n        ref(node);\n      } else {\n        ref.current = node;\n      }\n    }\n  };\n}\n\nfunction clamp(value: number, min: number, max: number) {\n  return Math.min(Math.max(value, min), max);\n}\n\nexport function ParticleHoverButton({\n  children,\n  className,\n  classNames,\n  duration = 1200,\n  interval = 100,\n  particle = DEFAULT_PARTICLE,\n  poolSize = 24,\n  ref,\n  spread = 16,\n}: ParticleHoverButtonProps) {\n  const prefersReducedMotion = usePrefersReducedMotion();\n\n  const rootRef = useRef<HTMLSpanElement>(null);\n  const contentRef = useRef<HTMLSpanElement>(null);\n  const particleRefs = useRef<(HTMLSpanElement | null)[]>([]);\n  const inUseRef = useRef<boolean[]>([]);\n  const pointerRef = useRef({ x: 0, y: 0 });\n  const emitIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null);\n  const pulseControlsRef = useRef<ReturnType<typeof animate> | null>(null);\n  const activeParticleAnimationsRef = useRef<Set<ReturnType<typeof animate>>>(\n    new Set()\n  );\n\n  useEffect(\n    () => () => {\n      if (emitIntervalRef.current !== null) {\n        clearInterval(emitIntervalRef.current);\n      }\n      pulseControlsRef.current?.stop();\n      for (const controls of activeParticleAnimationsRef.current) {\n        controls.stop();\n      }\n      activeParticleAnimationsRef.current.clear();\n    },\n    []\n  );\n\n  const updatePointer = (e: ReactPointerEvent<HTMLSpanElement>) => {\n    const root = rootRef.current;\n    if (!root) {\n      return;\n    }\n    const rect = root.getBoundingClientRect();\n    pointerRef.current = {\n      x: clamp(e.clientX - rect.left, 0, rect.width),\n      y: clamp(e.clientY - rect.top, 0, rect.height),\n    };\n  };\n\n  const emitParticle = () => {\n    const index = inUseRef.current.findIndex((inUse) => !inUse);\n    if (index === -1) {\n      return;\n    }\n    const el = particleRefs.current[index];\n    if (!el) {\n      return;\n    }\n\n    inUseRef.current[index] = true;\n    el.style.left = `${pointerRef.current.x}px`;\n    el.style.top = `${pointerRef.current.y}px`;\n\n    const jitterA = (Math.random() * 2 - 1) * spread;\n    const jitterB = (Math.random() * 2 - 1) * spread;\n    const drift = 30 + Math.random() * 20;\n\n    const controls = animate(\n      el,\n      {\n        opacity: [0, 1, 1, 0],\n        scale: [0, 0.85, 0],\n        x: [0, jitterA, jitterB],\n        y: [0, -drift],\n      },\n      { duration: duration / 1000, ease: \"easeOut\" }\n    );\n    activeParticleAnimationsRef.current.add(controls);\n    controls.then(() => {\n      inUseRef.current[index] = false;\n      activeParticleAnimationsRef.current.delete(controls);\n    });\n  };\n\n  const handlePointerEnter = (e: ReactPointerEvent<HTMLSpanElement>) => {\n    if (prefersReducedMotion) {\n      return;\n    }\n    updatePointer(e);\n    if (emitIntervalRef.current === null) {\n      emitIntervalRef.current = setInterval(emitParticle, interval);\n    }\n    pulseControlsRef.current?.stop();\n    if (contentRef.current) {\n      pulseControlsRef.current = animate(\n        contentRef.current,\n        { scale: [1, 1.04, 1] },\n        { duration: 0.9, ease: \"easeInOut\", repeat: Number.POSITIVE_INFINITY }\n      );\n    }\n  };\n\n  const handlePointerLeave = () => {\n    if (emitIntervalRef.current !== null) {\n      clearInterval(emitIntervalRef.current);\n      emitIntervalRef.current = null;\n    }\n    pulseControlsRef.current?.stop();\n    if (contentRef.current) {\n      pulseControlsRef.current = animate(\n        contentRef.current,\n        { scale: 1 },\n        { duration: 0.3 }\n      );\n    }\n  };\n\n  return (\n    <span\n      className={cn(\"relative inline-flex\", className)}\n      onPointerEnter={handlePointerEnter}\n      onPointerLeave={handlePointerLeave}\n      onPointerMove={updatePointer}\n      ref={mergeRefs(rootRef, ref)}\n    >\n      <span\n        className={cn(\"relative z-[1] inline-flex\", classNames?.content)}\n        ref={contentRef}\n      >\n        {children}\n      </span>\n      {prefersReducedMotion ? null : (\n        <span\n          aria-hidden=\"true\"\n          className=\"pointer-events-none absolute inset-0 overflow-visible\"\n        >\n          {Array.from({ length: poolSize }, (_, index) => (\n            <span\n              className={cn(PARTICLE_CLASS, classNames?.particle)}\n              key={index}\n              ref={(el) => {\n                particleRefs.current[index] = el;\n                inUseRef.current[index] ??= false;\n              }}\n            >\n              {particle}\n            </span>\n          ))}\n        </span>\n      )}\n    </span>\n  );\n}\n",
      "type": "registry:ui",
      "target": "components/sora-ui/buttons/particle-hover-button.tsx"
    }
  ],
  "meta": {
    "keywords": [
      "button",
      "hover",
      "particles",
      "sponsor",
      "cta",
      "motion",
      "glow"
    ]
  },
  "type": "registry:ui"
}
