Created
April 24, 2015 08:30
-
-
Save eldh/d6342b45d501766ca72c to your computer and use it in GitHub Desktop.
react-in-view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = React.createClass | |
displayName: 'InViewWrapper' | |
propsTypes: | |
onInView: React.PropTypes.func.isRequired | |
inViewOffset: React.PropTypes.number | |
getDefaultProps: -> | |
inViewOffset: 0 | |
getInitialState: -> | |
submerged: false | |
render: -> | |
React.DOM.div _.omit(@props, ['onInView', 'onInViewData', 'inViewOffset']), @props.children | |
componentWilllUnmount: -> | |
window.removeEventListener 'scroll', @debouncedCheckPosition | |
componentWillMount: -> | |
@debouncedCheckPosition = _.throttle @checkPosition, 300 | |
window.addEventListener 'scroll', @debouncedCheckPosition | |
checkPosition: -> | |
rect = @getDOMNode().getBoundingClientRect() | |
top = rect.top | |
height = rect.height | |
if (not @state.submerged and top < @props.inViewOffset) or (@state.submerged and top + height > @props.inViewOffset) | |
@setState submerged: [email protected] | |
@props.onInView @props.onInViewData |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment