-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Screen.tsx
118 lines (109 loc) · 3.14 KB
/
Screen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
NavigationContext,
type NavigationProp,
NavigationRouteContext,
type ParamListBase,
type RouteProp,
} from '@react-navigation/native';
import * as React from 'react';
import {
Animated,
type StyleProp,
StyleSheet,
View,
type ViewStyle,
} from 'react-native';
import {
useSafeAreaFrame,
useSafeAreaInsets,
} from 'react-native-safe-area-context';
import { Background } from './Background';
import { getDefaultHeaderHeight } from './Header/getDefaultHeaderHeight';
import { HeaderHeightContext } from './Header/HeaderHeightContext';
import { HeaderShownContext } from './Header/HeaderShownContext';
type Props = {
focused: boolean;
modal?: boolean;
navigation: NavigationProp<ParamListBase>;
route: RouteProp<ParamListBase>;
header: React.ReactNode;
headerShown?: boolean;
headerStatusBarHeight?: number;
headerTransparent?: boolean;
style?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
children: React.ReactNode;
};
export function Screen(props: Props) {
const dimensions = useSafeAreaFrame();
const insets = useSafeAreaInsets();
const isParentHeaderShown = React.useContext(HeaderShownContext);
const parentHeaderHeight = React.useContext(HeaderHeightContext);
const {
focused,
modal = false,
header,
headerShown = true,
headerTransparent,
headerStatusBarHeight = isParentHeaderShown ? 0 : insets.top,
navigation,
route,
children,
style,
} = props;
const [headerHeight, setHeaderHeight] = React.useState(() =>
getDefaultHeaderHeight(dimensions, modal, headerStatusBarHeight)
);
return (
<Background
accessibilityElementsHidden={!focused}
importantForAccessibility={focused ? 'auto' : 'no-hide-descendants'}
style={[styles.container, style]}
// On Fabric we need to disable collapsing for the background to ensure
// that we won't render unnecessary views due to the view flattening.
collapsable={false}
>
<View style={styles.content}>
<HeaderShownContext.Provider
value={isParentHeaderShown || headerShown !== false}
>
<HeaderHeightContext.Provider
value={headerShown ? headerHeight : parentHeaderHeight ?? 0}
>
{children}
</HeaderHeightContext.Provider>
</HeaderShownContext.Provider>
</View>
{headerShown ? (
<NavigationContext.Provider value={navigation}>
<NavigationRouteContext.Provider value={route}>
<View
onLayout={(e) => {
const { height } = e.nativeEvent.layout;
setHeaderHeight(height);
}}
style={headerTransparent ? styles.absolute : null}
>
{header}
</View>
</NavigationRouteContext.Provider>
</NavigationContext.Provider>
) : null}
</Background>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column-reverse',
},
// This is necessary to avoid applying 'column-reverse' to screen content
content: {
flex: 1,
},
absolute: {
position: 'absolute',
top: 0,
start: 0,
end: 0,
},
});