-
Notifications
You must be signed in to change notification settings - Fork 93
/
index.js
81 lines (72 loc) · 1.68 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
'use strict';
import React, { Component } from 'react'
import { View, NativeMethodsMixin, Dimensions } from 'react-native'
exports.InViewPort = class extends Component {
constructor(props) {
super(props)
this.state = { rectTop: 0, rectBottom: 0 }
}
componentDidMount() {
if (!this.props.disabled) {
this.startWatching()
}
}
componentWillUnmount() {
this.stopWatching()
}
componentWillReceiveProps(nextProps) {
if (nextProps.disabled) {
this.stopWatching()
} else {
this.lastValue = null
this.startWatching()
}
}
startWatching() {
if (this.interval) {
return
}
this.interval = setInterval(() => {
if (!this.myview) {
return
}
this.myview.measure((x, y, width, height, pageX, pageY) => {
this.setState({
rectTop: pageY,
rectBottom: pageY + height,
rectWidth: pageX + width
})
})
this.isInViewPort()
}, this.props.delay || 100)
}
stopWatching() {
this.interval = clearInterval(this.interval)
}
isInViewPort() {
const window = Dimensions.get('window')
const isVisible =
this.state.rectBottom != 0 &&
this.state.rectTop >= 0 &&
this.state.rectBottom <= window.height &&
this.state.rectWidth > 0 &&
this.state.rectWidth <= window.width
if (this.lastValue !== isVisible) {
this.lastValue = isVisible
this.props.onChange(isVisible)
}
}
render() {
return (
<View
collapsable={false}
ref={component => {
this.myview = component
}}
{...this.props}
>
{this.props.children}
</View>
)
}
}