# Magnetic Button

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

Source: https://nyxui.com/components/magnetic-button

---
<ComponentPreview name="magnetic-button-demo" />

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

<Tabs defaultValue="cli">

<TabsList>
  <TabsTrigger value="cli">CLI</TabsTrigger>
  <TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>

<TabsContent value="cli">

```bash
npx shadcn@latest add "https://nyxui.com/r/magnetic-button.json"
```

</TabsContent>

<TabsContent value="manual">

<Steps>

<Step>Install dependencies.</Step>

```bash
npm install motion
```

<Step>Copy and paste the following code into your project.</Step>

<ComponentSource name="magnetic-button" />

<Step>Update the import paths to match your project setup.</Step>

</Steps>

</TabsContent>

</Tabs>

## Usage

```tsx
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

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

## Props

| Prop             | Type                   | Default | Description                                                                                     |
| ---------------- | ---------------------- | ------- | ----------------------------------------------------------------------------------------------- |
| `strength`       | `number`               | `18`    | How far the button can travel toward the cursor, in pixels.                                     |
| `innerStrength`  | `number`               | `1.4`   | Inner element drift relative to the outer button. `1` matches; `>1` leads the button.           |
| `stiffness`      | `number`               | `200`   | Spring stiffness. Lower = looser.                                                               |
| `damping`        | `number`               | `15`    | Spring damping. Higher = snappier settle.                                                       |
| `innerClassName` | `string`               | —       | Extra classes for the inner (parallaxed) wrapper.                                               |
| `className`      | `string`               | —       | Extra classes for the outer button.                                                             |
| …`button`        | `ButtonHTMLAttributes` | —       | Everything else forwards to the underlying `<button>` (onClick, disabled, type, aria-\*, etc.). |