Skip to content

cta · HTML and CSS

MagneticButton

Buttons and links that lean gently towards the pointer, so the main action feels within reach.

Magnetic spring wrapper for buttons and links with fine-pointer gating

  • HTML and CSS
  • Reduced motion: static
  • 1.88 KB gzip
  • Layout shift 0
View the source Spec sheet as Markdown

Install

Install
pnpm dlx shadcn@4.21.1 add @frictionborn/magnetic-button

Playground

Move the pointer near the button. Fine pointers only; nothing moves with reduced motion.

Your code
import { MagneticButton } from "@/components/ui/magnetic-button";

<MagneticButton>
  <button type="button">Start a project</button>
</MagneticButton>

In a full example

The full example loads as you scroll to it.

Accessibility

  • The wrapped button or link keeps its native semantics. The wrapper div uses overflow: visible so it does not clip focus rings, and keyboard focus immediately resets translation to origin.
  • Keyboard: Focus snaps the wrapper to its origin immediately and the child's own onFocus still runs; the wrapper uses overflow: visible
  • Touch: Zero pointer handlers attached on coarse/touch screens
  • WCAG criteria it is built for: 2.1.1, 2.4.7, 2.5.3

Props

PropTypeDefaultWhat it does
childrenReact.ReactElementnoneRequired. A single interactive element (button or link) wrapped by the magnetic controller.
pullDistancenumber16Maximum pull translation in pixels (replaces deprecated strength).
pausedbooleanfalseUniversal pause prop. Freezes magnetic displacement calculation.
tensionnumber180Spring stiffness coefficient.
dampingnumber16Spring velocity damping coefficient.
classNamestring""Additional CSS classes applied to the outer wrapper div (the built-in class fb-magnetic-button is kept).
styleReact.CSSPropertiesnoneOptional. No default. Inline styles on the wrapper div, merged after display: inline-block and position: relative (consumer wins).
refReact.Ref<HTMLDivElement>noneReceives the wrapper <div> (not the child button).
maxTiernumber3Hard ceiling on the capability tier.

Measured on the bench

Measured by the automated bench at commit 801163e on 2026-10-21. These numbers are written by the bench, never by hand.

MeasureResult
Size, gzip1.88 KB at load, 0.02 KB loaded later
Layout shift0
Frames when nothing should moveoff screen 0, hidden tab 0, reduced motion 0
axe accessibility scan0 serious, 0 critical

Browsers it ran in

Automated Playwright runs, some with device emulation. Results from physical phones and tablets are still pending, so none are listed.

EnvironmentTierResultDateNotes
Chromium Desktop (SwiftShader)3pass2026-10-2114 passed, 0 failed
Chromium Mobile (Pixel 7 emulation)2pass2026-10-2113 passed, 0 failed
Firefox Desktop3pass2026-10-2113 passed, 0 failed
WebKit Desktop3pass2026-10-2113 passed, 0 failed
WebKit Mobile (iPhone 14 emulation)2pass2026-10-2113 passed, 0 failed

On different devices

Reduced motion (static): Button retains static position without magnetic cursor tracking.

TierWhat it does
T3 FullSame behaviour as tier 2: spring physics on the shared frame loop while the pointer is over the wrapper, settling to zero frames
T2 StandardMagnetic pull with the configured strength, only when the device reports (hover: hover) and (pointer: fine)
T1 LiteStatic wrapper without pointer handlers (coarse pointer with low-end hints)
T0 StaticStatic wrapper without pointer handlers or translation (server render, prefers-reduced-motion, Save-Data, or no WebGL2)

Content Security Policy

  • Works without 'unsafe-eval'.
  • Needs no reveal gate.
  • Sets inline style attributes, so allow style-src-attr 'unsafe-inline'.

Goes well with