# Magnetic button

> A button that drifts after your cursor on a spring and snaps back into place when you move away.

- Category: [Buttons](https://library.aimran.es/components/buttons)
- Built with: React + Motion
- Live editor: https://library.aimran.es/blocks/magnetic-button
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

Use it on the one click that matters most: “Start free trial” on a SaaS landing page, “Get a quote” on a studio site, or the download button of an app. The way it follows the pointer draws the eye without flashing or shouting. If you were looking for a magnetic hover effect in React, this is it.

Start with the default intensity and only push it for large, isolated buttons. Next to a row of links it can feel restless. Touch screens have no hover, so on mobile it acts like a regular button.

## Code

### React (Motion)

```tsx
import { motion, useMotionValue, useSpring } from "framer-motion";

interface MagneticButtonProps {
  color?: string;
  radius?: number;
  text?: string;
  intensity?: number;
}

export default function MagneticButton({
  color = "#0071e3",
  radius = 24,
  text = "Hover me",
  intensity = 0.5,
}: MagneticButtonProps) {
  const x = useMotionValue(0);
  const y = useMotionValue(0);
  const sx = useSpring(x, { stiffness: 200, damping: 15 });
  const sy = useSpring(y, { stiffness: 200, damping: 15 });

  return (
    <motion.button
      type="button"
      style={{ x: sx, y: sy, backgroundColor: color, borderRadius: radius }}
      className="px-6 py-3 text-sm font-semibold text-white shadow-lg"
      onMouseMove={(e) => {
        const r = e.currentTarget.getBoundingClientRect();
        x.set((e.clientX - r.left - r.width / 2) * intensity);
        y.set((e.clientY - r.top - r.height / 2) * intensity);
      }}
      onMouseLeave={() => {
        x.set(0);
        y.set(0);
      }}
    >
      {text}
    </motion.button>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Magnetic button” block (Buttons) from AIMRAN Library to my project.

## What it is
A button that drifts after your cursor on a spring and snaps back into place when you move away.

## Chosen settings
- Color: #0071e3
- Corner radius: 24px (from 0 to 24px)
- Text: "Hover me"
- Intensity: 0.5 (from 0.2 to 2)

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with React + TypeScript, with framer-motion for the animation and lucide-react for the icons. If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
import { motion, useMotionValue, useSpring } from "framer-motion";

interface MagneticButtonProps {
  color?: string;
  radius?: number;
  text?: string;
  intensity?: number;
}

export default function MagneticButton({
  color = "#0071e3",
  radius = 24,
  text = "Hover me",
  intensity = 0.5,
}: MagneticButtonProps) {
  const x = useMotionValue(0);
  const y = useMotionValue(0);
  const sx = useSpring(x, { stiffness: 200, damping: 15 });
  const sy = useSpring(y, { stiffness: 200, damping: 15 });

  return (
    <motion.button
      type="button"
      style={{ x: sx, y: sy, backgroundColor: color, borderRadius: radius }}
      className="px-6 py-3 text-sm font-semibold text-white shadow-lg"
      onMouseMove={(e) => {
        const r = e.currentTarget.getBoundingClientRect();
        x.set((e.clientX - r.left - r.width / 2) * intensity);
        y.set((e.clientY - r.top - r.height / 2) * intensity);
      }}
      onMouseLeave={() => {
        x.set(0);
        y.set(0);
      }}
    >
      {text}
    </motion.button>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
