forked from oblador/react-native-parallax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParallaxScrollViewComposition.js
82 lines (72 loc) · 2 KB
/
ParallaxScrollViewComposition.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
/**
* @providesModule ParallaxComposition
*/
'use strict';
var isArray = require('lodash/lang/isArray');
var React = require('react');
var {
Animated,
ScrollView
} = require('react-native');
var ParallaxImage = require('./ParallaxImage');
var applyPropsToParallaxImages = function(children, props) {
if(isArray(children)) {
return children.map(child => {
if(isArray(child)) {
return applyPropsToParallaxImages(child, props);
}
if(child.type === ParallaxImage) {
return React.cloneElement(child, props);
}
return child;
});
}
if(children.type === ParallaxImage) {
return React.cloneElement(children, props);
}
return children;
};
var ParallaxScrollViewComposition = React.createClass({
propTypes: {
scrollViewComponent: React.PropTypes.func,
},
setNativeProps: function(nativeProps) {
this._root.setNativeProps(nativeProps);
},
getScrollResponder: function() {
return this._scrollComponent.getScrollResponder();
},
componentWillMount: function() {
var scrollY = new Animated.Value(0);
this.setState({ scrollY });
this.onParallaxScroll = Animated.event(
[{nativeEvent: {contentOffset: {y: scrollY}}}]
);
},
render: function() {
var { ref, children, scrollViewComponent, onScroll, ...props } = this.props;
var { scrollY } = this.state;
var ScrollComponent = scrollViewComponent || ScrollView;
var handleScroll = (onScroll
? event => { this.onParallaxScroll(event); onScroll(event); }
: this.onParallaxScroll
);
children = children && applyPropsToParallaxImages(children, { scrollY });
return (
<ScrollComponent
ref={component => {
this._root = component;
if(typeof ref === 'function') {
ref(component);
}
}}
scrollEventThrottle={16}
onScroll={handleScroll}
{...props}
>
{children}
</ScrollComponent>
);
}
});
module.exports = ParallaxScrollViewComposition;