react-native-unit-components
Version:
Unit React Native components
46 lines (45 loc) • 1.65 kB
JavaScript
import { Dimensions, Platform } from 'react-native';
import { initialWindowMetrics } from 'react-native-safe-area-context';
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 by comparing window to screen height
// Edge-to-edge ON: windowHeight ≈ screenHeight | OFF: windowHeight < screenHeight
const heightDifference = Math.abs(screenHeight - windowHeight);
const isEdgeToEdgeActive = isAndroid15AndAbove && heightDifference < 10;
const effectiveBottomInset = isEdgeToEdgeActive ? insets.bottom : 0;
const calculateFullScreenHeight = () => {
if (isAndroid15AndAbove) {
return isEdgeToEdgeActive ? screenHeight : windowHeight;
} else if (isAndroid10To14) {
return windowHeight + insets.top;
}
return windowHeight;
};
const fullScreenHeight = calculateFullScreenHeight();
const calculateContentHeight = () => {
if (isAndroid15AndAbove && !isEdgeToEdgeActive) {
return windowHeight;
}
return fullScreenHeight - insets.top;
};
const contentHeight = calculateContentHeight();
export const useSafeAreaDimensions = () => {
return {
insets,
fullScreenHeight,
contentHeight,
isAndroid10AndAbove: isAndroid && Platform.Version >= 29,
isEdgeToEdgeActive,
effectiveBottomInset
};
};
//# sourceMappingURL=useSafeAreaDimensions.js.map