Stop Rebuilding UI

Shadcn.io is not affiliated with official shadcn/ui

React useOnClickOutside Hook

React useOnClickOutside hook for detecting clicks outside elements. Dismiss modals, dropdowns, popovers. TypeScript ready.

Detect clicks outside any element with useOnClickOutside. Pass a ref and handler—fires when user clicks anywhere except inside that element. Supports single ref or array of refs for complex UIs with multiple exclusion zones. Configurable event types: mousedown, mouseup, touchstart, touchend, focusin, focusout. Essential for modals, dropdowns, context menus, search overlays, and any dismissible UI. Ignores clicks on disconnected DOM nodes for edge case safety. SSR safe—event listeners attach client-side only. Works with Next.js App Router, portals, and any React framework.

React useOnClickOutside Hook preview

Scroll to load preview

Installation

FAQ

Was this page helpful?

Sign in to leave feedback.