This repository was archived by the owner on Dec 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathHeader.js
More file actions
145 lines (131 loc) · 3.72 KB
/
Header.js
File metadata and controls
145 lines (131 loc) · 3.72 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React from 'react';
import {
Animated,
Platform,
StatusBar,
StyleSheet,
View,
} from 'react-native';
import { HeaderBackButton } from '@react-navigation/elements';
import { getStatusBarHeight } from 'react-native-safe-area-view';
import { isIphoneX } from 'react-native-iphone-x-helper';
import { useNavigation } from '@react-navigation/native';
// @todo: make this work properly when in landscape
const hasNotch = isIphoneX();
const APPBAR_HEIGHT = Platform.OS === 'ios' ? 50 : 56;
const TITLE_OFFSET = Platform.OS === 'ios' ? 70 : 56;
class Header extends React.PureComponent {
constructor(props) {
super(props);
// @todo: this is static and we don't know if it's visible or not on iOS.
// need to use a more reliable and cross-platform API when one exists, like
// LayoutContext. We also don't know if it's translucent or not on Android
// and depend on react-native-safe-area-view to tell us.
const ANDROID_STATUS_BAR_HEIGHT = getStatusBarHeight
? getStatusBarHeight()
: StatusBar.currentHeight;
const STATUSBAR_HEIGHT =
Platform.OS === 'ios' ? (hasNotch ? 40 : 25) : ANDROID_STATUS_BAR_HEIGHT;
let platformContainerStyles;
if (Platform.OS === 'ios') {
platformContainerStyles = {
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: '#A7A7AA',
};
} else {
platformContainerStyles = {
shadowColor: 'black',
shadowOpacity: 0.1,
shadowRadius: StyleSheet.hairlineWidth,
shadowOffset: {
height: StyleSheet.hairlineWidth,
},
elevation: 4,
};
}
this.styles = {
container: {
backgroundColor: '#fff',
paddingTop: STATUSBAR_HEIGHT,
height: STATUSBAR_HEIGHT + APPBAR_HEIGHT,
...platformContainerStyles,
},
appBar: {
flex: 1,
},
header: {
flexDirection: 'row',
},
item: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'transparent',
},
title: {
bottom: 0,
left: TITLE_OFFSET,
right: TITLE_OFFSET,
top: 0,
position: 'absolute',
alignItems: Platform.OS === 'ios' ? 'center' : 'flex-start',
},
left: {
left: 0,
bottom: 0,
top: 0,
position: 'absolute',
},
right: {
right: 0,
bottom: 0,
top: 0,
position: 'absolute',
},
};
}
_navigateBack = () => {
if (this.props.onCancelPress) {
this.props.onCancelPress(this.props.navigation.goBack);
} else {
this.props.navigation.goBack();
}
};
_maybeRenderBackButton = () => {
if (!this.props.backButton) {
return;
}
return (
<HeaderBackButton
onPress={this._navigateBack}
pressColorAndroid={this.props.tintColor || '#fff'}
tintColor={this.props.tintColor}
title={this.props.backButtonTitle || null}
truncatedTitle={this.props.backButtonTruncatedTitle || null}
titleStyle={this.props.backButtonTitleStyle || null}
/>
);
};
render() {
let { styles } = this;
let headerStyle = {};
if (this.props.backgroundColor) {
headerStyle.backgroundColor = this.props.backgroundColor;
}
return (
<Animated.View style={[styles.container, headerStyle]}>
<View style={styles.appBar}>
<View style={[StyleSheet.absoluteFill, styles.header]}>
{this._maybeRenderBackButton()}
{this.props.children}
</View>
</View>
</Animated.View>
);
}
}
export default function (props) {
const navigation = useNavigation();
return (
<Header {...props} navigation={navigation} />
);
}