fix: drop usage of Dimensions in favor of metrics from safe-area-context

This commit is contained in:
Satyajit Sahoo
2020-12-03 01:40:00 +01:00
parent 73aad00f19
commit 12b893d7ca
9 changed files with 85 additions and 164 deletions

View File

@@ -1,5 +1,5 @@
import * as React from 'react';
import { Platform } from 'react-native';
import { Dimensions, Platform } from 'react-native';
import {
SafeAreaProvider,
SafeAreaInsetsContext,
@@ -10,13 +10,15 @@ type Props = {
children: React.ReactNode;
};
const frame = Dimensions.get('window');
// To support SSR on web, we need to have empty insets for initial values
// Otherwise there can be mismatch between SSR and client output
// We also need to specify empty values to support tests environments
const initialMetrics =
export const initialMetrics =
Platform.OS === 'web' || initialWindowMetrics == null
? {
frame: { x: 0, y: 0, width: 0, height: 0 },
frame: { x: 0, y: 0, width: frame.width, height: frame.height },
insets: { top: 0, left: 0, right: 0, bottom: 0 },
}
: initialWindowMetrics;

View File

@@ -3,7 +3,6 @@ import {
Animated,
StyleSheet,
LayoutChangeEvent,
Dimensions,
Platform,
} from 'react-native';
import type { EdgeInsets } from 'react-native-safe-area-context';
@@ -18,6 +17,7 @@ import {
MaybeScreen,
shouldUseActivityState,
} from '../Screens';
import { initialMetrics } from '../SafeAreaProviderCompat';
import { getDefaultHeaderHeight } from '../Header/HeaderSegment';
import type { Props as HeaderContainerProps } from '../Header/HeaderContainer';
import CardContainer from './CardContainer';
@@ -286,13 +286,11 @@ export default class CardStack extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
const { height = 0, width = 0 } = Dimensions.get('window');
this.state = {
routes: [],
scenes: [],
gestures: {},
layout: { height, width },
layout: initialMetrics.frame,
descriptors: this.props.descriptors,
// Used when card's header is null and mode is float to make transition
// between screens with headers and those without headers smooth.