Skip to content

useMouse

Tracks the current mouse position in relation to both the window and a provided element ref.

Usage

import { useRef } from "react";
import { useMouse } from "@/hooks/sensors/use-mouse";
const Component = () => {
const ref = useRef<HTMLDivElement>(null);
const { x, y, elementX, elementY, elementPositionX, elementPositionY } =
useMouse(ref);
return (
<div ref={ref}>
<p>Mouse X: {x}</p>
<p>Mouse Y: {y}</p>
<p>Element Mouse X: {elementX}</p>
<p>Element Mouse Y: {elementY}</p>
<p>Element Position X: {elementPositionX}</p>
<p>Element Position Y: {elementPositionY}</p>
</div>
);
};

Installation

Terminal window
npx atmx add hook use-mouse