import { useRef } from 'react';

function useFocusWithin({
  onBlur,
  onFocus,
  otherRelatedElements
} = {}) {
  const isFocusedRef = useRef(false);
  const handleFocus = (event) => {
    if (!isFocusedRef.current) {
      isFocusedRef.current = true;
      onFocus?.(event);
    }
  };
  const handleBlur = (event) => {
    if (isFocusedRef.current && !containsRelatedTarget(event) && !otherRelatedElements?.some(
      (element) => element.contains(event.relatedTarget)
    )) {
      isFocusedRef.current = false;
      onBlur?.(event);
    }
  };
  return {
    props: { onFocus: handleFocus, onBlur: handleBlur }
  };
}
function containsRelatedTarget(event) {
  if (event.currentTarget instanceof HTMLElement && event.relatedTarget instanceof HTMLElement) {
    return event.currentTarget.contains(event.relatedTarget);
  }
  return false;
}

export { useFocusWithin as u };
//# sourceMappingURL=useFocusWithin-hi77jsrB.js.map
