diff --git a/.eslintrc.json b/.eslintrc.json index d6a48e9..e533e06 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -39,7 +39,7 @@ "react-hooks/exhaustive-deps": [ "warn", { - "additionalHooks": "(useTypedQuery|useTypedParams)" + "additionalHooks": "(useEqualMemo)" } ], "jsx-a11y/label-has-associated-control": 0, diff --git a/src/core/hooks/useOutsideClick.ts b/src/core/hooks/useOutsideClick.ts new file mode 100644 index 0000000..b5f3117 --- /dev/null +++ b/src/core/hooks/useOutsideClick.ts @@ -0,0 +1,25 @@ +import {useCallback, useEffect, useRef} from 'react'; + +export const useOutsideClick = (callback: EventListener) => { + const container = useRef(null); + + const handleEvent = useCallback((e: Event) => { + if (container.current && e.target !== null) { + if (!container.current.contains(e.target as Node)) { + callback(e); + } + } + }, [callback]); + + useEffect(() => { + document.addEventListener('mousedown', handleEvent, true); + document.addEventListener('touchstart', handleEvent, true); + return () => { + document.removeEventListener('mousedown', handleEvent, true); + document.removeEventListener('touchstart', handleEvent, true); + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + return container; +};