The useMousePosition hook provides real-time tracking of the mouse position, either globally or relative to a specific HTML element. This is useful for applications needing precise cursor positioning, such as custom drag-and-drop interfaces, canvas drawing, or interactive visualizations.
Name | Type | Default | Description |
---|---|---|---|
element | React.RefObject<HTMLElement> | string | - | An optional reference or ID of the HTML element to calculate mouse position relative to. If not provided, defaults to the window. |
Name | Type | Description |
---|---|---|
mousePosition | { x: number , y: number} | An object containing the current mouse coordinates { x, y }. |
Usage
Move the mouse over the element to see the mouse position RELATIVE to the element
Move the mouse over the element to see the mouse position RELATIVE to the element