Overview
useMouse tracks pointer coordinates for a target element and returns a ref to bind that element. Coordinates are reported relative to the element when it is mounted.
This package also exports useMousePosition which tracks global mouse coordinates on the document.
Installation
Import
import { useMouse, useMousePosition } from "@kuzenbo/hooks";
Usage
Basic Example
import { useMouse } from "@kuzenbo/hooks";
export function MouseTracker() {
const { ref, x, y } = useMouse();
return (
<div
ref={ref}
className="relative h-64 w-full bg-muted rounded-lg"
>
<div className="p-4">
<p className="text-sm text-foreground">
Mouse position: x={x}, y={y}
</p>
</div>
</div>
);
}
Reset on Exit
import { useMouse } from "@kuzenbo/hooks";
export function MouseTrackerWithReset() {
const { ref, x, y } = useMouse({ resetOnExit: true });
return (
<div
ref={ref}
className="relative h-64 w-full bg-muted rounded-lg"
>
<div className="p-4">
<p className="text-sm text-foreground">
Position: ({x}, {y})
</p>
<p className="text-xs text-muted-foreground">
Coordinates reset to (0, 0) when mouse leaves
</p>
</div>
</div>
);
}
Global Mouse Position
import { useMousePosition } from "@kuzenbo/hooks";
export function GlobalMouseTracker() {
const { x, y } = useMousePosition();
return (
<div className="p-4">
<p className="text-sm text-foreground">
Global mouse position: x={x}, y={y}
</p>
</div>
);
}
API Reference
useMouse
function useMouse<T extends HTMLElement = HTMLElement>(
options?: { resetOnExit?: boolean }
): UseMouseReturnValue<T>
Optional configuration objectResets coordinates to { x: 0, y: 0 } when the pointer leaves the element
Ref callback to attach to the target element
Horizontal coordinate relative to the element
Vertical coordinate relative to the element
useMousePosition
function useMousePosition(): UseMousePositionReturnValue
Global horizontal coordinate (clientX)
Global vertical coordinate (clientY)
Type Definitions
interface UseMouseReturnValue<T extends HTMLElement = HTMLElement> {
ref: RefCallback<T | null>;
x: number;
y: number;
}
interface UseMousePositionReturnValue {
x: number;
y: number;
}
Caveats
- Coordinates are relative to the element’s bounding box, not the viewport
- Coordinates are clamped to minimum value of 0
- The hook uses
mousemove events for tracking
SSR and RSC Notes
- Use this hook in Client Components only
- Do not call it from React Server Components