useMouse
Tracks mouse cursor coordinates across the document, viewport, screen, and an optional target element using requestAnimationFrame.
Loading preview...
The useMouse hook listens to mousemove and dragover events (throttled via requestAnimationFrame) and reports cursor coordinates relative to the document (x, y), viewport (clientX, clientY), screen (screenX, screenY), and an optional target element (elementX, elementY).
Installation
Usage
Examples
Spotlight Effect Relative to an Element
Pass a target element ref to compute elementX and elementY relative to that element's top-left corner.
Global Viewport Cursor Tracking
Call useMouse() without arguments to track global document and viewport coordinates.
API Reference
MousePositionState
Cursor and element coordinate values returned by the hook.
Property
Specification
xnumber
Horizontal coordinate relative to the whole document (
pageX)ynumber
Vertical coordinate relative to the whole document (
pageY)clientXnumber
Horizontal coordinate relative to the viewport (
clientX)clientYnumber
Vertical coordinate relative to the viewport (
clientY)screenXnumber
Horizontal coordinate relative to the user's screen (
screenX)screenYnumber
Vertical coordinate relative to the user's screen (
screenY)elementXnumber
Horizontal coordinate relative to the target element
elementYnumber
Vertical coordinate relative to the target element
elementPositionXnumber
Target element's horizontal document offset
elementPositionYnumber
Target element's vertical document offset