useMove
The useMove
hook listens to pointer movements(without needing a press or drag) and provides real-time feedback like movement delta, velocity, and cursor offset. It supports both HTML elements and the window object.
Syntax
useMove(
refs: RefObject<HTMLElement> | RefObject<HTMLElement>[] | window,
callback: (event: MoveEvent) => void
): void
Parameters
Name | Type | Description |
---|---|---|
Refs (required) | RefObject<HTMLElement> or RefObject<HTMLElement>[] or Window | Element(s) or window to track pointer movement over. |
Callback (required) | (event:MoveEvent) => void | Called on every move event and when the pointer leaves. |
MouseEvent Signature
type MoveEvent = {
index: number;
movement: { x: number; y: number }; // Movement from start of gesture
offset: { x: number; y: number }; // Pointer offset inside element
velocity: { x: number; y: number }; // px/ms
event: PointerEvent; // Native pointer event
cancel?: () => void; // Cancels the gesture (optional)
};
Examples
1. Track Pointer Across Entire Window
This example creates a custom teal-colored cursor that follows the pointer anywhere on the page.
2. Track Pointer Within a Specific Element
This example tracks pointer movements only inside a target element (the gray box).
The teal "cursor" still moves globally but updates its position only when the pointer is inside the target box.
3. Tracking Pointer Movement Across Multiple Elements
This example demonstrates how to use useMove with multiple DOM elements (using refs) to track pointer movements. It updates the position of a floating teal circle based on pointer events inside any of the target boxes.