Skip to content

Instantly share code, notes, and snippets.

@eldh
Created April 24, 2015 08:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldh/d6342b45d501766ca72c to your computer and use it in GitHub Desktop.
Save eldh/d6342b45d501766ca72c to your computer and use it in GitHub Desktop.
react-in-view
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: !@state.submerged
@props.onInView @props.onInViewData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment