        } else if (!ref.current.contains(target)) {
          callback(event);
        }
      }
    };

    window.addEventListener('mousedown', listener);
    document.addEventListener(`touchstart`, listener);

    return () => {
      window.removeEventListener('mousedown', listener);
      document.removeEventListener(`touchstart`, listener);
    };
  }, [callback, ref, children]);