|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { |
| 3 | + Animated, |
| 4 | + ScrollView, |
| 5 | + StyleSheet, |
| 6 | + View, |
| 7 | + } from 'react-native'; |
| 8 | +import _ from 'lodash'; |
| 9 | + |
| 10 | +const SCROLLVIEW_REF = 'ScrollView'; |
| 11 | + |
| 12 | +const styles = StyleSheet.create({ |
| 13 | + container: { |
| 14 | + flex: 1, |
| 15 | + }, |
| 16 | + header: { |
| 17 | + position: 'absolute', |
| 18 | + top: 0, |
| 19 | + left: 0, |
| 20 | + right: 0, |
| 21 | + overflow: 'hidden', |
| 22 | + }, |
| 23 | + headerChildren: { |
| 24 | + backgroundColor: 'transparent', |
| 25 | + justifyContent: 'center', |
| 26 | + }, |
| 27 | + blackOverlay: { |
| 28 | + backgroundColor: 'black', |
| 29 | + position: 'absolute', |
| 30 | + top: 0, |
| 31 | + right: 0, |
| 32 | + left: 0, |
| 33 | + bottom: 0, |
| 34 | + zIndex: 100, |
| 35 | + }, |
| 36 | + fixedForeground: { |
| 37 | + position: 'absolute', |
| 38 | + top: 0, |
| 39 | + right: 0, |
| 40 | + left: 0, |
| 41 | + bottom: 0, |
| 42 | + zIndex: 101, |
| 43 | + } |
| 44 | +}); |
| 45 | + |
| 46 | + |
| 47 | +class ImageHeaderScrollView extends Component { |
| 48 | + constructor(props) { |
| 49 | + super(props); |
| 50 | + this.state = { |
| 51 | + scrollY: new Animated.Value(0), |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + /* |
| 56 | + * Expose `ScrollView` API so this component is composable |
| 57 | + * with any component that expects a `ScrollView`. |
| 58 | + */ |
| 59 | + getScrollResponder() { |
| 60 | + return this[SCROLLVIEW_REF].getScrollResponder(); |
| 61 | + } |
| 62 | + getScrollableNode() { |
| 63 | + return this.getScrollResponder().getScrollableNode(); |
| 64 | + } |
| 65 | + getInnerViewNode() { |
| 66 | + return this.getScrollResponder().getInnerViewNode(); |
| 67 | + } |
| 68 | + setNativeProps(props) { |
| 69 | + this[SCROLLVIEW_REF].setNativeProps(props); |
| 70 | + } |
| 71 | + scrollTo(...args) { |
| 72 | + this.getScrollResponder().scrollTo(...args); |
| 73 | + } |
| 74 | + |
| 75 | + getChildContext() { |
| 76 | + return { scrollY: this.state.scrollY }; |
| 77 | + } |
| 78 | + |
| 79 | + interpolateOnImageHeight(outputRange) { |
| 80 | + const headerScrollDistance = this.props.maxHeight - this.props.minHeight; |
| 81 | + return this.state.scrollY.interpolate({ |
| 82 | + inputRange: [0, headerScrollDistance], |
| 83 | + outputRange, |
| 84 | + extrapolate: 'clamp', |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + renderHeader() { |
| 89 | + const headerHeight = this.interpolateOnImageHeight([ |
| 90 | + this.props.maxHeight, |
| 91 | + this.props.minHeight, |
| 92 | + ]); |
| 93 | + const overlayOpacity = this.interpolateOnImageHeight([ |
| 94 | + this.props.minOverlayOpacity, |
| 95 | + this.props.maxOverlayOpacity, |
| 96 | + ]); |
| 97 | + |
| 98 | + const headerScale = this.state.scrollY.interpolate({ |
| 99 | + inputRange: [-this.props.maxHeight, 0], |
| 100 | + outputRange: [3, 1], |
| 101 | + extrapolate: 'clamp', |
| 102 | + }); |
| 103 | + |
| 104 | + const headerTransformStyle = { height: headerHeight, transform: [{ scale: headerScale }] }; |
| 105 | + return ( |
| 106 | + <Animated.View style={[styles.header, headerTransformStyle]}> |
| 107 | + <Animated.View style={[styles.blackOverlay, { opacity: overlayOpacity }]} /> |
| 108 | + <View style={styles.fixedForeground}> |
| 109 | + { this.props.renderFixedForeground() } |
| 110 | + </View> |
| 111 | + { this.props.renderHeader() } |
| 112 | + </Animated.View> |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + renderForeground() { |
| 117 | + const headerTranslate = this.state.scrollY.interpolate({ |
| 118 | + inputRange: [0, this.props.maxHeight * 2], |
| 119 | + outputRange: [0, -this.props.maxHeight * 2 * this.props.foregroundParallaxRatio], |
| 120 | + extrapolate: 'clamp', |
| 121 | + }); |
| 122 | + const opacity = this.interpolateOnImageHeight([1, -0.3]); |
| 123 | + |
| 124 | + const headerTransformStyle = { |
| 125 | + height: this.props.maxHeight, |
| 126 | + transform: [{ translateY: headerTranslate }], |
| 127 | + opacity: this.props.fadeOutForeground ? opacity : 1, |
| 128 | + }; |
| 129 | + return ( |
| 130 | + <Animated.View style={[styles.header, headerTransformStyle]}> |
| 131 | + { this.props.renderForeground() } |
| 132 | + </Animated.View> |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + render() { |
| 137 | + const headerScrollDistance = this.props.maxHeight - this.props.minHeight; |
| 138 | + const scrollViewProps = _.pick(this.props, _.keys(ScrollView.propTypes)); |
| 139 | + return ( |
| 140 | + <View style={styles.container}> |
| 141 | + <ScrollView |
| 142 | + ref={(sv) => { this[SCROLLVIEW_REF] = sv; }} |
| 143 | + style={[styles.container, { marginTop: this.props.minHeight }]} |
| 144 | + scrollEventThrottle={16} |
| 145 | + onScroll={Animated.event( |
| 146 | + [{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }], |
| 147 | + )} |
| 148 | + {...scrollViewProps} |
| 149 | + > |
| 150 | + <Animated.View style={[{ paddingTop: headerScrollDistance }, this.props.childrenStyle]}> |
| 151 | + {this.props.children} |
| 152 | + </Animated.View> |
| 153 | + </ScrollView> |
| 154 | + { this.renderHeader() } |
| 155 | + { this.renderForeground() } |
| 156 | + </View> |
| 157 | + ); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +ImageHeaderScrollView.propTypes = { |
| 162 | + children: React.PropTypes.node || React.PropTypes.nodes, |
| 163 | + childrenStyle: View.propTypes.style, |
| 164 | + fadeOutForeground: React.PropTypes.bool, |
| 165 | + foregroundParallaxRatio: React.PropTypes.number, |
| 166 | + maxHeight: React.PropTypes.number, |
| 167 | + maxOverlayOpacity: React.PropTypes.number, |
| 168 | + minHeight: React.PropTypes.number, |
| 169 | + minOverlayOpacity: React.PropTypes.number, |
| 170 | + renderFixedForeground: React.PropTypes.func, |
| 171 | + renderForeground: React.PropTypes.func, |
| 172 | + renderHeader: React.PropTypes.func, |
| 173 | + ...ScrollView.propTypes, |
| 174 | +}; |
| 175 | + |
| 176 | +ImageHeaderScrollView.defaultProps = { |
| 177 | + fadeOutForeground: false, |
| 178 | + foregroundParallaxRatio: 1, |
| 179 | + maxHeight: 125, |
| 180 | + maxOverlayOpacity: 0.3, |
| 181 | + minHeight: 80, |
| 182 | + minOverlayOpacity: 0, |
| 183 | + renderFixedForeground: () => <View />, |
| 184 | + renderForeground: () => <View />, |
| 185 | + renderHeader: () => <View />, |
| 186 | +}; |
| 187 | + |
| 188 | +ImageHeaderScrollView.childContextTypes = { |
| 189 | + scrollY: React.PropTypes.instanceOf(Animated.Value), |
| 190 | +}; |
| 191 | + |
| 192 | +export default ImageHeaderScrollView; |
0 commit comments