Free · MIT licence · No dependencies

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.

  • React
  • CSS
  • Hover
  • CTA
  • Spring
Magnetic Button

Get the code

Install in one linenpx shadcn@latest add https://alphenex.com/r/magnetic-button.json

Works 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>

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

No. It uses plain React, requestAnimationFrame and CSS, so it adds about 2 KB and no dependencies to your project.

Want motion like this on your website?

We design and build custom websites with motion, 3D and SEO built in. Websites from $500.

More free components

Explore our work