Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to ES6 and React 16 #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 79 additions & 77 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,87 +1,89 @@
'use strict';
import React from 'react'
import { View, Dimensions } from 'react-native'
import PropTypes from 'prop-types'

var React = require('react-native');
var window = React.Dimensions.get('window');
var {View, NativeMethodsMixin} = React;
const window = Dimensions.get('window')

module.exports = React.createClass({
displayName: 'InViewPort',
mixins: [NativeMethodsMixin],
propTypes: {
onChange: React.PropTypes.func.isRequired,
active: React.PropTypes.bool,
delay: React.PropTypes.number
},
class InViewPort extends React.PureComponent {
static propTypes = {
onChange: PropTypes.func.isRequired,
active: PropTypes.bool,
delay: PropTypes.number
}

getDefaultProps: function () {
return {
active: true,
delay: 100
};
},
static defaultProps = {
active: true,
delay: 100,
}

getInitialState: function(){
return {
rectTop: 0,
rectBottom: 0
}
},
componentDidMount: function () {
if (this.props.active) {
this.startWatching();
}
},
constructor(props) {
super(props)
this.state = {
rectTop: 0,
rectBottom: 0
}
}

componentWillUnmount: function () {
this.stopWatching();
},
onLayout() {
if (this.props.active) {
this.startWatching()
}
}

componentWillReceiveProps: function (nextProps) {
if (nextProps.active) {
this.lastValue = null;
this.startWatching();
} else {
this.stopWatching();
}
},
componentWillUnmount() {
this.stopWatching()
}

startWatching: function () {
if (this.interval) { return; }
this.interval = setInterval(this.check, this.props.delay);
},
componentWillReceiveProps(nextProps) {
if(nextProps.active != this.props.active){
if (nextProps.active) {
this.lastValue = null
this.startWatching()
} else {
this.stopWatching()
}
}
}

stopWatching: function () {
this.interval = clearInterval(this.interval);
},
/**
* Check if the element is within the visible viewport
*/
check: function () {
var el = this.refs.myview;
var rect = el.measure((ox, oy, width, height, pageX, pageY) => {
this.setState({
rectTop: pageY,
rectBottom: pageY + height,
rectWidth: pageX + width,
})
});
var isVisible = (
this.state.rectBottom != 0 && this.state.rectTop >= 0 && this.state.rectBottom <= window.height &&
this.state.rectWidth > 0 && this.state.rectWidth <= window.width
);
startWatching() {
if (this.interval) { return }
this.interval = setInterval(this.check, this.props.delay)
}

// notify the parent when the value changes
if (this.lastValue !== isVisible) {
this.lastValue = isVisible;
this.props.onChange(isVisible);
}
},
stopWatching() {
this.interval = clearInterval(this.interval)
}

render: function () {
return (
<View ref='myview' {...this.props}>
{this.props.children}
</View>
);
}
});
check = () => {
const el = this.myview
if (!el) { return }
const rect = el.measure((ox, oy, width, height, pageX, pageY) => {
this.setState({
rectTop: pageY,
rectBottom: pageY + height,
rectWidth: pageX + width,
})
})
const isVisible = (
this.state.rectBottom != 0 && this.state.rectTop >= 0 && this.state.rectBottom <= window.height &&
this.state.rectWidth > 0 && this.state.rectWidth <= window.width
)

// notify the parent when the value changes
if (this.lastValue !== isVisible) {
this.lastValue = isVisible
this.props.onChange(isVisible)
}
}

render() {
return (
<View ref={component => this.myview = component} {...this.props} onLayout={() => this.onLayout()}>
{this.props.children}
</View>
)
}

}

export default InViewPort