Stop Rebuilding UI

Shadcn.io is not affiliated with official shadcn/ui

React useMousePosition Hook

React useMousePosition hook for real-time cursor tracking. Global and element-relative coordinates. TypeScript ready.

Track mouse position anywhere on the page or relative to an element. useMousePosition returns x/y coordinates plus a ref for element-relative tracking. Get global page coordinates and optional element-relative positions when you attach the ref. Updates on every mousemove event with page scroll accounted for. Perfect for custom cursors, tooltips that follow mouse, drawing apps, drag previews, or interactive hover effects. Uses useLayoutEffect for synchronous updates. SSR safe—returns zeros on server. Works with Next.js App Router and any React framework.

React useMousePosition Hook preview

Scroll to load preview

Installation

FAQ

Was this page helpful?

Sign in to leave feedback.