'use client'; 'use strict'; var React = require('react'); function containsRelatedTarget(event) { if (event.currentTarget instanceof HTMLElement && event.relatedTarget instanceof HTMLElement) { return event.currentTarget.contains(event.relatedTarget); } return false; } function useFocusWithin({ onBlur, onFocus } = {}) { const ref = React.useRef(); const [focused, _setFocused] = React.useState(false); const focusedRef = React.useRef(false); const setFocused = (value) => { _setFocused(value); focusedRef.current = value; }; const handleFocusIn = (event) => { if (!focusedRef.current) { setFocused(true); onFocus?.(event); } }; const handleFocusOut = (event) => { if (focusedRef.current && !containsRelatedTarget(event)) { setFocused(false); onBlur?.(event); } }; React.useEffect(() => { if (ref.current) { ref.current.addEventListener("focusin", handleFocusIn); ref.current.addEventListener("focusout", handleFocusOut); return () => { ref.current?.removeEventListener("focusin", handleFocusIn); ref.current?.removeEventListener("focusout", handleFocusOut); }; } return void 0; }, [handleFocusIn, handleFocusOut]); return { ref, focused }; } exports.useFocusWithin = useFocusWithin; //# sourceMappingURL=use-focus-within.cjs.map