Specification
- Catalogue
- No. 93 of 110
- Section
- Buttons and toggles
- Interaction
- Hover, Press
- Element
- Button
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
The button leans toward the pointer while it is near, then springs back when the pointer leaves. The label is fixed inside the button, so the two move as one piece and the text never slides around within its pill.
The offset is the pointer's distance from the centre multiplied by a pull factor (0.32), clamped so the button never leaves its spot. Only the button is transformed; the label inherits the movement. Two transitions do the feel: a short ease-out while following, and a slower overshooting curve for the return.
The preview can be scaled, so pointer positions are divided by rect.width / offsetWidth before use. Touch input is ignored, since there is no hover to follow.
Code
React and plain CSS"use client";
import { useEffect, useRef, useState, type CSSProperties, type PointerEvent } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./magnetic-button.module.css";
type Point = { x: number; y: number };
/** How far the button follows the pointer, as a share of the distance. The label moves with it. */
const PULL = 0.32;
const MAX_SHIFT = 36;
const DEMO_PATH: Point[] = [
{ x: -110, y: -54 },
{ x: 96, y: -40 },
{ x: 70, y: 58 },
{ x: -64, y: 46 },
{ x: 0, y: 4 },
];
const clamp = (value: number) => Math.max(-MAX_SHIFT, Math.min(MAX_SHIFT, value));
export default function MagneticButton({ active, context }: LivePreviewProps) {
const areaRef = useRef<HTMLDivElement>(null);
const [userPoint, setUserPoint] = useState<Point | null>(null);
const [step, setStep] = useState(0);
const demo = active && context === "card";
// Each time the demo starts, it starts from the first step.
const [demoRunning, setDemoRunning] = useState(demo);
if (demoRunning !== demo) {
setDemoRunning(demo);
setStep(0);
}
useEffect(() => {
if (!demo) return;
let id = window.setTimeout(function tick() {
setStep((s) => s + 1);
id = window.setTimeout(tick, 900);
}, 200);
return () => window.clearTimeout(id);
}, [demo]);
const point = context === "card" ? (demo ? (DEMO_PATH[step % DEMO_PATH.length] ?? null) : null) : userPoint;
const shift = point ? { x: clamp(point.x * PULL), y: clamp(point.y * PULL) } : { x: 0, y: 0 };
function track(event: PointerEvent<HTMLDivElement>) {
if (event.pointerType !== "mouse") return;
const area = areaRef.current;
if (!area) return;
const rect = area.getBoundingClientRect();
// The preview may be scaled; convert screen pixels back to layout pixels.
const scale = rect.width / area.offsetWidth || 1;
setUserPoint({
x: (event.clientX - rect.left - rect.width / 2) / scale,
y: (event.clientY - rect.top - rect.height / 2) / scale,
});
}
const style = { "--x": `${shift.x}px`, "--y": `${shift.y}px` } as CSSProperties;
return (
<div ref={areaRef} className={styles.area} onPointerMove={track} onPointerLeave={() => setUserPoint(null)}>
{context === "card" && (
<span
className={styles.cursor}
data-visible={point ? "" : undefined}
style={{ transform: `translate(${point?.x ?? 0}px, ${point?.y ?? 0}px)` }}
aria-hidden="true"
/>
)}
<button type="button" className={styles.button} data-engaged={point ? "" : undefined} style={style} tabIndex={context === "card" ? -1 : 0}>
<span className={styles.label}>
Start a project
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M5 12h14M13 6l6 6-6 6" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" />
</svg>
</span>
</button>
</div>
);
}