---
title: "Magnetic"
description: "Any item leans toward a mouse that comes near and springs home when it leaves. Magnetic Layer moves a part more or less than the item."
url: https://gelatinui.com/components/magnetic
markdown_url: https://gelatinui.com/components/magnetic.md
access: free
install: "npx shadcn@latest add @gelatin-ui/magnetic"
registry_item: https://gelatinui.com/r/magnetic.json
version: "2026-11-09"
index: https://gelatinui.com/llms.txt
docs_index: https://gelatinui.com/docs/llms.txt
---

# Magnetic

> Any item leans toward a mouse that comes near and springs home when it leaves. Magnetic Layer moves a part more or less than the item.

Page: https://gelatinui.com/components/magnetic

## When to use it

Best for: One main call to action, or the buttons of a dock, that lean toward the mouse.

- Wrap one child. The child keeps its own look, focus ring, press, and clicks.
- Lower `strength` for a large item: 0.3 suits a button, about 0.06 a card.
- Use `axis="x"` or `axis="y"` for a link that must stay on its line.
- Put `MagneticLayer` inside the child for an icon that moves `depth` times the pull.

## When not to use it

- For an item that turns in 3D toward the mouse, use [Tilt](https://gelatinui.com/components/tilt.md).
- For a plain action with no effect, use a [Button](https://gelatinui.com/components/button.md).

## Notes for agents

- The pull starts `range` px (default 40) outside the edge and grows to `strength` (default 0.3) of the pointer distance.
- A mouse or a pen near the item pulls it, and so does a finger held on it, until it lifts. A scroll that passes over it, a key, or `disabled` gives no pull.
- In a row, keep the gap to a neighbour larger than the pull, or lower `strength` or `range`.
- Use `useMagnetic(ref)` to get the two spring values for your own Motion element.
- No React render on a pointer move, and no work while the item is off screen.

## Anatomy

```text
Magnetic
└─ MagneticLayer (0..n)
```

## API reference

### Magnetic

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `strength` | `number` |  | The share of the pointer distance from the center that the item moves: 0.3 moves it 3px for each 10px. The pull grows with the size, so lower it for a large item (0.06 for a card). In a row, the item reaches past its edge toward the pointer (about 16px for a 140px button at 0.3): keep the gap to a neighbour larger, or lower `strength` or `range`, or it covers the neighbour. |
| `range` | `number` |  | How far outside the edge the pull starts, in px. 0 pulls only while the pointer is on the item. |
| `axis` | `"both" \| "x" \| "y"` |  | `x` or `y` moves the item on one axis only (a nav link that must stay on its line). |
| `disabled` | `boolean` | false | Springs the item home and stops the pointer work. |

### MagneticLayer

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `depth` | `number` | 2 | How far the layer moves, as a multiple of the item pull: 2 moves it twice as far as the item (an icon that floats ahead), 1 with the item, 0 keeps it still on the page, and below 0 it moves against the pull. |

## Styling

| Part | Slot | States | CSS variables | Tokens |
| --- | --- | --- | --- | --- |
| Magnetic | `magnetic` |  |  |  |
| MagneticLayer | `magnetic-layer` |  |  |  |

## Usage

Wrap one child, and put a `MagneticLayer` around a part that must move more than the item, such as the arrow.

The sample also uses `button`, which this add does not write. One command adds them with this item:

```bash
npx shadcn@latest add @gelatin-ui/magnetic @gelatin-ui/button
```

```tsx title="start-trial-button.tsx"
import { ArrowRightIcon } from "@phosphor-icons/react/ssr";

import { Magnetic, MagneticLayer } from "@/components/effects/magnetic";
import { Button } from "@/components/ui/button";

export function StartTrialButton() {
  return (
    <Magnetic>
      <Button size="lg">
        Start free trial
        <MagneticLayer depth={2}>
          <ArrowRightIcon data-icon="inline-end" aria-hidden />
        </MagneticLayer>
      </Button>
    </Magnetic>
  );
}
```

## Compose

Rules:

- Compose the parts (`compose-parts`): Build each item from its named parts as children, and pass your text and icons in as parts.
- Every state is designed (`every-state`): Give each view of data its loading, empty, and error states: a skeleton in the final layout, an `Empty` with an illustration and one next action, and an error that says what failed and how to retry.

## Add it

```bash
npx shadcn@latest add @gelatin-ui/magnetic
```

It adds 18 registry items: 17 files, 2720 lines, and the npm packages `@base-ui/react`, `@mantine/hooks`, `clsx`, `motion`, `tailwind-merge`.

## For AI agents

- Every item, one file: https://gelatinui.com/llms.txt
- MCP server (`search_items`, `get_item`, `get_install_plan`): https://gelatinui.com/mcp

## Related

- [Tilt](https://gelatinui.com/components/tilt.md)
- [Button](https://gelatinui.com/components/button.md)
- [Tactile Button](https://gelatinui.com/components/tactile-button.md)
- [Magnifying Dock](https://gelatinui.com/components/magnifying-dock.md)

## Accessibility

Decoration only: it adds no role, no name, and no focus stop. The child keeps its own.

On a touch screen only a finger pressed on the item pulls it, so a finger elsewhere on the page never moves it, and a tap still lands on the item.

## Motion

| Part | Motion |
| --- | --- |
| Pull and return | glide · spring (stiffness 700, damping 53), the item follows the pointer and springs home |
