Magnetic Button Free React component
A call-to-action button that leans toward the cursor, glides back smoothly and fills with colour from the point where the cursor enters. Plain React and CSS, no libraries.
Get the code
npx shadcn@latest add https://alphenex.com/r/magnetic-button.jsonWorks with any React project through the shadcn CLI. Prefer to paste it yourself? Copy the two files below into components/alphenex/. No Tailwind or animation library needed.
import MagneticButton from '@/components/alphenex/magnetic-button';
<MagneticButton>
Book a call
</MagneticButton>Paste this into Claude, ChatGPT, Cursor, Lovable or v0 to build it from scratch.
Build a React "MagneticButton" component with plain CSS, no animation libraries. When the cursor comes within 80px of the button edge, the button moves toward the cursor by 20% of the distance and the label moves a further 15% of that, for a subtle parallax feel. Animate with requestAnimationFrame and a soft spring (stiffness around 0.06, damping around 0.8) so it glides smoothly with almost no overshoot. Measure from the resting centre so it never drifts. On hover, a circle of a second colour grows from the exact point where the cursor entered and fills the button. Turn the movement off for touch screens and prefers-reduced-motion. Props: strength, radius, color, fillColor, textColor, as, children.How the magnetic button works
The button listens to pointer movement on the whole window, not just on itself, so the pull starts before the cursor touches it. Each move sets a target offset: the distance from the button centre to the cursor, multiplied by the pull strength.
A tiny spring moves the button toward that target on every animation frame. Velocity chases the target and damping slows it, which gives a soft, smooth glide instead of a mechanical snap. The label moves a little further than the button, so it reads as two layers.
The hover fill is pure CSS. On pointer enter the component stores the entry point in two CSS variables, and a circle centred there scales from 0 to cover the button. Touch screens and reduced-motion users get a normal button with the fill only.
Frequently asked questions
Want motion like this on your website?
We design and build custom websites with motion, 3D and SEO built in. Websites from $500.