-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
204 lines (190 loc) · 6.89 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
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
const React = require('react-native');
const {
Animated,
Component,
Dimensions,
ListView,
StyleSheet,
View
} = React;
const {
any,
bool,
func,
number,
string
} = React.PropTypes;
// Properties accepted by `ParallaxListView`.
const IPropTypes = {
parallaxHeaderHeight: number.isRequired,
renderParallaxHeader: func.isRequired,
rowHeight: number,
backgroundColor: string,
renderStickyHeader: func,
renderFixedHeader: func,
renderBackground: func,
stickyHeaderHeight: number,
shouldPadBottom: bool,
style: any
};
class ParallaxListView extends Component {
constructor(props) {
super(props);
if (props.renderStickyHeader && !props.stickyHeaderHeight) {
console.error('Property `stickyHeaderHeight` must be set if `renderStickyHeader` is used')
}
this.state = { scrollY: new Animated.Value(0) };
this._animatedEvent = Animated.event([{nativeEvent: { contentOffset: { y: this.state.scrollY } } }]);
}
render() {
const { scrollY } = this.state;
const {
backgroundColor,
dataSource,
parallaxHeaderHeight,
stickyHeaderHeight,
renderBackground,
renderFixedHeader,
renderParallaxHeader,
renderStickyHeader,
rowHeight,
onScroll: prevOnScroll = () => {},
style,
...listViewProps
} = this.props;
return (
<View style={styles.container}>
<Animated.View style={[styles.backgroundImage, {
backgroundColor,
height: parallaxHeaderHeight,
width: window.width,
transform: [{
translateY: scrollY.interpolate({
inputRange: [0, parallaxHeaderHeight - stickyHeaderHeight],
outputRange: [0, -parallaxHeaderHeight],
extrapolateRight: 'extend',
extrapolateLeft: 'clamp'
})
}, {
scale: scrollY.interpolate({
inputRange: [-window.height, 0],
outputRange: [5, 1],
extrapolate: 'clamp'
})
}]
}]}>
<View>
{ renderBackground && renderBackground() }
</View>
</Animated.View>
<ListView {...listViewProps}
style={[style, styles.listView]}
dataSource={dataSource}
ref="ListView"
scrollEventThrottle={16}
onScroll={e => {
this._animatedEvent(e);
prevOnScroll(e);
}}
renderHeader={() => (
<View style={styles.parallaxHeaderContainer}>
<Animated.View style={[styles.parallaxHeader, {
height: scrollY.interpolate({
inputRange: [0, parallaxHeaderHeight - stickyHeaderHeight],
outputRange: [parallaxHeaderHeight, stickyHeaderHeight],
extrapolate: 'clamp'
}),
opacity: scrollY.interpolate({
inputRange: [0, (parallaxHeaderHeight - stickyHeaderHeight) / 2 - 20, (parallaxHeaderHeight - stickyHeaderHeight) / 2],
outputRange: [1, .9, 0],
extrapolate: 'extend'
})
}]}>
{ renderParallaxHeader && renderParallaxHeader() }
</Animated.View>
</View>
)}
renderFooter={() => {
let height;
const extraTopHeight = renderStickyHeader ? stickyHeaderHeight : 0;
// If `rowHeight` is provided, we can calculate exact remaining height in order to allow
// parallax header to scroll all the way up. Otherwise, extra padding will have to be accounted
// for outside of this component.
if (rowHeight) {
height = Math.max(0, window.height - extraTopHeight - dataSource.getRowCount() * rowHeight);
} else {
height = 0;
}
return <View style={{ height }}/>;
}}/>
{ renderStickyHeader
? (
<View style={[styles.stickyHeader, { height: stickyHeaderHeight }]}>
<Animated.View style={{backgroundColor,
height: stickyHeaderHeight,
opacity: scrollY.interpolate({
inputRange: [-window.height, 0, stickyHeaderHeight],
outputRange: [0, 0, 1],
extrapolate: 'clamp'
})
}}>
<Animated.View style={{transform: [{
translateY: scrollY.interpolate({
inputRange: [-window.height, 0, stickyHeaderHeight],
outputRange: [stickyHeaderHeight, stickyHeaderHeight, 0],
extrapolate: 'clamp'
})
}]}}>
{ renderStickyHeader() }
</Animated.View>
</Animated.View>
{ renderFixedHeader && renderFixedHeader() }
</View>
)
: null
}
</View>
);
}
getScrollResponder() {
return this.refs.ListView.getScrollResponder();
}
}
ParallaxListView.propTypes = IPropTypes;
ParallaxListView.defaultProps = {
backgroundColor: '#000',
stickyHeaderHeight: 0,
style: {}
};
const window = Dimensions.get('window');
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'transparent'
},
parallaxHeaderContainer: {
backgroundColor: 'transparent',
overflow: 'hidden'
},
parallaxHeader: {
backgroundColor: 'transparent',
overflow: 'hidden'
},
backgroundImage: {
position: 'absolute',
backgroundColor: 'transparent',
top: 0
},
stickyHeader: {
backgroundColor: 'transparent',
position: 'absolute',
overflow: 'hidden',
top: 0,
left: 0,
width: window.width
},
listView: {
backgroundColor: 'transparent'
}
});
module.exports = ParallaxListView;