import { useCallback, useEffect, useRef } from "react";
export const useCloseOnOutsideClick = (onClose) => {
    const ref = useRef(null);
    const escapeListener = useCallback((e) => {
        if (e.key === "Escape") {
            onClose();
        }
    }, [onClose]);
    const clickListener = useCallback((e) => {
        if (ref.current && !ref.current.contains(e.target)) {
            if (onClose && typeof onClose === "function") {
                onClose();
            }
        }
    }, [onClose]);
    useEffect(() => {
        document.addEventListener("click", clickListener);
        document.addEventListener("keyup", escapeListener);
        return () => {
            document.removeEventListener("click", clickListener);
            document.removeEventListener("keyup", escapeListener);
        };
    }, [clickListener, escapeListener]);
    return ref;
};
//# sourceMappingURL=use-click-outside.js.map