Skip to content

useMousePosition

This hook tracks the mouse cursor position of a target element.

Parameters

  • the ref object of the target element (default is window).

Return Values

NameTypeDescription
refRefObjectthe target element reference.
xNumberthe x position.
yNumberthe y position.

Example Usage

tsx
import { useMousePosition } from 'react-pre-hooks';

export default function Trailer() {
  const { x: left, y: top } = useMousePosition();

  return (
    <main>
      <div style={{ top, left }}></div>
    </main>
  );
}
import { useMousePosition } from 'react-pre-hooks';

export default function Trailer() {
  const { x: left, y: top } = useMousePosition();

  return (
    <main>
      <div style={{ top, left }}></div>
    </main>
  );
}

Released under the MIT License.