WebberUI

Magnetic Button

A magnetic button that is pulled towards the cursor as it approaches, with a parallax on the inner text and a spring back once the cursor leaves.

Loading preview…
npx shadcn@latest add https://webberui.com/r/magnetic-button.json

Playground

Tune the props live — the code snippet updates as you go, so you can dial in the look you want before copying it.

0.35
<MagneticButton />

Installation

npx shadcn@latest add https://webberui.com/r/magnetic-button.json

Installing also writes the --wb-duration-fast CSS variable into your global stylesheet.

Usage

import { MagneticButton } from "@/components/ui/magnetic-button";

<MagneticButton onClick={() => console.log("clicked")}>
  Get started
</MagneticButton>

Props

PropTypeDefaultDescription
strengthnumber0.35Magnet strength: the button's offset = the cursor's distance from center × strength; 0 means it does not move at all
springConfigSpringOptions{ stiffness: 260, damping: 18, mass: 0.7 }Spring parameters for the return after the cursor leaves
classNamestringOverrides the default styles (a solid button by default, changeable to an outline or other variant)
childrenReactNodeButton content

All other <button> attributes (onClick, disabled, aria-*, and so on) are forwarded directly.

How it works

  • Parallax depth: as the button shifts towards the cursor, the inner text shifts half as far in the opposite direction, producing a two-layer sense of depth
  • Spring back: driven by useMotionValue + useSpring, so it springs back to its original position with momentum once the cursor leaves
  • Disabled on touch devices automatically: it only responds to mouse pointer events (pointerType === "mouse"), so on touch it is just an ordinary button
  • When the user has "reduce motion" enabled at the system level, the magnetism is disabled: the button stays put and functionality is unaffected
  • The magnetism is also disabled while disabled

On this page