Each letter leans toward the cursor and springs home, like a magnetic button for every character.
A button that leans toward the cursor with a springy, physical pull.
Customize
Usage
import { MagneticButton } from "@/components/motionkit/magnetic-button";
<MagneticButton>
Get started
</MagneticButton>Motion
The exact curves this component runs, plotted with Motion's own maths.
Follow
Pointer moves over the button
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.
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.
Works in the App Router as is. Requires Tailwind CSS v4.
One command adds the file and installs its dependencies. Needs a components.json (run npx shadcn@latest init once if you don't have one).
npx shadcn@latest add https://www.motionkitui.com/r/magnetic-button.jsonnpm install motionSave as components/motionkit/magnetic-button.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>
);
}
| 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. |
How we audit: the WCAG 2.2 animation checklist and respecting reduced motion in React.
Yes. The file starts with "use client", so you can import it straight into a Server Component page or layout. It works in Vite as is, and in Astro as a React island.
Only the motion package, beside React and Tailwind CSS v4. There is no MotionKit package and no Tailwind config to add.
Yes. Run npx shadcn@latest add https://www.motionkitui.com/r/magnetic-button.json in any project with a components.json. It saves the file to components/motionkit and installs its dependencies.
With reduced motion on, the button stays put instead of following the cursor.
No. Touch has no hover, so the button stays put and works as a normal button with a small press effect.
The strength prop is the share of the cursor's distance the button follows, from 0 to 1. The default 0.35 feels noticeable without making the button hard to click; go lower for small buttons.
Each letter leans toward the cursor and springs home, like a magnetic button for every character.
A call to action whose arrow slides out on hover while a fresh one slides in from the left. Pure CSS, solid or outline.
An outline button that floods with colour from exactly where the cursor came in, and drains out towards where it leaves.