|
1 |
| -'use strict'; |
2 |
| - |
3 |
| -var React = require('react-native'); |
4 |
| -var window = React.Dimensions.get('window'); |
5 |
| -var {View, NativeMethodsMixin} = React; |
6 |
| - |
7 |
| -module.exports = React.createClass({ |
8 |
| - displayName: 'InViewPort', |
9 |
| - mixins: [NativeMethodsMixin], |
10 |
| - propTypes: { |
11 |
| - onChange: React.PropTypes.func.isRequired, |
12 |
| - active: React.PropTypes.bool, |
13 |
| - delay: React.PropTypes.number |
14 |
| - }, |
15 |
| - |
16 |
| - getDefaultProps: function () { |
17 |
| - return { |
18 |
| - active: true, |
19 |
| - delay: 100 |
20 |
| - }; |
21 |
| - }, |
22 |
| - |
23 |
| - getInitialState: function(){ |
24 |
| - return { |
25 |
| - rectTop: 0, |
26 |
| - rectBottom: 0 |
27 |
| - } |
28 |
| - }, |
29 |
| - componentDidMount: function () { |
30 |
| - if (this.props.active) { |
31 |
| - this.startWatching(); |
32 |
| - } |
33 |
| - }, |
34 |
| - |
35 |
| - componentWillUnmount: function () { |
36 |
| - this.stopWatching(); |
37 |
| - }, |
38 |
| - |
39 |
| - componentWillReceiveProps: function (nextProps) { |
40 |
| - if (nextProps.active) { |
41 |
| - this.lastValue = null; |
42 |
| - this.startWatching(); |
43 |
| - } else { |
44 |
| - this.stopWatching(); |
45 |
| - } |
46 |
| - }, |
47 |
| - |
48 |
| - startWatching: function () { |
49 |
| - if (this.interval) { return; } |
50 |
| - this.interval = setInterval(this.check, this.props.delay); |
51 |
| - }, |
52 |
| - |
53 |
| - stopWatching: function () { |
54 |
| - this.interval = clearInterval(this.interval); |
55 |
| - }, |
56 |
| - /** |
57 |
| - * Check if the element is within the visible viewport |
58 |
| - */ |
59 |
| - check: function () { |
60 |
| - var el = this.refs.myview; |
61 |
| - var rect = el.measure((ox, oy, width, height, pageX, pageY) => { |
62 |
| - this.setState({ |
63 |
| - rectTop: pageY, |
64 |
| - rectBottom: pageY + height, |
65 |
| - rectWidth: pageX + width, |
66 |
| - }) |
67 |
| - }); |
68 |
| - var isVisible = ( |
69 |
| - this.state.rectBottom != 0 && this.state.rectTop >= 0 && this.state.rectBottom <= window.height && |
70 |
| - this.state.rectWidth > 0 && this.state.rectWidth <= window.width |
71 |
| - ); |
72 |
| - |
73 |
| - // notify the parent when the value changes |
74 |
| - if (this.lastValue !== isVisible) { |
75 |
| - this.lastValue = isVisible; |
76 |
| - this.props.onChange(isVisible); |
77 |
| - } |
78 |
| - }, |
79 |
| - |
80 |
| - render: function () { |
81 |
| - return ( |
82 |
| - <View ref='myview' {...this.props}> |
83 |
| - {this.props.children} |
84 |
| - </View> |
85 |
| - ); |
86 |
| - } |
87 |
| -}); |
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var React = require('react'); |
| 4 | +var ReactNative = require('react-native'); |
| 5 | +var window = ReactNative.Dimensions.get('window'); |
| 6 | +var {View, NativeMethodsMixin} = ReactNative; |
| 7 | + |
| 8 | +module.exports = React.createClass({ |
| 9 | + displayName: 'InViewPort', |
| 10 | + mixins: [NativeMethodsMixin], |
| 11 | + propTypes: { |
| 12 | + onChange: React.PropTypes.func.isRequired, |
| 13 | + active: React.PropTypes.bool, |
| 14 | + delay: React.PropTypes.number |
| 15 | + }, |
| 16 | + |
| 17 | + getDefaultProps: function () { |
| 18 | + return { |
| 19 | + active: true, |
| 20 | + delay: 100 |
| 21 | + }; |
| 22 | + }, |
| 23 | + |
| 24 | + getInitialState: function(){ |
| 25 | + return { |
| 26 | + rectTop: 0, |
| 27 | + rectBottom: 0 |
| 28 | + } |
| 29 | + }, |
| 30 | + componentDidMount: function () { |
| 31 | + if (this.props.active) { |
| 32 | + this.startWatching(); |
| 33 | + } |
| 34 | + }, |
| 35 | + |
| 36 | + componentWillUnmount: function () { |
| 37 | + this.stopWatching(); |
| 38 | + }, |
| 39 | + |
| 40 | + componentWillReceiveProps: function (nextProps) { |
| 41 | + if (nextProps.active) { |
| 42 | + this.lastValue = null; |
| 43 | + this.startWatching(); |
| 44 | + } else { |
| 45 | + this.stopWatching(); |
| 46 | + } |
| 47 | + }, |
| 48 | + |
| 49 | + startWatching: function () { |
| 50 | + if (this.interval) { return; } |
| 51 | + this.interval = setInterval(this.check, this.props.delay); |
| 52 | + }, |
| 53 | + |
| 54 | + stopWatching: function () { |
| 55 | + this.interval = clearInterval(this.interval); |
| 56 | + }, |
| 57 | + /** |
| 58 | + * Check if the element is within the visible viewport |
| 59 | + */ |
| 60 | + check: function () { |
| 61 | + var el = this.refs.myview; |
| 62 | + var rect = el.measure((ox, oy, width, height, pageX, pageY) => { |
| 63 | + this.setState({ |
| 64 | + rectTop: pageY, |
| 65 | + rectBottom: pageY + height |
| 66 | + }) |
| 67 | + }); |
| 68 | + var isVisible = ( |
| 69 | + this.state.rectTop >= 0 && this.state.rectBottom <= window.height |
| 70 | + ); |
| 71 | + |
| 72 | + // notify the parent when the value changes |
| 73 | + if (this.lastValue !== isVisible) { |
| 74 | + this.lastValue = isVisible; |
| 75 | + this.props.onChange(isVisible); |
| 76 | + } |
| 77 | + }, |
| 78 | + |
| 79 | + render: function () { |
| 80 | + return ( |
| 81 | + <View ref='myview' {...this.props}> |
| 82 | + {this.props.children} |
| 83 | + </View> |
| 84 | + ); |
| 85 | + } |
| 86 | +}); |
0 commit comments