Magnetic Button
Calculates pointer offset inside a magnetic capture zone and smoothly translates both the pill container and inner label with damped spring return.
#button#magnetic#cursor#cta
Move pointer around stage to feel pull
Customize
0.35
#A961FD
Trailing Arrow
Installation
$pnpm dlx yourbrand@latest add @yourbrand/MagneticButton-TS-TWUsage (synced with live props)
<MagneticButton
label="Deploy Workspace"
strength={0.35}
accentColor="#A961FD"
showArrow
/>Source Code
MagneticButton.tsx"use client";
import React from "react";
export interface MagneticButtonProps {
label?: string;
strength?: number;
accentColor?: string;
showArrow?: boolean;
}
export function MagneticButton({
label = "Deploy Workspace",
strength = 0.35,
accentColor = "#A961FD",
showArrow = true,
}: MagneticButtonProps) {
const [offset, setOffset] = React.useState({ x: 0, y: 0 });
return (
<button
type="button"
onMouseMove={(e) => {
const rect = e.currentTarget.getBoundingClientRect();
const x = (e.clientX - (rect.left + rect.width / 2)) * strength;
const y = (e.clientY - (rect.top + rect.height / 2)) * strength;
setOffset({ x, y });
}}
onMouseLeave={() => setOffset({ x: 0, y: 0 })}
className="inline-flex items-center gap-2 rounded-[13px] px-6 py-3 text-sm font-semibold text-white transition-transform duration-150"
style={{ backgroundColor: accentColor, transform: `translate3d(${offset.x}px, ${offset.y}px, 0)` }}
>
<span>{label}</span>
{showArrow ? <span>→</span> : null}
</button>
);
}
Props API
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | "Deploy Workspace" | Primary CTA label. |
| strength | number | 0.35 | Pointer tracking multiplier. |
| accentColor | string (hex) | "#A961FD" | Button background fill. |
| showArrow | boolean | true | Show kinetic directional arrow. |