Magnetic Button

A button whose surface is pulled toward the cursor while hovering, with a secondary parallax on its inner label/icon.

ByMihirJaiswalUpdated

Loading...

Magnetic Button is a cursor-reactive React button whose surface travels toward the pointer while it hovers, bounded by the configured strength. A second motion value drives the inner label or icon on a different ratio, so the two layers parallax against each other, and on leave both spring back to centre.

Installation

Usage

import { ArrowUpRight } from "lucide-react";
import { MagneticButton } from "@/components/ui/magnetic-button";
 
export function Example() {
  return (
    <MagneticButton onClick={() => alert("hi")}>
      Say hello
      <ArrowUpRight className="size-4" />
    </MagneticButton>
  );
}

Tuning the pull

<MagneticButton strength={28} innerStrength={1.8} stiffness={160}>
  View work
</MagneticButton>

Props

PropTypeDefaultDescription
strengthnumber18How far the button can travel toward the cursor, in pixels.
innerStrengthnumber1.4Inner element drift relative to the outer button. 1 matches; >1 leads the button.
stiffnessnumber200Spring stiffness. Lower = looser.
dampingnumber15Spring damping. Higher = snappier settle.
innerClassNamestring—Extra classes for the inner (parallaxed) wrapper.
classNamestring—Extra classes for the outer button.
…buttonButtonHTMLAttributes—Everything else forwards to the underlying <button> (onClick, disabled, type, aria-*, etc.).