import { useEffect, useRef } from 'react';

export const useClickOutside = (handleClickOutside?: any) => {
  const ref = useRef<any>(null);

  useEffect(()=> {
    const handleClick = ({ target }: any) => {
      if (ref.current && !ref.current.contains(target)) {
        handleClickOutside && handleClickOutside();
      }
    };

    document.addEventListener('click', handleClick, false);

    return () => {
      document.removeEventListener('click', handleClick, false);
    };
  }, [handleClickOutside]);

  return ref;
};
