useOnClickOutside
Handle clicks outside specified elements with support for multiple refs and event types. Perfect for React applications requiring modal and dropdown behavior with Next.js integration and TypeScript support.
Installation
npx shadcn@latest add https://www.shadcn.io/registry/use-on-click-outside.json
npx shadcn@latest add https://www.shadcn.io/registry/use-on-click-outside.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/use-on-click-outside.json
bunx shadcn@latest add https://www.shadcn.io/registry/use-on-click-outside.json
Features
- Multiple element support allowing monitoring of single ref or array of refs
- Flexible event types supporting mouse, touch, and focus events for comprehensive interaction
- TypeScript generics with type-safe element references and event handling
- Event listener options supporting passive listeners and capture phase control
- Performance optimized using useEventListener with proper cleanup and delegation
- shadcn/ui integration ideal for dropdown menus, modals, and popup components
Examples
Multiple Elements with Event Types
Advanced configuration with multiple elements and different event type handling.
Use Cases
This free open source React component works well for:
- Modal dialogs - Close modals when clicking outside with Next.js and TypeScript
- Dropdown menus - Hide dropdowns on outside clicks using shadcn/ui components
- Context menus - Dismiss right-click menus with Tailwind CSS styling
- Tooltip management - Auto-hide tooltips when focus moves elsewhere
API Reference
useOnClickOutside
Parameter | Type | Default | Description |
---|---|---|---|
ref | RefObject<T> | RefObject<T>[] | required | Single ref or array of refs to monitor |
handler | (event) => void | required | Callback fired when clicking outside element(s) |
eventType | EventType | 'mousedown' | Event type to listen for |
eventListenerOptions | AddEventListenerOptions | {} | Native event listener options |
Event Types
Type | Description |
---|---|
'mousedown' | Mouse button pressed (default) |
'mouseup' | Mouse button released |
'touchstart' | Touch interaction begins |
'touchend' | Touch interaction ends |
'focusin' | Element receives focus |
'focusout' | Element loses focus |
Event Listener Options
Option | Type | Description |
---|---|---|
passive | boolean | Passive event listener for performance |
capture | boolean | Capture events in capture phase |
once | boolean | Remove listener after first trigger |
Usage Patterns
Single Element
import { useOnClickOutside } from '@repo/use-on-click-outside';
function Modal({ onClose }) {
const modalRef = useRef(null);
useOnClickOutside(modalRef, onClose);
return <div ref={modalRef}>Modal content</div>;
}
Multiple Elements
const [menu1Ref, menu2Ref] = [useRef(null), useRef(null)];
useOnClickOutside([menu1Ref, menu2Ref], () => {
setMenusOpen(false);
});
Custom Event Type
useOnClickOutside(ref, handler, 'touchstart', { passive: true });
Implementation Notes
- Automatically filters refs with null current values for safety
- Checks event target connectivity to prevent stale reference issues
- Works with both single refs and ref arrays for complex UI patterns
- Compatible with React portals and dynamically rendered content
- Supports all AddEventListenerOptions for fine-tuned event handling
- Integrates seamlessly with useEventListener for consistent behavior