Magnetic button
A pill button that leans toward the cursor and floods with color from the exact point you enter it.
"use client";
import { useRef, type ButtonHTMLAttributes, type PointerEvent } from "react";
import "./magnetic-button.css";
type MagneticButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
/** How strongly the button follows the cursor. 0 = not at all, 0.5 = a lot. */
strength?: number;
/** "solid" = filled, "outline" = border only */
variant?: "solid" | "outline";
theme?: "light" | "dark";
};
/**
* MagneticButton
* - Leans toward the mouse while hovered (and springs back on leave).
* - A color fill grows from the exact point where the pointer entered.
* - Works with keyboard (normal <button>) and touch (fill grows from the tap).
*/
export function MagneticButton({
strength = 0.3,
variant = "solid",
theme = "light",
className = "",
children,
...props
}: MagneticButtonProps) {
const ref = useRef<HTMLButtonElement>(null);
// Remember where the pointer is inside the button (used as the fill origin).
const setOrigin = (e: PointerEvent<HTMLButtonElement>) => {
const el = ref.current;
if (!el) return;
const r = el.getBoundingClientRect();
el.style.setProperty("--ox", `${e.clientX - r.left}px`);
el.style.setProperty("--oy", `${e.clientY - r.top}px`);
};
// Pull the button toward the mouse (mouse only, touch screens skip this).
const onMove = (e: PointerEvent<HTMLButtonElement>) => {
const el = ref.current;
if (!el || e.pointerType !== "mouse") return;
const r = el.getBoundingClientRect();
el.style.setProperty(
"--mx",
`${(e.clientX - (r.left + r.width / 2)) * strength}px`,
);
el.style.setProperty(
"--my",
`${(e.clientY - (r.top + r.height / 2)) * strength}px`,
);
};
// Spring back to the middle.
const onLeave = (e: PointerEvent<HTMLButtonElement>) => {
setOrigin(e);
ref.current?.style.setProperty("--mx", "0px");
ref.current?.style.setProperty("--my", "0px");
};
return (
<button
ref={ref}
data-theme={theme}
data-variant={variant}
className={`gu-magnetic ${className}`}
onPointerEnter={setOrigin}
onPointerDown={setOrigin}
onPointerMove={onMove}
onPointerLeave={onLeave}
{...props}
>
<span className="gu-magnetic__fill" aria-hidden="true" />
<span className="gu-magnetic__label">{children}</span>
</button>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Magnetic button
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A pill-shaped call-to-action button with two layered effects: it is magnetically pulled toward the mouse while hovered, and a circle of accent color floods the button starting from the exact point where the pointer entered.
## Layout and look
- Pill shape (fully rounded), minimum height 56px, horizontal padding 32px, 2px border in the button color.
- Text: system sans-serif, weight 700, 17px, letter-spacing -0.01em, centered.
- Two variants: "solid" (filled near-black #0e0e10 with white text) and "outline" (transparent with near-black text and border).
- Dark theme swaps the button color to #f3eee3 with near-black text.
- Accent fill color #ff5a1f with near-black text on top.
## Behavior and motion
1. On pointer enter, record the pointer position inside the button. A circle (2.6x the button width) centered there scales from 0 to 1 in 550ms with easing cubic-bezier(0.16, 1, 0.3, 1). Text switches to the fill text color.
2. While the mouse moves over the button, translate the button by (pointer offset from center × strength), strength default 0.3. Translate the label by an extra 35% of that offset for a parallax depth effect. Both ease over 600ms with the same curve.
3. On pointer leave, record the exit point (so the fill shrinks toward where the pointer left) and spring the button back to 0,0.
4. Only mouse pointers drive the magnetic pull.
## Touch
On touch screens there is no hover. On press: scale to 0.96 and grow the fill from the finger position in 350ms.
## Accessibility
- It is a real <button>. Focus-visible state shows the filled look plus a 3px accent outline with 4px offset.
- prefers-reduced-motion: no translation at all; the fill appears as a 200ms opacity fade instead of a growing circle.
## Props
strength (number, default 0.3), variant ("solid" | "outline"), theme ("light" | "dark"), plus every normal button attribute.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### MagneticButton.tsx
```tsx
"use client";
import { useRef, type ButtonHTMLAttributes, type PointerEvent } from "react";
import "./magnetic-button.css";
type MagneticButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
/** How strongly the button follows the cursor. 0 = not at all, 0.5 = a lot. */
strength?: number;
/** "solid" = filled, "outline" = border only */
variant?: "solid" | "outline";
theme?: "light" | "dark";
};
/**
* MagneticButton
* - Leans toward the mouse while hovered (and springs back on leave).
* - A color fill grows from the exact point where the pointer entered.
* - Works with keyboard (normal <button>) and touch (fill grows from the tap).
*/
export function MagneticButton({
strength = 0.3,
variant = "solid",
theme = "light",
className = "",
children,
...props
}: MagneticButtonProps) {
const ref = useRef<HTMLButtonElement>(null);
// Remember where the pointer is inside the button (used as the fill origin).
const setOrigin = (e: PointerEvent<HTMLButtonElement>) => {
const el = ref.current;
if (!el) return;
const r = el.getBoundingClientRect();
el.style.setProperty("--ox", `${e.clientX - r.left}px`);
el.style.setProperty("--oy", `${e.clientY - r.top}px`);
};
// Pull the button toward the mouse (mouse only, touch screens skip this).
const onMove = (e: PointerEvent<HTMLButtonElement>) => {
const el = ref.current;
if (!el || e.pointerType !== "mouse") return;
const r = el.getBoundingClientRect();
el.style.setProperty(
"--mx",
`${(e.clientX - (r.left + r.width / 2)) * strength}px`,
);
el.style.setProperty(
"--my",
`${(e.clientY - (r.top + r.height / 2)) * strength}px`,
);
};
// Spring back to the middle.
const onLeave = (e: PointerEvent<HTMLButtonElement>) => {
setOrigin(e);
ref.current?.style.setProperty("--mx", "0px");
ref.current?.style.setProperty("--my", "0px");
};
return (
<button
ref={ref}
data-theme={theme}
data-variant={variant}
className={`gu-magnetic ${className}`}
onPointerEnter={setOrigin}
onPointerDown={setOrigin}
onPointerMove={onMove}
onPointerLeave={onLeave}
{...props}
>
<span className="gu-magnetic__fill" aria-hidden="true" />
<span className="gu-magnetic__label">{children}</span>
</button>
);
}
```
### magnetic-button.css
```css
/* MagneticButton: change the four colors below to match your brand. */
.gu-magnetic {
--bg: #0e0e10; /* button color */
--fg: #ffffff; /* text color */
--fill: #ff5a1f; /* color that floods in on hover */
--fill-fg: #0e0e10; /* text color on top of the fill */
--mx: 0px;
--my: 0px;
--ox: 50%;
--oy: 50%;
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
min-height: 3.5rem;
padding: 0 2rem;
border: 2px solid var(--bg);
border-radius: 999px;
overflow: hidden;
isolation: isolate;
background: var(--bg);
color: var(--fg);
font:
700 1.05rem/1 system-ui,
sans-serif;
letter-spacing: -0.01em;
cursor: pointer;
transform: translate3d(var(--mx), var(--my), 0);
transition:
transform 0.6s cubic-bezier(0.16, 1, 0.3, 1),
color 0.3s;
-webkit-tap-highlight-color: transparent;
}
.gu-magnetic[data-theme="dark"] {
--bg: #f3eee3;
--fg: #0e0e10;
}
.gu-magnetic[data-variant="outline"] {
background: transparent;
color: var(--bg);
}
/* The fill: a circle that grows from the pointer's entry point. */
.gu-magnetic__fill {
position: absolute;
left: var(--ox);
top: var(--oy);
z-index: -1;
width: 260%;
aspect-ratio: 1;
border-radius: 50%;
background: var(--fill);
transform: translate(-50%, -50%) scale(0);
transition: transform 0.55s cubic-bezier(0.16, 1, 0.3, 1);
}
.gu-magnetic__label {
display: inline-block;
/* The label moves a little more than the button: a tiny depth effect. */
transform: translate3d(calc(var(--mx) * 0.35), calc(var(--my) * 0.35), 0);
transition: transform 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}
@media (hover: hover) {
.gu-magnetic:hover .gu-magnetic__fill {
transform: translate(-50%, -50%) scale(1);
}
.gu-magnetic:hover {
color: var(--fill-fg);
border-color: var(--fill);
}
}
/* Keyboard users get the same filled state. */
.gu-magnetic:focus-visible {
outline: 3px solid var(--fill);
outline-offset: 4px;
}
.gu-magnetic:focus-visible .gu-magnetic__fill {
transform: translate(-50%, -50%) scale(1);
}
.gu-magnetic:focus-visible {
color: var(--fill-fg);
}
/* Touch: squeeze, and flood from the finger. */
.gu-magnetic:active {
transform: translate3d(var(--mx), var(--my), 0) scale(0.96);
color: var(--fill-fg);
}
.gu-magnetic:active .gu-magnetic__fill {
transform: translate(-50%, -50%) scale(1);
transition-duration: 0.35s;
}
@media (prefers-reduced-motion: reduce) {
.gu-magnetic,
.gu-magnetic__label {
transform: none !important;
}
.gu-magnetic__fill {
transform: translate(-50%, -50%) scale(1);
opacity: 0;
transition: opacity 0.2s;
}
.gu-magnetic:hover .gu-magnetic__fill,
.gu-magnetic:focus-visible .gu-magnetic__fill {
opacity: 1;
}
}
```
You might also want
Next piece: Marker highlight3D tilt card
Components, intermediate
Glossy toys physics 3D hero
Components, advanced, needs three
Orbit border button
Components, beginner