Jade-Infra-test / src /components /MagneticButton.jsx
rushiljain's picture
Upload 29 files
691cdd0 verified
import React, { useRef } from 'react';
export default function MagneticButton({ children, className = '', strength = 0.25, ...props }) {
const ref = useRef(null);
function onMove(e) {
const el = ref.current;
if (!el) return;
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
const rect = el.getBoundingClientRect();
const relX = e.clientX - rect.left - rect.width / 2;
const relY = e.clientY - rect.top - rect.height / 2;
el.style.transform = `translate(${relX * strength}px, ${relY * strength}px)`;
}
function onLeave() {
const el = ref.current;
if (!el) return;
el.style.transform = 'translate(0, 0)';
}
return (
<span
ref={ref}
className={className}
onMouseMove={onMove}
onMouseLeave={onLeave}
{...props}
>
{children}
</span>
);
}