import { useEffect } from 'react';

export const useOnClickOutside = (ref, handler?): void => {
  useEffect(() => {
    const listener = (event): void => {
      if (!ref.current || ref.current.contains(event.target)) {
        return;
      }
      handler(event);
    };
    document.addEventListener('mousedown', listener);
    return (): void => {
      document.removeEventListener('mousedown', listener);
    };
  }, [ref, handler]);
};

export default useOnClickOutside;
