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
134 lines (123 loc) · 3.51 KB
/
Header.js
File metadata and controls
134 lines (123 loc) · 3.51 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
import React from 'react';
import {
Animated,
Dimensions,
Platform,
StatusBar,
StyleSheet,
View,
} from 'react-native';
import { withNavigation } from 'react-navigation';
import { HeaderBackButton } from 'react-navigation-stack';
import { getInset, getStatusBarHeight } from 'react-native-safe-area-view';
import { isIphoneX } from 'react-native-iphone-x-helper';
// @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;
@withNavigation
export default 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 = () => {
this.props.navigation.goBack(null);
};
_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>
);
}
}