useClickOutside
Detects clicks outside an element and fires a callback. Useful for closing menus, dropdowns, modals and popovers when the user interacts with any area outside the component.
The hook listens for mousedown in the capture phase on document and fires
the callback whenever the event target is not contained in the referenced
element.
When to use
| ✅ Use when… | 🚫 Avoid when… |
|---|---|
|
|
Signature
const ref = useClickOutside<T extends HTMLElement>(callback: () => void);Example
import { useClickOutside } from '@apollion-dsi/core/hooks';
function Dropdown({ onClose }: { onClose: () => void }) {
const ref = useClickOutside<HTMLDivElement>(onClose);
return (
<div ref={ref}>
<p>menu content</p>
</div>
);
}See also
- Full API:
useClickOutside(reference generated from TSDoc).