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

NameTypeDescriptionDefault
handlerfunctionCallback invoked on outside click (auto-ref pattern).-
refRefObjectExisting ref to monitor (existing-ref pattern).-
excludeRefsRefObject[]Optional refs to exclude from outside clicks.-

Returns

NameDescription
refReturns 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.
Saha UI - The Modern React Component Library