Skip to Content
ohooks

useOnClickOutside

Invokes a handler when a pointer, mouse, or touch interaction occurs outside one or more target elements (with Shadow DOM composedPath support).

Loading preview...

The useOnClickOutside hook detects clicks, taps, or pointer presses outside a target element (or an array of elements) using event.composedPath() for accurate Shadow DOM and portal handling.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useOnClickOutside.json

Usage

Examples

Closing a Popover with a Separate Trigger Button

Pass an array of refs so clicking either the popover content or its trigger button does not count as an outside click.

Customizing the Event Type and Enabling Conditionally

Use eventType to listen on mouseup or click instead of pointerdown, and disable the listener when the overlay is closed.

API Reference

UseOnClickOutsideOptions

Configuration options for the outside interaction listener.

Property
Specification
enabled?
uniondefault:true
Whether the outside click listener is active
events?
arraydefault:['pointerdown']
DOM events to listen for on document
capture?
uniondefault:true
Listen during the capture phase