Skip to main content
Clever Ops

Magnetic Button

Magnetic button done with restraint, at most 8px of pull inside an 80px radius, spring return on leave, and the effect disabled on touch and reduced motion so the hit target stays honest.

Preview & code

Loading Magnetic Button preview

Installation

pnpm dlx shadcn@latest add @cleverui/button-magnetic

Requires the @cleverui registry in your components.json, a one-off, two-line setup.

Usage

tsx
import { ButtonMagnetic } from '@/components/ui/button-magnetic'
Exampletsx
import { ArrowRight } from 'lucide-react'
import { ButtonMagnetic } from '@/components/ui/button-magnetic'

export default function ButtonMagneticDemo() {
  return (
    <div className="mx-auto flex min-h-[360px] max-w-3xl flex-col items-center justify-center gap-10 px-6 py-16">
      <div className="max-w-md text-center">
        <p className="font-mono text-[11px] uppercase tracking-[0.2em] text-muted-foreground">
          Meridian Studio · public beta
        </p>
        <p className="mt-3 text-sm leading-relaxed text-muted-foreground">
          Move the pointer near a control, it leans at most 8px. On touch devices the
          effect is off, so the hit target never lies.
        </p>
      </div>

      <div className="flex flex-wrap items-center justify-center gap-4">
        <ButtonMagnetic>
          Start free trial
          <ArrowRight className="size-4" aria-hidden="true" />
        </ButtonMagnetic>
        <ButtonMagnetic variant="ink">Book a walkthrough</ButtonMagnetic>
        <ButtonMagnetic variant="outline">View pricing</ButtonMagnetic>
      </div>

      <div className="flex flex-wrap items-center justify-center gap-4">
        <ButtonMagnetic size="sm">Export CSV</ButtonMagnetic>
        <ButtonMagnetic size="md">Invite teammates</ButtonMagnetic>
        <ButtonMagnetic size="lg">Open workspace</ButtonMagnetic>
      </div>
    </div>
  )
}

The example itself also uses lucide-react , install it too if you copy the example as-is (the component itself does not need it).

API reference

ButtonMagnetic accepts the following props.

PropTypeDefaultDescription
pullnumber8maximum translation in px, component clamps anything larger back to 8
radiusnumber80attraction radius in px measured from the button's center

…plus everything from Omit< React.ButtonHTMLAttributes<HTMLButtonElement>, 'onDrag' | 'onDragStart' | 'onDragEnd' | 'onAnimationStart' >, VariantProps<typeof buttonMagneticVariants>: className, event handlers, aria attributes and the rest pass straight through.

Frequently asked questions

How do I install the Magnetic Button component?

Register the @cleverui registry in your components.json ("@cleverui": "https://cleverops.com.au/r/{name}.json"), then run `npx shadcn@latest add @cleverui/button-magnetic`. The shadcn CLI copies the source into your project, you own the code from that point. You can also copy the source directly from this page.

What dependencies does Magnetic Button require?

Magnetic Button uses motion and class-variance-authority on top of React and Tailwind CSS. You don't need to install them manually, the shadcn CLI resolves and installs npm dependencies automatically when you add the component.

Can I theme Magnetic Button to match my brand?

Yes. The component is styled entirely with Tailwind utilities over standard design tokens (primary, background, muted, border), so it inherits your existing shadcn/ui theme automatically. Change your CSS variables and the component follows, no source edits needed for palette, radius, or fonts.

Want this built for you?

CleverUI is built by the Clever Ops web team. If you'd rather ship a hand-coded, fast, custom website than assemble one, we build them for Australian businesses every week.

Explore web development services