Skip to Content
zjkm-ui
Components

Magnetic Button

A button that leans toward the cursor.

Props
0.35
80px
<MagneticButton>
  <button>Get started</button>
</MagneticButton>

Installation

npx shadcn@latest add https://ui.zjkm.tech/r/magnetic-button.json
import { MagneticButton } from "@/components/magnetic-button"

Using JavaScript? Set tsx: false in components.json and the CLI converts it.

New to shadcn? Run npx shadcn@latest init first. The CLI needs components.json and the @/ import alias.

Usage

Wrap a button or link. When a mouse comes near, it leans toward the cursor, and it springs back when the cursor leaves. Touch and keyboard use are unchanged, and the button keeps its own semantics.

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

export function Cta() {
  return (
    <MagneticButton>
      <button>Get started</button>
    </MagneticButton>
  );
}

Props

PropTypeDefaultDescription
childrenReactNodeYour button or link.
strengthnumber0.35Share of the cursor's offset the button follows, from 0 to 1.
radiusnumber80Pixels around the button where the pull starts.
classNamestringClasses for the wrapper.

With reduced motion, the button stays put.

On this page