MCPcopy Create free account
hub / github.com/SyntaxUI/syntaxui / MagneticButton

Function MagneticButton

src/showcase/components/button/MagneticButton.tsx:11–42  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9}
10
11export default function MagneticButton() {
12 const ref = useRef<HTMLButtonElement>(null);
13 const [position, setPosition] = useState<Position>({ x: 0, y: 0 });
14
15 const handleMouse = (e: React.MouseEvent<HTMLButtonElement>) => {
16 if (ref.current) {
17 const { clientX, clientY } = e;
18 const { height, width, left, top } = ref.current.getBoundingClientRect();
19 const middleX = clientX - (left + width / 2);
20 const middleY = clientY - (top + height / 2);
21 setPosition({ x: middleX, y: middleY });
22 }
23 };
24
25 const reset = () => {
26 setPosition({ x: 0, y: 0 });
27 };
28
29 const { x, y } = position;
30 return (
31 <motion.button
32 ref={ref}
33 onMouseMove={handleMouse}
34 onMouseLeave={reset}
35 animate={{ x, y }}
36 transition={{ type: "spring", stiffness: 150, damping: 15, mass: 0.1 }}
37 className='relative rounded-xl bg-red-500 px-6 py-2 text-sm font-medium text-white'
38 >
39 SyntaxUI
40 </motion.button>
41 );
42}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected