import { Dimensions, Platform } from 'react-native'; import { initialWindowMetrics } from 'react-native-safe-area-context'; import { isNavigationBarTranslucent } from '../nativeModulesHelpers/UNNavigationBarHelper'; const isAndroid = Platform.OS === 'android'; const isAndroid10To14 = isAndroid && Platform.Version >= 29 && Platform.Version < 35; const isAndroid15AndAbove = isAndroid && Platform.Version >= 35; const insets = initialWindowMetrics?.insets ?? { top: 0, bottom: 0, left: 0, right: 0 }; const windowHeight = Dimensions.get('window').height; const screenHeight = Dimensions.get('screen').height; // Detect edge-to-edge on API 35+ by comparing window to screen height const heightDifference = Math.abs(screenHeight - windowHeight); const isEdgeToEdgeActive = isAndroid15AndAbove && heightDifference < 10; // Resolved once at module load time; defaults to false until resolved. // The native call is fast (reads a window flag), so it resolves before // any bottom sheet is opened. let _navBarTranslucent = false; if (isAndroid) { isNavigationBarTranslucent().then((result) => { _navBarTranslucent = result; }); } const calculateFullScreenHeight = (): number => { if (isAndroid15AndAbove) { return isEdgeToEdgeActive ? screenHeight : windowHeight; } else if (isAndroid10To14) { // screenHeight is stable even when translucent status bar alters windowHeight return screenHeight; } return windowHeight; }; const fullScreenHeight = calculateFullScreenHeight(); const calculateContentHeight = (): number => { if (isAndroid15AndAbove && !isEdgeToEdgeActive) { return windowHeight; } if (isAndroid10To14) { return fullScreenHeight - insets.top - insets.bottom; } return fullScreenHeight - insets.top; }; const contentHeight = calculateContentHeight(); export const useSafeAreaDimensions = () => { const shouldApplyBottomInset = isEdgeToEdgeActive || _navBarTranslucent; const effectiveBottomInset = shouldApplyBottomInset ? insets.bottom : 0; return { insets, fullScreenHeight, contentHeight, isAndroid10AndAbove: isAndroid && Platform.Version >= 29, isEdgeToEdgeActive, effectiveBottomInset, }; };