Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

636 B
Raw Blame
import type { RefObject } from "react";
import { useEffect } from "react";

/**
 * Calls `onClose` when a click occurs outside the referenced element while `active` is true.
 */
export function useClickOutside(
  ref: RefObject<HTMLElement | null>,
  active: boolean,
  onClose: () => void
) {
  useEffect(() => {
    if (!active) return;
    const handler = (e: MouseEvent) => {
      if (ref.current && !ref.current.contains(e.target as Node)) {
        onClose();
      }
    };
    document.addEventListener("mousedown", handler);
    return () => document.removeEventListener("mousedown", handler);
  }, [ref, active, onClose]);
}