# Magnetic Button

> A button that leans toward the cursor with a springy, physical pull.

Category: Buttons · Free · Updated 2026-11-14 · Live demo: https://www.motionkitui.com/components/magnetic-button

## When to use it

Your main call to action: a hero's sign-up button, a pricing plan's buy button or a contact link. One per screen is plenty. The pull invites a click without shouting about it.

## How it works

On every pointer move the button measures how far the cursor is from its centre and sets x and y to a fraction of that distance (the strength prop). Both values run through Motion springs, so the button trails the cursor with a little weight and snaps home when the pointer leaves. Nothing re-renders while it moves: the springs write straight to the transform.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/magnetic-button.json
```

Or copy `components/motionkit/magnetic-button.tsx` below. Requires react, motion and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `strength` | `number` | `0.35` | How far (0–1) the button follows the pointer. |
| `...props` | `motion.button props` |  | Any button or Motion prop, e.g. onClick. |

## Accessibility

- **Keyboard: Pass.** A native <button>: reachable with Tab, activated with Enter or Space, with a visible focus ring.
- **Screen readers: Pass.** Announced as a button with its label. The pull effect is visual only.
- **Reduced motion: Pass.** With reduced motion on, the button stays put instead of following the cursor.
- **Colour contrast: Pass.** White on Petrol Teal measures 4.95:1, above the 4.5:1 AA minimum.

## Source

```tsx
"use client";

import { useRef, type ComponentProps, type PointerEvent } from "react";
import { motion, useMotionValue, useReducedMotion, useSpring } from "motion/react";

type MagneticButtonProps = Omit<ComponentProps<typeof motion.button>, "ref"> & {
  /** How far (0–1) the button follows the pointer. */
  strength?: number;
};

export function MagneticButton({
  strength = 0.35,
  className = "",
  children,
  onPointerMove,
  onPointerLeave,
  ...props
}: MagneticButtonProps) {
  const ref = useRef<HTMLButtonElement>(null);
  const reduceMotion = useReducedMotion();
  const x = useSpring(useMotionValue(0), { stiffness: 220, damping: 16, mass: 0.4 });
  const y = useSpring(useMotionValue(0), { stiffness: 220, damping: 16, mass: 0.4 });

  function handleMove(event: PointerEvent<HTMLButtonElement>) {
    onPointerMove?.(event);
    if (reduceMotion || !ref.current) return;
    const rect = ref.current.getBoundingClientRect();
    x.set((event.clientX - rect.left - rect.width / 2) * strength);
    y.set((event.clientY - rect.top - rect.height / 2) * strength);
  }

  function handleLeave(event: PointerEvent<HTMLButtonElement>) {
    onPointerLeave?.(event);
    x.set(0);
    y.set(0);
  }

  return (
    <motion.button
      ref={ref}
      style={{ x, y }}
      whileTap={{ scale: 0.96 }}
      onPointerMove={handleMove}
      onPointerLeave={handleLeave}
      className={`relative inline-flex items-center justify-center gap-2 rounded-xl bg-[#0E7C86] px-5 py-3 text-sm font-medium text-white shadow-[0_8px_30px_-8px_rgb(14_124_134/0.6)] transition-colors hover:bg-[#2E9098] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[#5FAEB4] ${className}`}
      {...props}
    >
      {children}
    </motion.button>
  );
}
```
