{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "magnetic-button",
  "title": "Magnetic Button",
  "description": "A button that's pulled toward the pointer as it comes near, with its label drifting a little further for depth, a fill that sweeps in from the side you entered, and a springy wobble back into place when you move away.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/magnetic-button/magnetic-button.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\ntype Common = {\n  children: React.ReactNode;\n  /** How strongly the button follows the pointer (0–1). */\n  strength?: number;\n  /** How far outside the button the pull starts, in px. */\n  radius?: number;\n  /** How much further the label moves than the button, for a sense of depth. */\n  labelDepth?: number;\n  /** Springiness of the return: 0 = no overshoot, 1 = wobbly. */\n  bounce?: number;\n  /** Fill circle that grows from the pointer on hover. */\n  fill?: string;\n  /** Label colour while the fill covers the button. */\n  fillText?: string;\n  className?: string;\n  labelClassName?: string;\n};\n\nexport type MagneticButtonProps = Common &\n  (({ href: string } & Omit<React.ComponentPropsWithoutRef<\"a\">, \"children\" | \"className\">) | ({ href?: undefined } & Omit<React.ComponentPropsWithoutRef<\"button\">, \"children\" | \"className\">));\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\nexport function MagneticButton({ children, strength = 0.4, radius = 70, labelDepth = 0.5, bounce = 0.5, fill, fillText, className, labelClassName, ...props }: MagneticButtonProps) {\n  const reduce = useReducedMotion();\n  const ref = React.useRef<HTMLElement>(null);\n  const labelRef = React.useRef<HTMLSpanElement>(null);\n  const fillRef = React.useRef<HTMLSpanElement>(null);\n  const opts = React.useRef({ strength, radius, labelDepth, bounce });\n  React.useEffect(() => {\n    opts.current = { strength, radius, labelDepth, bounce };\n  }, [strength, radius, labelDepth, bounce]);\n\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el || reduce) return;\n    // A damped spring per axis: position, velocity and the target it's pulled toward.\n    const s = { x: 0, y: 0, vx: 0, vy: 0, tx: 0, ty: 0 };\n    let raf = 0;\n    let running = false;\n    let last = 0;\n\n    const tick = (now: number) => {\n      const dt = Math.min(0.032, last ? (now - last) / 1000 : 1 / 60);\n      last = now;\n      const { bounce: b, labelDepth: depth } = opts.current;\n      const k = 170;\n      const c = 2 * Math.sqrt(k) * (1 - b * 0.75);\n      s.vx += (-(s.x - s.tx) * k - s.vx * c) * dt;\n      s.vy += (-(s.y - s.ty) * k - s.vy * c) * dt;\n      s.x += s.vx * dt;\n      s.y += s.vy * dt;\n      el.style.transform = `translate3d(${s.x.toFixed(2)}px, ${s.y.toFixed(2)}px, 0)`;\n      if (labelRef.current) labelRef.current.style.transform = `translate3d(${(s.x * depth).toFixed(2)}px, ${(s.y * depth).toFixed(2)}px, 0)`;\n      const settled = Math.abs(s.x - s.tx) + Math.abs(s.y - s.ty) + Math.abs(s.vx) + Math.abs(s.vy) < 0.02;\n      if (settled) {\n        running = false;\n        last = 0;\n      } else raf = requestAnimationFrame(tick);\n    };\n    const kick = () => {\n      if (!running) {\n        running = true;\n        raf = requestAnimationFrame(tick);\n      }\n    };\n    const onMove = (event: PointerEvent) => {\n      if (event.pointerType !== \"mouse\") return;\n      // Measure the resting box (undo the current pull) so the field doesn't chase itself.\n      const r = el.getBoundingClientRect();\n      const cx = r.left + r.width / 2 - s.x;\n      const cy = r.top + r.height / 2 - s.y;\n      const dx = event.clientX - cx;\n      const dy = event.clientY - cy;\n      const { strength: st, radius: rad } = opts.current;\n      const inside = Math.abs(dx) < r.width / 2 + rad && Math.abs(dy) < r.height / 2 + rad;\n      const tx = inside ? dx * st : 0;\n      const ty = inside ? dy * st : 0;\n      if (tx !== s.tx || ty !== s.ty) {\n        s.tx = tx;\n        s.ty = ty;\n        kick();\n      }\n    };\n    const onLeaveWindow = () => {\n      s.tx = 0;\n      s.ty = 0;\n      kick();\n    };\n    window.addEventListener(\"pointermove\", onMove, { passive: true });\n    document.documentElement.addEventListener(\"pointerleave\", onLeaveWindow);\n    return () => {\n      cancelAnimationFrame(raf);\n      window.removeEventListener(\"pointermove\", onMove);\n      document.documentElement.removeEventListener(\"pointerleave\", onLeaveWindow);\n    };\n  }, [reduce]);\n\n  // The fill grows from the side the pointer entered and leaves toward the side it exits.\n  const sweep = (event: React.PointerEvent, on: boolean) => {\n    const f = fillRef.current;\n    if (!f || !fill) return;\n    const r = event.currentTarget.getBoundingClientRect();\n    f.style.left = `${event.clientX - r.left}px`;\n    f.style.top = `${event.clientY - r.top}px`;\n    f.style.transform = `translate(-50%, -50%) scale(${on ? 1 : 0})`;\n    if (labelRef.current && fillText) labelRef.current.style.color = on ? fillText : \"\";\n  };\n\n  const inner = (\n    <>\n      {fill && (\n        <span\n          ref={fillRef}\n          aria-hidden=\"true\"\n          className=\"pointer-events-none absolute aspect-square w-[250%] rounded-full transition-transform duration-500 ease-[cubic-bezier(.2,.8,.2,1)]\"\n          style={{ background: fill, transform: \"translate(-50%, -50%) scale(0)\", left: \"50%\", top: \"50%\" }}\n        />\n      )}\n      <span ref={labelRef} className={cn(\"relative inline-flex items-center gap-2 transition-colors duration-300 will-change-transform\", labelClassName)}>\n        {children}\n      </span>\n    </>\n  );\n  const classes = cn(\"relative inline-flex cursor-pointer items-center justify-center overflow-hidden will-change-transform\", className);\n  const hover = {\n    onPointerEnter: (event: React.PointerEvent) => sweep(event, true),\n    onPointerLeave: (event: React.PointerEvent) => sweep(event, false),\n  };\n\n  if (props.href !== undefined) {\n    const { href, ...rest } = props;\n    return (\n      <a ref={ref as React.Ref<HTMLAnchorElement>} href={href} className={classes} {...hover} {...rest}>\n        {inner}\n      </a>\n    );\n  }\n  const { type = \"button\", ...rest } = props as React.ComponentPropsWithoutRef<\"button\">;\n  return (\n    <button ref={ref as React.Ref<HTMLButtonElement>} type={type} className={classes} {...hover} {...rest}>\n      {inner}\n    </button>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}