Overview
Magnetic Button extends an invisible field around a regular <button>. While a mouse moves inside it, the button is displaced toward the pointer through a spring, and its label travels a little further to suggest depth. Touch input, keyboard users and reduced-motion preferences get a calm, static button with a clear focus ring.
01Installation
Method 01 · Registry
CLI verifiedUses the shadcn CLI in a project initialised with shadcn init. Installs the file to components/ui/magnetic-button.tsx and adds its dependencies.
npx shadcn@latest add https://wancraft.my/r/magnetic-button.jsonMethod 02 · Copy and paste
- 1. Install the dependencies below.
- 2. Copy the source from the Code tab into
components/ui/magnetic-button.tsx. - 3. Import it and adjust props.
npm install motion- Dependencies
- motion
- Peer
- react ≥ 18, tailwindcss ≥ 4
- Added
- 1 Oct 2026
- Updated
- —
02Usage
The default configuration. Change properties above to generate a tailored example in the Usage tab.
import { MagneticButton } from "@/components/ui/magnetic-button";
export function MagneticButtonExample() {
return (
<MagneticButton>
Get in touch
</MagneticButton>
);
}03Props
| Prop | Type | Default | Description |
|---|---|---|---|
| strength | number | 40 | How strongly the button follows the pointer (0–100). 0 disables magnetism. |
| radius | number | 48 | Extra distance in px around the button where the field is active. Does not affect layout. |
| spring | { stiffness: number; damping: number; mass: number } | { stiffness: 180, damping: 14, mass: 0.6 } | Spring used to move and settle the button. |
| accent | string | "#FF7448" | Accent colour for the fill, hover border and focus ring. |
| rounded | number | 999 | Corner radius in px. |
| variant | "solid" | "outline" | "solid" | Visual style. |
| ...props | ButtonHTMLAttributes | — | All native button attributes (onClick, disabled, aria-*) are forwarded. The ref points to the <button>. |
04Features
- 01Bounded magnetic field that never shifts surrounding layout
- 02Spring-driven motion with a separate label parallax
- 03Magnetism ignores touch and pen input
- 04Static button when prefers-reduced-motion is set
05Accessibility
- Renders a native <button> — Enter and Space work, ref is forwarded.
- Visible focus ring drawn with the accent colour (focus-visible).
- Motion is purely decorative; no information is conveyed by movement.