startermotion.
← All templates

Micro

Magnetic Buttons

The button is pulled toward the cursor while hovered and springs back on leave. Movement is dampened so it feels weighted, not twitchy — the kind of micro-interaction that signals craft.

What's included

  • React component with spring physics
  • Configurable pull strength
  • Source Figma file

The code

npm install motion
"use client";

import { useRef } from "react";
import { motion, useMotionValue, useSpring } from "motion/react";

export function MagneticButton({
  children,
  strength = 0.4,
}: {
  children: React.ReactNode;
  strength?: number;
}) {
  const ref = useRef<HTMLButtonElement>(null);
  const x = useMotionValue(0);
  const y = useMotionValue(0);
  const sx = useSpring(x, { stiffness: 200, damping: 15 });
  const sy = useSpring(y, { stiffness: 200, damping: 15 });

  function onMove(e: React.MouseEvent) {
    const r = ref.current!.getBoundingClientRect();
    x.set((e.clientX - (r.left + r.width / 2)) * strength);
    y.set((e.clientY - (r.top + r.height / 2)) * strength);
  }

  return (
    <motion.button
      ref={ref}
      onMouseMove={onMove}
      onMouseLeave={() => { x.set(0); y.set(0); }}
      style={{ x: sx, y: sy }}
    >
      {children}
    </motion.button>
  );
}

Dependencies: motion