-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
132 lines (110 loc) · 3.99 KB
/
index.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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {Animated, ScrollView, StyleSheet, View, Dimensions, Text, PanResponder} from 'react-native';
const {width} = Dimensions.get('window')
const HEADER_MAX_HEIGHT = 199;
class CollapsingHeader extends Component {
constructor(props) {
super(props);
// scrolling distance
this.HEADER_SCROLL_DISTANCE = props.headerMaxHeight - props.headerMinHeight
this.state = {
scrollY: new Animated.Value(0),
showScrollShadow: false
}
}
onScroll = (event) => {
const { showScrollShadow } = this.state;
const { y } = event.nativeEvent.contentOffset;
this.props.scrollCallback && this.props.scrollCallback(event);
if (y > 1 && !showScrollShadow)
this.setState({ showScrollShadow: true });
else if (y <= 1 && showScrollShadow)
this.setState({ showScrollShadow: false });
}
render() {
const headerPosition = this.state.scrollY.interpolate({
inputRange: [0, this.HEADER_SCROLL_DISTANCE],
outputRange: [0, - this.HEADER_SCROLL_DISTANCE],
extrapolate: 'clamp' // clamp so translateY can’t go beyond -160
})
const opacity = this.state.scrollY.interpolate({
inputRange: [0, this.HEADER_SCROLL_DISTANCE],
outputRange: [1, 0],
extrapolate: 'clamp' // clamp so translateY can’t go beyond -160
})
return (
<View style={[styles.container]}>
<Animated.View style={[styles.header,{height: this.props.headerMaxHeight, shadowOpacity: this.state.showScrollShadow ? 1 : 0, elevation: this.state.showScrollShadow ? 7 : 0},
{transform: [{
translateY: headerPosition
}]
}]} >
{this.props.header(opacity)}
</Animated.View>
<Animated.ScrollView
ref={(ref)=>{
if(ref && this.props.scrollViewRef) {
this.props.scrollViewRef(ref)
}
}}
contentContainerStyle={[{paddingTop: this.props.headerMaxHeight},this.props.contentContainerStyle]}
scrollIndicatorInsets={{
top: this.props.headerMaxHeight
}}
scrollEventThrottle={this.props.scrollEventThrottle}
bounces={false}
style={{zIndex: -1}}
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }],
{
useNativeDriver: true,
listener: this.onScroll
},
)}
>
{this.props.children}
</Animated.ScrollView>
</View>)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
zIndex: 1
},
header: {
position: 'absolute',
backgroundColor: 'white',
top: 0,
left: 0,
right: 0,
height: HEADER_MAX_HEIGHT,
width,
shadowColor: "rgba(1, 12, 39, 0.2)",
shadowOffset: {
width: 0,
height: 0
},
shadowRadius: 4,
// shadowOpacity: 1,
// elevation: 7
},
contentContainer: {
width,
marginTop: HEADER_MAX_HEIGHT
},
});
CollapsingHeader.propTypes = {
header: PropTypes.any,
headerMaxHeight: PropTypes.number,
headerMinHeight: PropTypes.number,
scrollEventThrottle: PropTypes.number
}
CollapsingHeader.defaultProps = {
headerMaxHeight: 199,
headerMinHeight: 117,
scrollEventThrottle: 16
}
export default CollapsingHeader;