Skip to content

Commit

Permalink
Add useMousePosition hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Peeke Kuepers committed Jun 10, 2022
1 parent dcac539 commit d53a6df
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/machinery/useMousePosition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useEvent } from '/machinery/useEvent'

export function useMousePosition(onMouseMove) {
const callback = useEvent(onMouseMove)

React.useEffect(
() => {
window.addEventListener('mousemove', handleMouseMove)
return () => window.removeEventListener('mousemove', handleMouseMove)
function handleMouseMove(e) {
callback({ x: e.clientX, y: e.clientY })
}
},
[callback]
)
}

0 comments on commit d53a6df

Please sign in to comment.