Back to EffectsYou are on Magnetic.

Magnetic

An element pulled toward the pointer inside a radius.

Available inAvailable in React, Vue, Svelte, Solid, JavaScript· All five frameworks
PrequelUI

Libraries

PrequelUI

Motion

Spring + short tween

Reduced motion

Fade in place
magnetic.tsx
Play with the real component, then read the code it ships with.All components
01

Installation

Installing for React·components/prequelui/effects/magnetic.tsx

Add the component

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/magnetic.json
npmthe default on every machine

First component? Set up your project first

Frameworks

The live preview is React. Each tab below is the file that ships for that framework, with the same durations, springs and reduced-motion rule.

5 runtimes
magnetic.tsx
"use client";

import * as React from "react";
import {
  motion,
  useMotionValue,
  useSpring,
  type HTMLMotionProps,
  type SpringOptions,
} from "motion/react";
import { cn } from "@/lib/utils";
import { useReduceMotion, useTempoTransition } from "../motion";
import { useCanHover } from "./shared";

export type MagneticProps = HTMLMotionProps<"div"> & {
  strength?: number;
  range?: number;
  springOptions?: SpringOptions;
  onlyOnHover?: boolean;
  disableOnTouch?: boolean;
};

const defaultSpring: SpringOptions = {
  stiffness: 100,
  damping: 10,
  mass: 0.5,
};

export function Magnetic({
  children,
  strength = 0.5,
  range = 120,
  springOptions,
  onlyOnHover = false,
  disableOnTouch = true,
  className,
  style,
  onPointerEnter,
  onPointerLeave,
  onPointerMove,
  ...props
}: MagneticProps) {
  const ref = React.useRef<HTMLDivElement>(null);
  const scrubX = useMotionValue(0);
  const scrubY = useMotionValue(0);
  const tempoSpring = useTempoTransition(springOptions ?? defaultSpring) as SpringOptions;
  const x = useSpring(scrubX, tempoSpring);
  const y = useSpring(scrubY, tempoSpring);
  const canHover = useCanHover();
  const reduce = useReduceMotion();
  const enabled = !reduce && (canHover || !disableOnTouch);
  const hovering = React.useRef(false);

  const reset = React.useCallback(() => {
    scrubX.set(0);
    scrubY.set(0);
  }, [scrubX, scrubY]);

  const compute = React.useCallback(
    (event: MouseEvent | React.PointerEvent) => {
      const node = ref.current;
      if (!node) return;
      const rect = node.getBoundingClientRect();
      const centerX = rect.left + rect.width / 2;
      const centerY = rect.top + rect.height / 2;
      const dx = event.clientX - centerX;
      const dy = event.clientY - centerY;
      const distance = Math.hypot(dx, dy);

      if (distance <= range) {
        const factor = (1 - distance / range) * strength;
        scrubX.set(dx * factor);
        scrubY.set(dy * factor);
      } else {
        reset();
      }
    },
    [range, reset, scrubX, scrubY, strength],
  );

  React.useEffect(() => {
    if (!enabled || onlyOnHover) return;
    const handleMove = (event: MouseEvent) => compute(event);
    window.addEventListener("mousemove", handleMove, { passive: true });
    return () => {
      window.removeEventListener("mousemove", handleMove);
      reset();
    };
  }, [compute, enabled, onlyOnHover, reset]);

  React.useEffect(() => {
    if (enabled) return;
    reset();
  }, [enabled, reset]);

  return (
    <motion.div
      ref={ref}
      data-slot="magnetic"
      className={cn("inline-block", className)}
      style={{ x, y, ...style }}
      onPointerEnter={(event) => {
        onPointerEnter?.(event);
        hovering.current = true;
      }}
      onPointerMove={(event) => {
        onPointerMove?.(event);
        if (enabled && onlyOnHover && hovering.current) compute(event);
      }}
      onPointerLeave={(event) => {
        onPointerLeave?.(event);
        hovering.current = false;
        reset();
      }}
      {...props}
    >
      {children}
    </motion.div>
  );
}

Install

npx shadcn@latest add https://prequeluianimate.terravidhal.me/r/magnetic.json
npmthe default on every machine

Dependencies

motion lucide-react

The registry keeps the component source, its motion tokens and the reduced-motion branch together, so the install never pulls a different animation from the preview.

02

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>
  );
}
03

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
04

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—

Next component

Marquee

A seamless row that drifts at a fixed speed and pauses under the pointer.