React Magnetic Button – Interactive Magnetic UI Component
Explore a customizable magnetic button for React. Adjust attraction strength, spring stiffness, damping, colours, and shadow. Copy code or install directly.
Live vector preview rendered straight from the same 120 Hz spring engine—no heavy GIF compression artifacts, zero layout shift.
Customization playground
Tune outline, fill, accent, text colour, and shadow direction live above. The snippet updates automatically for copy-and-paste.
Customize
<MagneticSurface />Installation
Install directly into your project source with the Kipluo CLI (React/Tailwind v4) or add the matching Flutter package.
Install
$ npx kipluo add magnetic-surfaceWorking code example
import { MagneticSurface } from "@/components/kipluo/magnetic-surface";
export function HeroCta() {
return (
<MagneticSurface strength={0.35} radius={96}>
<button
type="button"
className="rounded-full bg-primary px-6 py-3 font-semibold text-primary-foreground"
>
Start free trial
</button>
</MagneticSurface>
);
}What Magnetic Surface does
- Measures pointer offset relative to the element's bounding box on every pointermove event and maps it to a bounded attraction vector.
- Drives translation through a 120 Hz semi-implicit Euler spring so the button accelerates smoothly toward the cursor and snaps back with natural overshoot on pointer leave.
- Disables magnetic drift automatically on coarse/touch pointers and under prefers-reduced-motion so mobile taps and keyboard users stay rock solid.
Accessibility & sound behavior
Keyboard & screen readers: The child stays a normal focusable element. Keyboard focus gives a gentle lift instead of the pointer pull.
Reduced motion: Automatically respects prefers-reduced-motion: reduce by snapping spring targets immediately without jarring travel.
Sound cues: A soft whoosh when the pull engages, a tiny tick when snapping to the center, and a lower whoosh when it lets go. Disable per instance with sound={false} or globally via setSoundEnabled(false).
API reference
strengthnumber- How far the child follows the pointer, from 0 to 1. Default
0.35. radiusnumber- Distance in px at which the pull starts. Default
120. springSpringConfig- How it settles back. Default
{ stiffness: 260, damping: 14, mass: 1 }. disabledboolean- Turns the effect off without unmounting. Default
false. soundboolean- Plays this component's sounds. The global switch setSoundEnabled(false) silences every component. Default
true.
Framework compatibility
| Platform / Tool | Status | Details |
|---|---|---|
| React 18 & 19 | Full support | Zero peer runtime lock-in beyond @kipluo/core. |
| Next.js App Router | Full support | Ships with 'use client' boundaries; SSR-safe hydration. |
| Vite / Remix / Astro | Full support | Standard ESM TypeScript source files you own. |
| Tailwind CSS v4 & shadcn/ui | Native | Inherits CSS variables and installs via shadcn registry or CLI. |
| Flutter (Dart) | Native package | Matching spring physics, themes, and PCM audio in packages/flutter/kipluo. |
Build it yourself: related tutorials
All tutorials →How to Build a Magnetic Button in React
Step-by-step guide to building a smooth, interruptible magnetic button in React using Pointer Events, requestAnimationFrame, and a lightweight spring integrator.
Magnetic Button in Vanilla JavaScript
Build a reusable zero-dependency magnetic button in plain JavaScript using Pointer Events, semi-implicit Euler springs, data attributes, and AbortController cleanup.
Magnetic Button in Next.js App Router
Keep your Next.js App Router pages as Server Components while wrapping Next.js <Link> and CTA buttons inside a tiny hydration-safe 'use client' magnetic boundary.
Magnetic Button with shadcn/ui
Compose a physics-driven magnetic button cleanly with shadcn/ui Button, buttonVariants, asChild (Radix Slot), disabled states, and Tailwind v4 focus rings.
Related components
- Open component →Elastic AccordionFree
Accordion panels that open on a spring (a touch of overshoot, then settle), with a chevron that turns on the same spring. One-at-a-time or many-open.
- Open component →Arc Reveal 3DPaid
Interruptible 3D curved arc curtain that peels upward in perspective with spring overshoot to reveal stage content, with a clean cross-fade for reduced motion.
- Open component →Tilt CardFree
A card that tilts toward the pointer in 3D with a glare sliding across it, and springs flat when the pointer leaves. Off on touch and with reduced motion.
- Open component →Infinite MarqueeFree
Seamless ticker strip that decelerates cleanly on hover, accepts drag/fling momentum in either direction, and pauses under reduced motion.
- Open component →Expandable Search PillFree
Compact search icon button that springs open into a focused query pill with clear button pop and Escape dismissal.
- Open component →Elastic Step TrackerFree
Wizard progress rail whose active marker is a gooey pill: its leading edge runs ahead on a stiff spring and the tail catches up, so it stretches in flight. Drag it and release to snap to a step.