use-on-outside-click
Version:
A lightweight and flexible React hook to detect clicks outside one or multiple elements.
75 lines (53 loc) • 1.62 kB
Markdown
A lightweight and flexible React hook to detect clicks outside one or multiple elements.
- Supports **multiple elements** (Refs, DOM nodes, or CSS selectors)
- **Skip option** to temporarily disable the listener
- Automatically cleans up listeners on unmount
- Works with **React 18+**
- TypeScript support
```bash
npm install use-on-outside-click
yarn add use-on-outside-click
```
```tsx
import { useRef } from "react";
import useOnOutsideClick from "use-on-outside-click";
export default function Example() {
const ref1 = useRef<HTMLDivElement>(null);
const ref2 = useRef<HTMLDivElement>(null);
useOnOutsideClick([ref1, ref2, "#modal"], () => {
console.log("Clicked outside all elements!");
});
return (
<>
<div ref={ref1}>Box 1</div>
<div ref={ref2}>Box 2</div>
<div id="modal">Modal</div>
</>
);
}
```
| Option | Type | Default | Description |
| ------ | --------- | ------- | -------------------------------- |
| `skip` | `boolean` | `false` | Disable the listener temporarily |
```ts
type ElementInput = RefObject<HTMLElement | null> | HTMLElement | string | null;
type ElementList = ElementInput | ElementInput[];
type Options = {
skip?: boolean;
};
```
```tsx
useOnOutsideClick(ref, callback, { skip: boolean });
```
The hook automatically removes the event listener when the component unmounts.
---
Made with ❤️ for React developers.