DOM & Events
useClickOutside
Detect clicks outside of a specific element for closing menus and modals.
import { useClickOutside } from "saha-ui"Uses
Detect clicks / touches outside a target element to close popovers, menus or overlays. Supports two usage patterns: returns a ref or accepts a ref.
Props
| Name | Type | Description | Default |
|---|---|---|---|
| handler | function | Callback invoked on outside click (auto-ref pattern). | - |
| ref | RefObject | Existing ref to monitor (existing-ref pattern). | - |
| excludeRefs | RefObject[] | Optional refs to exclude from outside clicks. | - |
Returns
| Name | Description |
|---|---|
| ref | Returns a ref in the non-ref pattern; nothing in the ref-pattern. |
Example
// auto-ref pattern const ref = useClickOutside(() => setOpen(false)); // existing-ref pattern useClickOutside(menuRef, () => setOpen(false));
Notes
Listens to `mousedown` and `touchstart`; excludes provided refs when checking containment.