{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "magnetic-button",
  "title": "Magnetic button",
  "description": "Button and its label are pulled toward a nearby cursor with a smooth falloff and spring back on leave; off for touch and reduced motion.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json"
  ],
  "files": [
    {
      "path": "registry/components/buttons/magnetic-button.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { motion, useMotionValue, useReducedMotion, useSpring } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { GooduiTokens } from \"@/lib/goodui-motion\"\n\nconst VARIANTS = {\n  default: \"bg-primary text-primary-foreground shadow-[var(--pc-e1)] hover:bg-primary/90\",\n  secondary: \"bg-secondary text-secondary-foreground hover:bg-secondary/80\",\n  outline: \"border bg-background text-foreground shadow-xs hover:bg-muted dark:bg-input/30 dark:hover:bg-input/50\",\n  ghost: \"text-foreground hover:bg-muted\",\n} as const\n\nconst SIZES = {\n  sm: \"h-8 gap-1.5 rounded-md px-3 text-xs [&_svg]:size-3.5\",\n  md: \"h-10 gap-2 rounded-lg px-5 text-sm [&_svg]:size-4\",\n  lg: \"h-12 gap-2 rounded-xl px-7 text-base [&_svg]:size-5\",\n  icon: \"size-11 rounded-full [&_svg]:size-5\",\n} as const\n\nconst SPRING = { stiffness: 220, damping: 15, mass: 0.6 }\n\nexport type MagneticButtonProps = Omit<React.ComponentProps<\"button\">, \"onAnimationStart\" | \"onDrag\" | \"onDragStart\" | \"onDragEnd\"> & {\n  /** \"default\" | \"secondary\" | \"outline\" | \"ghost\", matching shadcn's button variants. */\n  variant?: keyof typeof VARIANTS\n  /** \"sm\" | \"md\" | \"lg\" | \"icon\"; \"icon\" is a round 44px button. */\n  size?: keyof typeof SIZES\n  /** Share of the pointer offset the button follows (0–1). */\n  strength?: number\n  /** Extra share for the label, for a parallax feel. */\n  labelStrength?: number\n  /** Distance in px outside the button where the pull starts. */\n  radius?: number\n}\n\n/**\n * Button (and its label, a bit more) pulled toward the pointer when it comes near, springing back when it leaves.\n * Off for touch, coarse pointers, disabled buttons and reduced motion.\n */\nexport function MagneticButton({\n  variant = \"default\",\n  size = \"md\",\n  strength = 0.35,\n  labelStrength = 0.2,\n  radius = 70,\n  className,\n  children,\n  disabled,\n  ...props\n}: MagneticButtonProps) {\n  const reduce = useReducedMotion() ?? false\n  const anchor = React.useRef<HTMLSpanElement>(null)\n  const x = useMotionValue(0)\n  const y = useMotionValue(0)\n  const lx = useMotionValue(0)\n  const ly = useMotionValue(0)\n  const sx = useSpring(x, SPRING)\n  const sy = useSpring(y, SPRING)\n  const slx = useSpring(lx, SPRING)\n  const sly = useSpring(ly, SPRING)\n\n  React.useEffect(() => {\n    const el = anchor.current\n    if (!el || reduce || disabled) return\n    const fine = window.matchMedia(\"(hover: hover) and (pointer: fine)\")\n    let visible = false\n    let listening = false\n\n    const reset = () => {\n      x.set(0)\n      y.set(0)\n      lx.set(0)\n      ly.set(0)\n    }\n    const onMove = (e: PointerEvent) => {\n      if (e.pointerType === \"touch\") return\n      const r = el.getBoundingClientRect()\n      const dx = e.clientX - (r.left + r.width / 2)\n      const dy = e.clientY - (r.top + r.height / 2)\n      // Full pull over the button, fading to zero at `radius` outside it.\n      const out = Math.hypot(Math.max(0, Math.abs(dx) - r.width / 2), Math.max(0, Math.abs(dy) - r.height / 2))\n      const f = Math.max(0, 1 - out / radius)\n      if (f === 0) return reset()\n      const ease = f * f * (3 - 2 * f)\n      x.set(dx * strength * ease)\n      y.set(dy * strength * ease)\n      lx.set(dx * labelStrength * ease)\n      ly.set(dy * labelStrength * ease)\n    }\n    const sync = () => {\n      const want = visible && fine.matches && !document.hidden\n      if (want && !listening) {\n        window.addEventListener(\"pointermove\", onMove, { passive: true })\n        document.documentElement.addEventListener(\"pointerleave\", reset)\n        listening = true\n      } else if (!want && listening) {\n        window.removeEventListener(\"pointermove\", onMove)\n        document.documentElement.removeEventListener(\"pointerleave\", reset)\n        listening = false\n        reset()\n      }\n    }\n    const io = new IntersectionObserver(([entry]) => {\n      if (!entry) return\n      visible = entry.isIntersecting\n      sync()\n    })\n    io.observe(el)\n    fine.addEventListener(\"change\", sync)\n    document.addEventListener(\"visibilitychange\", sync)\n    return () => {\n      io.disconnect()\n      fine.removeEventListener(\"change\", sync)\n      document.removeEventListener(\"visibilitychange\", sync)\n      window.removeEventListener(\"pointermove\", onMove)\n      document.documentElement.removeEventListener(\"pointerleave\", reset)\n      reset()\n    }\n  }, [reduce, disabled, strength, labelStrength, radius, x, y, lx, ly])\n\n  return (\n    <span ref={anchor} className=\"inline-flex\">\n      <GooduiTokens />\n      <motion.button\n        data-slot=\"button\"\n        disabled={disabled}\n        style={{ x: sx, y: sy }}\n        whileTap={reduce ? undefined : { scale: 0.95 }}\n        className={cn(\n          \"relative inline-flex shrink-0 items-center justify-center font-medium whitespace-nowrap transition-[background-color,box-shadow,color] outline-none select-none focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 [&_svg]:shrink-0\",\n          VARIANTS[variant],\n          SIZES[size],\n          className,\n        )}\n        {...props}\n      >\n        <motion.span className=\"inline-flex items-center gap-[inherit]\" style={{ x: slx, y: sly }}>\n          {children}\n        </motion.span>\n      </motion.button>\n    </span>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/magnetic-button.tsx"
    }
  ],
  "categories": [
    "components",
    "buttons"
  ],
  "type": "registry:component"
}