Skip to main content

Magnet Snap Button

A button that leans toward the cursor while it travels inside an attraction radius, then springs back on exit. Pointer tracking runs at the window level so the pull starts before you reach the button, the label drifts a touch further than the body for parallax, and coarse pointers plus reduced motion opt out automatically.

ButtonReactMotionTailwind CSSSolar Icons
CSSTailwind

Manual

Create a file and paste the following code into it.

magnet-snap-button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"use client";

import { useEffect, useRef } from "react";
import {
  motion,
  useMotionValue,
  useSpring,
  useTransform,
  type SpringOptions,
  type HTMLMotionProps,
} from "motion/react";
import { cva, type VariantProps } from "class-variance-authority";

import { cn } from "@/lib/cn";

/* ------------------------------------------------------------------ */
/*  Types                                                              */
/* ------------------------------------------------------------------ */

export const magneticButtonVariants = cva(
  cn(
    "relative inline-flex items-center justify-center gap-2 whitespace-nowrap",
    "rounded-full text-sm font-medium select-none transition-colors",
    "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-zinc-500/60",
    "focus-visible:ring-offset-2 focus-visible:ring-offset-white dark:focus-visible:ring-offset-zinc-950",
    "disabled:pointer-events-none disabled:opacity-50",
  ),
  {
    variants: {
      variant: {
        default: cn(
          "bg-zinc-900 text-white hover:bg-zinc-700",
          "dark:bg-white dark:text-zinc-900 dark:hover:bg-zinc-200",
        ),
        outline: cn(
          "border border-zinc-300 bg-transparent text-zinc-800 hover:border-zinc-500",
          "dark:border-zinc-700 dark:text-zinc-200 dark:hover:border-zinc-400",
        ),
        ghost: cn(
          "text-zinc-600 hover:bg-zinc-100 hover:text-zinc-900",
          "dark:text-zinc-400 dark:hover:bg-zinc-800/70 dark:hover:text-zinc-100",
        ),
      },
      size: {
        default: "h-10 px-5",
        lg: "h-12 px-7 text-base",
      },
    },
    defaultVariants: { variant: "default", size: "default" },
  },
);

export interface MagnetSnapButtonProps
  extends Omit<HTMLMotionProps<"button">, "style">,
    VariantProps<typeof magneticButtonVariants> {
  /** Attraction radius in px, measured from the button center. [Optional, default: 140] */
  radius?: number;
  /** Pull strength multiplier, 0 to 1. [Optional, default: 0.4] */
  strength?: number;
  /** Spring used for the pull and release. [Optional] */
  springOptions?: SpringOptions;
}

/* ------------------------------------------------------------------ */
/*  MagnetSnapButton                                                     */
/* ------------------------------------------------------------------ */

/**
 * A button that leans toward the cursor while it travels inside an
 * attraction radius, then springs back on exit. The pointer is tracked
 * at the window level so the pull starts before the cursor ever touches
 * the button, and the label drifts a little further than the body for a
 * subtle parallax. Attraction is skipped under prefers-reduced-motion
 * and on coarse (touch) pointers.
 * @param {number} radius - Attraction radius in px [Optional, default: 140]
 * @param {number} strength - Pull multiplier 0 to 1 [Optional, default: 0.4]
 * @param {SpringOptions} springOptions - Pull/release spring [Optional]
 */
export function MagnetSnapButton({
  children,
  radius = 140,
  strength = 0.4,
  springOptions = { stiffness: 160, damping: 16, mass: 0.1 },
  variant,
  size,
  className,
  ...props
}: MagnetSnapButtonProps) {
  const ref = useRef<HTMLButtonElement>(null);

  const rawX = useMotionValue(0);
  const rawY = useMotionValue(0);
  const x = useSpring(rawX, springOptions);
  const y = useSpring(rawY, springOptions);
  // The label overshoots the body slightly for depth.
  const labelX = useTransform(x, (v) => v * 0.42);
  const labelY = useTransform(y, (v) => v * 0.42);

  useEffect(() => {
    const prefersStill =
      window.matchMedia("(prefers-reduced-motion: reduce)").matches ||
      window.matchMedia("(pointer: coarse)").matches;
    if (prefersStill) return;

    const onPointerMove = (e: PointerEvent) => {
      const el = ref.current;
      if (!el) return;
      const rect = el.getBoundingClientRect();
      const dx = e.clientX - (rect.left + rect.width / 2);
      const dy = e.clientY - (rect.top + rect.height / 2);
      const dist = Math.hypot(dx, dy);

      if (dist < radius) {
        const pull = (1 - dist / radius) * strength;
        rawX.set(dx * pull);
        rawY.set(dy * pull);
      } else {
        rawX.set(0);
        rawY.set(0);
      }
    };
    const release = () => {
      rawX.set(0);
      rawY.set(0);
    };

    window.addEventListener("pointermove", onPointerMove, { passive: true });
    document.documentElement.addEventListener("pointerleave", release);
    return () => {
      window.removeEventListener("pointermove", onPointerMove);
      document.documentElement.removeEventListener("pointerleave", release);
    };
  }, [radius, strength, rawX, rawY]);

  return (
    <motion.button
      ref={ref}
      style={{ x, y }}
      whileTap={{ scale: 0.97 }}
      className={cn(magneticButtonVariants({ variant, size }), className)}
      {...props}
    >
      <motion.span
        style={{ x: labelX, y: labelY }}
        className="inline-flex items-center gap-2 font-medium"
      >
        {children}
      </motion.span>
    </motion.button>
  );
}

Update the import paths to match your project setup.

Similar components

Roll Reveal Button

Button Tilt

Gradient Party Button

Max

Animated Border Button

Install via CLI

Resource details

PublishedJuly 16, 2026
CategoryButton
ReactMotionTailwind CSSSolar Icons