UNPKG

@gluestack-ui/core

Version:

Universal UI components for React Native, Expo, and Next.js

38 lines 1.17 kB
import { useEffect } from 'react'; import { useFocusWithin } from '@react-aria/interactions'; const visibleOverlays = []; export function useOverlay(props, ref) { let { onClose, shouldCloseOnBlur, isOpen, isKeyboardDismissDisabled = false, } = props; useEffect(() => { if (isOpen) { visibleOverlays.push(ref); } return () => { let index = visibleOverlays.indexOf(ref); if (index >= 0) { visibleOverlays.splice(index, 1); } }; }, [isOpen, ref]); let onHide = () => { if (visibleOverlays[visibleOverlays.length - 1] === ref && onClose) { onClose(); } }; let onKeyDown = (e) => { if (e.key === 'Escape' && !isKeyboardDismissDisabled) { e.preventDefault(); onHide(); } }; let { focusWithinProps } = useFocusWithin({ isDisabled: !shouldCloseOnBlur, onBlurWithin: () => { onClose && onClose(); }, }); return { overlayProps: Object.assign({ onKeyDown }, focusWithinProps), }; } //# sourceMappingURL=useOverlay.web.js.map