All components
Magnetic Button
buttonsAceternity UI component.
responsive · 350px
Install
Same command in any shadcn project — React (Vite/CRA), Next.js, Remix, Astro, and more:
$
npx shadcn@latest add https://your-domain/r/magnetic-button.jsonUsage
"use client";
import { MagneticButton } from "@/registry/aceternity-ui/magnetic-button";
export default function MagneticButtonDemo() {
return (
<MagneticButton strength={0.8} maxDistance={100}>
<button className="px-8 py-3 rounded-lg border border-border bg-background text-foreground text-sm font-medium shadow-sm hover:bg-muted transition-colors">
Magnetic
</button>
</MagneticButton>
);
}Component source
"use client";
import React, { useRef, useState } from "react";
import { motion } from "motion/react";
export const MagneticButton = ({
children,
strength = 0.8,
maxDistance = 100,
}: {
children: React.ReactNode;
strength?: number;
maxDistance?: number;
}) => {
const ref = useRef<HTMLDivElement>(null);
const [position, setPosition] = useState({ x: 0, y: 0 });
const handleMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {
if (!ref.current) return;
const { width, height, left, top } = ref.current.getBoundingClientRect();
const { clientX, clientY } = e;
let x = (clientX - (left + width / 2)) * strength;
let y = (clientY - (top + height / 2)) * strength;
const distance = Math.hypot(x, y);
if (distance > maxDistance) {
const scale = maxDistance / distance;
x *= scale;
y *= scale;
}
setPosition({ x, y });
};
const handleMouseLeave = () => {
setPosition({ x: 0, y: 0 });
};
const hasMoved = position.x !== 0 || position.y !== 0;
return (
<div
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
className="cursor-pointer rounded-lg border border-dashed transition-colors duration-150 [--show-color:var(--color-blue-500)]"
style={{
borderColor: hasMoved ? "var(--show-color)" : "transparent",
backgroundColor: hasMoved
? "color-mix(in srgb,var(--show-color) 20%, transparent)"
: "transparent",
}}
>
<motion.div
ref={ref}
animate={{ x: position.x, y: position.y }}
transition={{ type: "spring", stiffness: 150, damping: 25, mass: 0.1 }}
>
{children}
</motion.div>
</div>
);
};Dependencies
motion
Source: Aceternity UI