UNPKG

@dailyshot/hooks

Version:

A set of hooks used in Dailyshot packages

27 lines (24 loc) 1.07 kB
import { useRef, useEffect } from 'react'; const DEFAULT_EVENTS = ["mousedown", "touchstart"]; function useClickOutside(handler, events, nodes) { const ref = useRef(); useEffect(() => { const listener = (event) => { const { target } = event != null ? event : {}; if (Array.isArray(nodes)) { const shouldIgnore = (target == null ? void 0 : target.hasAttribute("data-ignore-outside-clicks")) || !document.body.contains(target) && target.tagName !== "HTML"; const shouldTrigger = nodes.every((node) => !!node && !event.composedPath().includes(node)); shouldTrigger && !shouldIgnore && handler(); } else if (ref.current && !ref.current.contains(target)) { handler(); } }; (events || DEFAULT_EVENTS).forEach((fn) => document.addEventListener(fn, listener)); return () => { (events || DEFAULT_EVENTS).forEach((fn) => document.removeEventListener(fn, listener)); }; }, [ref, handler, nodes]); return ref; } export { useClickOutside }; //# sourceMappingURL=use-click-outside.js.map