Magnet
Wrapper that pulls its content toward the pointer when it comes near.
pnpm dlx shadcn@latest add @leo/magnetProps
| Name | Type | Default | Description |
|---|---|---|---|
| activeTransition | string | undefined | transform 0.3s ease-out | |
| children | ReactNode | ( <span className="inline-block rounded-full bg-white px-5 py-2 text-sm font-medium text-neutral-950"> Hover me </span> ) | |
| className | string | undefined | - | |
| disabled | boolean | undefined | false | |
| inactiveTransition | string | undefined | transform 0.5s ease-in-out | |
| innerClassName | string | undefined | ||
| magnetStrength | number | undefined | 2 | |
| padding | number | undefined | 100 | |
| wrapperClassName | string | undefined |
Source
"use client";
import React, { type HTMLAttributes, type ReactNode, useEffect, useRef, useState } from "react";
import { cn } from "@/lib/utils";
export interface MagnetProps extends HTMLAttributes<HTMLDivElement> {
children?: ReactNode;
padding?: number;
disabled?: boolean;
magnetStrength?: number;
activeTransition?: string;
inactiveTransition?: string;
wrapperClassName?: string;
innerClassName?: string;
}
const Magnet: React.FC<MagnetProps> = ({
children = (
<span className="inline-block rounded-full bg-white px-5 py-2 text-sm font-medium text-neutral-950">
Hover me
</span>
),
padding = 100,
disabled = false,
magnetStrength = 2,
activeTransition = "transform 0.3s ease-out",
inactiveTransition = "transform 0.5s ease-in-out",
wrapperClassName = "",
innerClassName = "",
className,
...props
}) => {
const [isActive, setIsActive] = useState<boolean>(false);
const [position, setPosition] = useState<{ x: number; y: number }>({ x: 0, y: 0 });
const magnetRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (disabled) {
setPosition({ x: 0, y: 0 });
return;
}
const handleMouseMove = (e: MouseEvent) => {
if (!magnetRef.current) return;
const { left, top, width, height } = magnetRef.current.getBoundingClientRect();
const centerX = left + width / 2;
const centerY = top + height / 2;
const distX = Math.abs(centerX - e.clientX);
const distY = Math.abs(centerY - e.clientY);
if (distX < width / 2 + padding && distY < height / 2 + padding) {
setIsActive(true);
const offsetX = (e.clientX - centerX) / magnetStrength;
const offsetY = (e.clientY - centerY) / magnetStrength;
setPosition({ x: offsetX, y: offsetY });
} else {
setIsActive(false);
setPosition({ x: 0, y: 0 });
}
};
window.addEventListener("mousemove", handleMouseMove);
return () => {
window.removeEventListener("mousemove", handleMouseMove);
};
}, [padding, disabled, magnetStrength]);
const transitionStyle = isActive ? activeTransition : inactiveTransition;
return (
<div
ref={magnetRef}
className={cn(wrapperClassName, className)}
style={{ position: "relative", display: "inline-block" }}
{...props}
>
<div
className={innerClassName}
style={{
transform: `translate3d(${position.x}px, ${position.y}px, 0)`,
transition: transitionStyle,
willChange: "transform",
}}
>
{children}
</div>
</div>
);
};
export { Magnet };
export default Magnet;