Skip to content
WANCRAFT

A.01Buttons & Controls

Magnetic Button

A button gently pulled toward the pointer inside a bounded magnetic field.

ReactMotionTailwindbasic

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 verified

Uses 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.json

Method 02 · Copy and paste

  1. 1. Install the dependencies below.
  2. 2. Copy the source from the Code tab into components/ui/magnetic-button.tsx.
  3. 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.

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

export function MagneticButtonExample() {
  return (
    <MagneticButton>
      Get in touch
    </MagneticButton>
  );
}

03Props

PropTypeDefaultDescription
strengthnumber40How strongly the button follows the pointer (0–100). 0 disables magnetism.
radiusnumber48Extra 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.
accentstring"#FF7448"Accent colour for the fill, hover border and focus ring.
roundednumber999Corner radius in px.
variant"solid" | "outline""solid"Visual style.
...propsButtonHTMLAttributes—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
motion · springinput · pointerinput · hoverinput · focus

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.