UNPKG

@airplane/views

Version:

A React library for building Airplane views. Views components are optimized in style and functionality to produce internal apps that are easy to build and maintain.

26 lines (25 loc) 933 B
import { useRef, useEffect } from "react"; const DEFAULT_EVENTS = ["mousedown", "touchstart"]; function useClickOutside(handler, events) { const ref = useRef(); useEffect(() => { const listener = (event) => { const { target } = event ?? {}; if (ref.current && !ref.current.contains(target) && !(target == null ? void 0 : target.hasAttribute("data-ignore-outside-clicks")) && // We don't want clicking a select item to register as a "click outside" !(target == null ? void 0 : target.classList.contains("airplane-Select-item"))) { handler(); } }; (events || DEFAULT_EVENTS).forEach((fn) => document.addEventListener(fn, listener)); return () => { (events || DEFAULT_EVENTS).forEach((fn) => document.removeEventListener(fn, listener)); }; }, [ref, handler, events]); return ref; } export { useClickOutside }; //# sourceMappingURL=useClickOutside.js.map