import { useEffect } from 'react';

function useOutsideClick(callback, refs) {
  useEffect(function () {
    function handleClick(event) {
      var isInside = refs.some(function (ref) {
        var el = ref.current;
        return el && el.contains(event.target);
      });
      if (!isInside) {
        callback(event);
      }
    }
    document.addEventListener('mousedown', handleClick);
    document.addEventListener('touchstart', handleClick);
    return function () {
      document.removeEventListener('mousedown', handleClick);
      document.removeEventListener('touchstart', handleClick);
    };
  }, [callback]);
}

export { useOutsideClick };
