Stop Rebuilding UI

Shadcn.io is not affiliated with official shadcn/ui

React useClickAnywhere Hook

React useClickAnywhere hook to detect clicks anywhere on the document. TypeScript, SSR safe, auto-cleanup.

Detect clicks outside your component or anywhere on the page. useClickAnywhere attaches a single document-level listener that fires your callback on every click. Handler ref is stable—no stale closures. Cleans up automatically on unmount. SSR safe with window check. Use it for closing dropdowns, dismissing modals, tracking user activity, or global click analytics.

React useClickAnywhere Hook preview

Scroll to load preview

Installation

FAQ

Was this page helpful?

Sign in to leave feedback.