Magnetic Button

A button that leans toward the cursor and springs back when it leaves.

Preview

"use client";

import { ArrowRight } from "lucide-react";
import { MagneticButton } from "@/components/motion/button";

export function ButtonMagneticPreview() {
  return (
    <div className="flex flex-wrap items-center justify-center gap-4">
      <MagneticButton variant="primary" size="md" strength={0.35}>
        Hover me
        <ArrowRight className="h-4 w-4" />
      </MagneticButton>
      <MagneticButton variant="secondary" size="md" strength={0.25}>
        Subtle pull
      </MagneticButton>
      <MagneticButton variant="outline" size="md" strength={0.5}>
        Strong pull
      </MagneticButton>
    </div>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/button-magnetic

API Reference

className?
string

Classes merged onto the root element.

Default—
size?
"sm" | "md" | "lg" | "icon"

Height and padding; "icon" is a square icon-only button. Default: "md"

Default—
variant?
"outline" | "ghost" | "primary" | "secondary"

Visual style: filled, card, ghost or outline. Default: "primary"

Default—
pressScale?
number

Scale while pressed. Default 0.97: subtle, so the press reads as feedback, not a squash.

Default—
ripple?
boolean

Spawn a Material-style ripple from the press point. Off by default.

Default—
sound?
boolean

Play a soft press sound. Off by default; a `SoundProvider` can mute it app-wide.

Default—
strength?
number

Magnetic pull strength. Default 0.25.

Default0.25
magneticClassName?
string

Class applied to the magnetic wrapper.

Default—
Also on React Native. Spring press, ripple, icons and a stateful variant.

More in Button

Updated