Magnetic

An element pulled toward the pointer inside a radius.

magnetic.tsx

Installation

1

Install the packages

npm install @prequelui/react motion

@prequelui/react reuses the primitive library your components already depend on.

2

Add the component

npx shadcn@latest add https://animate.prequelui.terravidhal.me/r/magnetic.json

npx shadcn@latest add https://animate.prequelui.terravidhal.me/r/magnetic.json

3

Turn on the motion

npx @prequelui/cli@latest animate magnetic

The CLI finds the component through components.json, swaps the import and removes the CSS animation classes that Motion replaces.

Usage

Import the component and compose it exactly like the primitive it wraps. Motion is enabled by default; the tempo prop scales every duration and softens every spring.

magnetic.tsx
import { Magnetic } from "@/components/prequelui/effects/magnetic";

export default function Demo() {
  return (
    <Magnetic range={120} strength={0.5}>
      <button className="rounded-lg bg-primary px-4 py-2 text-primary-foreground">
        Pull me closer
      </button>
    </Magnetic>
  );
}
import { Magnetic } from "@/components/prequelui/magnetic";

export function Variant() {
  return (
    <Magnetic tempo={1.2}>
      …
    </Magnetic>
  );
}

Animation

A magnetic element watches the pointer, measures the distance from its own centre and moves toward the cursor with a falloff. The displacement is a continuous motion value driven by a spring, so entering, leaving and interrupting are all the same gesture.

  • Spring: stiffness 100, damping 10, mass 0.5.
  • Strength 0.5 at the centre, falling linearly to zero at the range edge.
  • The window listener is removed when the element is disabled or off-screen.
  • Touch pointers and reduced motion disable the effect entirely.
spring 100 / 10range 120 pxlinear falloffpointer aware

API Reference

Each part accepts the usual HTML props in addition to the motion props below. No accessibility prop is hidden behind the animation layer.

Magnetic

PropTypeDefault
strengthnumber0.5
rangenumber120
springOptionsSpringOptions100 / 10 / 0.5
onlyOnHoverbooleanfalse
disableOnTouchbooleantrue
classNamestring—

Built by Vidhal. The source is available on GitHub.

Last updated: 11/17/2026