use-click-outside-react
v1.0.0
Published
Production-ready React 18 & 19 hook to detect clicks and touches outside target element(s) with Escape key support.
Maintainers
Readme
🎯 use-click-outside-react
Production-ready, zero-dependency React 18 & 19 hook to detect clicks and touch gestures outside element(s) with Escape key and multi-ref support.
✨ Features
- 🖱️ Pointer & Touch Support — Listens to both
mousedownandtouchstartfor seamless desktop and mobile interactions. - 📚 Multi-Ref Support — Supply an array of refs (e.g. dropdown toggle button + floating menu container) to prevent accidental closes.
- ⌨️ Escape Key Handling — Built-in
closeOnEscape: trueoption for accessible modals, popovers, and drawers. - ⚡ Zero Dependencies — Minimal bundle footprint (< 1 KB minified).
- 🔄 Stale Closure Proof — Uses
useRefinternally so callbacks always read fresh state without re-attaching listeners. - 📦 Dual ESM & CommonJS — Works in Next.js (App & Pages Router), Vite, Remix, and React Native Web.
📥 Installation
# npm
npm install use-click-outside-react
# pnpm
pnpm add use-click-outside-react
# yarn
yarn add use-click-outside-react
# bun
bun add use-click-outside-react🚀 Usage & Real-World Examples
1. Dropdown Menu
import React, { useState, useRef } from 'react';
import { useClickOutside } from 'use-click-outside-react';
export function Dropdown() {
const [isOpen, setIsOpen] = useState(false);
const menuRef = useRef<HTMLDivElement>(null);
// Close dropdown whenever clicking outside:
useClickOutside(menuRef, () => setIsOpen(false), { enabled: isOpen });
return (
<div className="relative">
<button onClick={() => setIsOpen((prev) => !prev)}>Toggle Menu</button>
{isOpen && (
<div ref={menuRef} className="dropdown-panel">
<ul>
<li>Profile Settings</li>
<li>Billing</li>
<li>Logout</li>
</ul>
</div>
)}
</div>
);
}2. Modal Dialog with Escape Key
import React, { useRef } from 'react';
import { useClickOutside } from 'use-click-outside-react';
interface ModalProps {
isOpen: boolean;
onClose: () => void;
children: React.ReactNode;
}
export function Modal({ isOpen, onClose, children }: ModalProps) {
const dialogRef = useRef<HTMLDivElement>(null);
// Closes when clicking backdrop OR pressing Escape key!
useClickOutside(dialogRef, onClose, {
enabled: isOpen,
closeOnEscape: true,
});
if (!isOpen) return null;
return (
<div className="modal-backdrop">
<div ref={dialogRef} className="modal-card">
{children}
<button onClick={onClose}>Close</button>
</div>
</div>
);
}3. Multi-Ref Handling (Trigger + Floating Popover)
When your trigger button and popover live in different parts of the DOM:
import React, { useState, useRef } from 'react';
import { useClickOutside } from 'use-click-outside-react';
export function Popover() {
const [open, setOpen] = useState(false);
const buttonRef = useRef<HTMLButtonElement>(null);
const popoverRef = useRef<HTMLDivElement>(null);
// Ignore clicks inside BOTH the button and the popover:
useClickOutside([buttonRef, popoverRef], () => setOpen(false), {
enabled: open,
closeOnEscape: true,
});
return (
<>
<button ref={buttonRef} onClick={() => setOpen((o) => !o)}>
Open Popover
</button>
{open && (
<div ref={popoverRef} className="popover-content">
<p>Helpful info tooltip!</p>
</div>
)}
</>
);
}📖 API Reference
useClickOutside(refOrRefs, handler, options?)
| Parameter | Type | Default | Description |
|---|---|---|---|
| refOrRefs | RefObject \| RefObject[] | Required | Element ref or array of refs to treat as "inside" |
| handler | (event) => void | Required | Callback when an outside interaction or escape occurs |
| options | ClickOutsideOptions | {} | Configuration options |
ClickOutsideOptions:
| Option | Type | Default | Description |
|---|---|---|---|
| enabled | boolean | true | Enables or disables event listeners dynamically |
| events | EventType[] | ['mousedown', 'touchstart'] | DOM pointer events to listen to |
| closeOnEscape | boolean | false | If true, fires handler on Escape key press |
💖 Support the Project
If this hook made your UI components cleaner, please consider sponsoring:
👉 Sponsor @Piratha97 on GitHub Sponsors
📄 License
MIT © 2026 Pirapaakaran Pirathapan
