shadcn-ahooks

useMouse

Track cursor position

Overview

Track cursor position

Documentation and Examples

Installation

Open in
pnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useMouse.json
npx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useMouse.json
yarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useMouse.json
bun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useMouse.json

Track cursor position

Examples

Default Usage

Mouse position relative to the element

API

const state: {
  screenX: number,
  screenY: number,
  clientX: number,
  clientY: number,
  pageX: number,
  pageY: number,
  elementX: number,
  elementY: number,
  elementH: number,
  elementW: number,
  elementPosX: number,
  elementPosY: number,
} = useMouse(target?: Target);

Params

PropertyDescriptionTypeDefault
targetDOM element or ref() => Element | Element | MutableRefObject<Element>-

result

PropertyDescriptionType
screenXPosition left relative to the top left of the physical screen/monitornumber
screenYPosition top relative to the top left of the physical screen/monitornumber
clientXPosition left relative to the upper left edge of the content areanumber
clientYPosition top relative to the upper left edge of the content areanumber
pageXPosition left relative to the top left of the fully rendered content area in the browsernumber
pageYPosition top relative to the top left of the fully rendered content area in the browsernumber
elementXPosition left relative to the upper left edge of the target elementnumber
elementYPosition top relative to the upper left edge of the target elementnumber
elementHTarget element heightnumber
elementWTarget element widthnumber
elementPosXThe position of the target element left relative to the top left of the fully rendered content area in the browsernumber
elementPosYThe position of the target element top relative to the top left of the fully rendered content area in the browsernumber

On this page