(ref: RefObject<HTMLElement>)
| 4 | const spring = { damping: 1, stiffness: 10, restDelta: 0.001 }; |
| 5 | |
| 6 | export function useFollowPointer(ref: RefObject<HTMLElement>) { |
| 7 | const xPoint = useMotionValue(0); |
| 8 | const yPoint = useMotionValue(0); |
| 9 | const x = useSpring(xPoint, spring); |
| 10 | const y = useSpring(yPoint, spring); |
| 11 | |
| 12 | useEffect(() => { |
| 13 | if (!ref.current) return; |
| 14 | |
| 15 | const handlePointerMove = ({ clientX, clientY }: MouseEvent) => { |
| 16 | const element = ref.current!; |
| 17 | |
| 18 | frame.read(() => { |
| 19 | xPoint.set(clientX - element.offsetLeft - element.offsetWidth / 4); |
| 20 | yPoint.set(clientY - element.offsetTop - element.offsetHeight / 4); |
| 21 | }); |
| 22 | }; |
| 23 | |
| 24 | window.addEventListener("pointermove", handlePointerMove); |
| 25 | |
| 26 | return () => window.removeEventListener("pointermove", handlePointerMove); |
| 27 | }, []); |
| 28 | |
| 29 | return { x, y }; |
| 30 | } |
nothing calls this directly
no outgoing calls
no test coverage detected