Join our Discord Community

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.

Loading component...

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

Loading component...

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

ParameterTypeDefaultDescription
refRefObject<T> | RefObject<T>[]requiredSingle ref or array of refs to monitor
handler(event) => voidrequiredCallback fired when clicking outside element(s)
eventTypeEventType'mousedown'Event type to listen for
eventListenerOptionsAddEventListenerOptions{}Native event listener options

Event Types

TypeDescription
'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

OptionTypeDescription
passivebooleanPassive event listener for performance
capturebooleanCapture events in capture phase
oncebooleanRemove 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