Skip to content

Magnetic Button

A tactile button that follows the pointer with a soft pull.

LIVE PREVIEWInteract with the component
Just React. No extra dependencies.

Copy the component and its CSS module into your project.

CSS Modules

Usage

Matches your current playground settings.

jsx
import MagneticButton from "./MagneticButton";
export default function Example() {
return (
<MagneticButton
children={"Get in touch ↗"}
strength={20}
background={"#e7dffc"}
color={"#241c36"}
borderRadius={32}
disabled={false}
/>
);
}

Using Magnetic Button

Place the action label in children and connect onClick to your application. strength controls how far the button follows the pointer.

Leave a little space around the button so it can move without colliding with nearby controls. Use disabled when its action is unavailable.

Props

NameTypeRequiredDefaultDescription
childrenReactNodeNoundefinedButton contents, including text or icons.
strengthnumberNo16Maximum total pointer travel in pixels.
backgroundstringNo"#e7dffc"CSS background for the button.
colorstringNo"#241c36"Text colour.
borderRadiusnumberNo999Corner radius in pixels; large values create a pill.
classNamestringNo""Additional CSS classes, merged with the component styles.
styleCSSPropertiesNoundefinedInline styles for caller-defined sizing and visual overrides.
disabledbooleanNofalseDisable native button interaction.
onPointerMovefunctionNoundefinedStandard native button event handler.
onPointerLeavefunctionNoundefinedStandard native button event handler.
onBlurfunctionNoundefinedStandard native button event handler.
onClick(event) => voidNoundefinedStandard button click handler. All other native button props are forwarded.

Related components

An action label contracts into a circular loader and resolves to a checkmark.

Alternating shutters close into a solid button face on hover or focus.

A raised rubber-stamp button leaves a fading outline when pressed.

A sculpted keycap compresses into its base with a satisfying press.