forked from maphongba008/react-native-animated-header
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeader.js
207 lines (187 loc) · 5.88 KB
/
Header.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import React from 'react';
import { Animated, Platform, StyleSheet, View, TouchableOpacity, Dimensions } from 'react-native';
const ios = Platform.OS === 'ios';
const {width, height} = Dimensions.get('window');
// from native-base
const isIphoneX = ios && (height === 812 || width === 812);
const iphoneXTopInset = 24;
const initToolbarHeight = ios ? 46 : 56;
const paddingTop = ios ? 18 : 0;
const topInset = isIphoneX ? iphoneXTopInset : 0;
const toolbarHeight = initToolbarHeight + topInset + paddingTop;
export default class Header extends React.PureComponent {
constructor(props) {
super(props);
this.headerHeight = props.headerMaxHeight;
this.state = {
scrollOffset: new Animated.Value(0),
left: 0,
bottom: 0,
};
}
onScroll = e => {
if (this.props.disabled) {
return;
}
this.state.scrollOffset.setValue(e.nativeEvent.contentOffset.y);
};
onBackLayout = (e) => {
const layout = e.nativeEvent.layout;
const bottom = toolbarHeight - layout.y - layout.height - paddingTop - topInset;
this.setState({bottom: bottom, left: e.nativeEvent.layout.x})
}
_getFontSize = () => {
const { scrollOffset } = this.state;
const backFontSize = this.props.backTextStyle.fontSize || Header.defaultProps.backTextStyle.fontSize;
const titleFontSize = this.props.titleStyle.fontSize || Header.defaultProps.titleStyle.fontSize;
return scrollOffset.interpolate({
inputRange: [0, this.headerHeight - toolbarHeight],
outputRange: [titleFontSize, backFontSize],
extrapolate: 'clamp',
});
}
_getLeft = () => {
const { scrollOffset } = this.state;
const left = this.props.titleStyle.left || Header.defaultProps.titleStyle.left;
return scrollOffset.interpolate({
inputRange: [0, this.headerHeight - toolbarHeight],
outputRange: [left, this.state.left],
extrapolate: 'clamp',
});
}
_getHeight = () => {
const { scrollOffset } = this.state;
return scrollOffset.interpolate({
inputRange: [0, this.headerHeight - toolbarHeight],
outputRange: [this.headerHeight, toolbarHeight],
extrapolate: 'clamp',
})
}
_getBottom = () => {
const { scrollOffset } = this.state;
const bottom = this.props.titleStyle.bottom || Header.defaultProps.titleStyle.bottom;
return scrollOffset.interpolate({
inputRange: [0, this.headerHeight - toolbarHeight],
outputRange: [bottom, this.state.bottom],
extrapolate: 'clamp',
});
}
_getOpacity = () => {
const { scrollOffset } = this.state;
return this.props.backText ? scrollOffset.interpolate({
inputRange: [0, this.headerHeight - toolbarHeight],
outputRange: [1, 0],
extrapolate: 'clamp',
}) : 0
}
_getImageOpacity = () => {
const { scrollOffset } = this.state;
return this.props.imageSource ? scrollOffset.interpolate({
inputRange: [0, this.headerHeight - toolbarHeight],
outputRange: [1, 0],
extrapolate: 'clamp',
}) : 0
}
_getImageScaleStyle = () => {
if (!this.props.parallax) {
return undefined;
}
const { scrollOffset } = this.state;
const scale = scrollOffset.interpolate({
inputRange: [-100, -0],
outputRange: [1.5, 1],
extrapolate: 'clamp',
})
return {
transform: [
{
scale,
}
]
}
}
render() {
const { imageSource, toolbarColor, titleStyle, onBackPress, backStyle, backTextStyle } = this.props;
const height = this._getHeight();
const left = this._getLeft();
const bottom = this._getBottom();
const opacity = this._getOpacity();
const fontSize = this._getFontSize();
const imageOpacity = this._getImageOpacity();
const headerStyle = this.props.noBorder ? undefined : { borderBottomWidth: 1, borderColor: '#a7a6ab'}
return (
<Animated.View
style={[
styles.header,
headerStyle,
{
height: height,
backgroundColor: toolbarColor,
},
]}>
{imageSource && <Animated.Image
style={[StyleSheet.absoluteFill, {width: null, height: null, opacity: imageOpacity}, this._getImageScaleStyle()]}
source={imageSource}
resizeMode='cover'
/>}
<View style={styles.toolbarContainer}>
<View style={styles.statusBar} />
<View style={styles.toolbar}>
{this.props.renderLeft && this.props.renderLeft()}
<TouchableOpacity disabled={!onBackPress} onPress={onBackPress} activeOpacity={0.8} style={[styles.titleButton, backStyle]} onLayout={this.onBackLayout}>
<Animated.Text style={[backTextStyle, { alignSelf: 'center', opacity: opacity }]}>{this.props.backText || 'Back2'}</Animated.Text>
</TouchableOpacity>
<View style={styles.flexView} />
{this.props.renderRight && this.props.renderRight()}
</View>
</View>
<Animated.Text style={[titleStyle, {
position: 'absolute',
left: left,
bottom: bottom,
fontSize,
}]}>
{this.props.title}
</Animated.Text>
</Animated.View>
);
}
}
const styles = StyleSheet.create({
toolbarContainer: {
height: toolbarHeight
},
statusBar: {
height: topInset + paddingTop
},
toolbar: {
flex: 1,
flexDirection: 'row',
alignItems: 'center'
},
header: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
},
titleButton: {
flexDirection: 'row',
},
flexView: {
flex: 1,
},
});
Header.defaultProps = {
backText: '',
title: '',
renderLeft: undefined,
renderRight: undefined,
backStyle: { marginLeft: 10 },
backTextStyle: { fontSize: 16 },
titleStyle: { fontSize: 20, left: 40, bottom: 30 },
toolbarColor: '#FFF',
headerMaxHeight: 200,
disabled: false,
imageSource: undefined,
}