()
| 9 | } |
| 10 | |
| 11 | export 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected