Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-headroom",
"description": "Hide your header until you need it. React.js port of headroom.js",
"version": "1.6.0",
"version": "1.6.1",
"author": "Kyle Mathews <[email protected]>",
"bugs": {
"url": "https://github.com/KyleAMathews/react-headroom/issues"
Expand Down
20 changes: 13 additions & 7 deletions src/index.cjsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = React.createClass
ticking: false

propTypes:
parent: React.PropTypes.node
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parent should always be a function right? so React.PropTypes.func?

children: PropTypes.any.isRequired
disableInlineStyles: PropTypes.bool
disable: PropTypes.bool
Expand All @@ -28,6 +29,7 @@ module.exports = React.createClass
wrapperStyle: PropTypes.object

getDefaultProps: ->
parent: -> window
disableInlineStyles: false
disable: false
upTolerance: 5
Expand All @@ -37,6 +39,7 @@ module.exports = React.createClass
onUnfix: ->
wrapperStyle: {}


getInitialState: ->
state: 'unfixed'
translateY: 0
Expand All @@ -45,23 +48,26 @@ module.exports = React.createClass
componentDidMount: ->
@setState height: @refs.inner.getDOMNode().offsetHeight
unless @props.disable
window.addEventListener('scroll', @handleScroll)
@props.parent().addEventListener('scroll', @handleScroll)

componentWillReceiveProps: (nextProps) ->
if nextProps.disable and not @props.disable
@unfix()

# Remove the event listener
window.removeEventListener('scroll', @handleScroll)
@props.parent().removeEventListener('scroll', @handleScroll)

else if not nextProps.disable and @props.disable
window.addEventListener('scroll', @handleScroll)
@props.parent().addEventListener('scroll', @handleScroll)

componentDidUpdate: (prevProps, prevState) ->
# If children have changed, remeasure height.
if prevProps.children isnt @props.children
@setState height: @refs.inner.getDOMNode().offsetHeight

componentWillUnmount: ->
@props.parent().removeEventListener('scroll', @handleScroll)

handleScroll: ->
unless @ticking
@ticking = true
Expand Down Expand Up @@ -112,10 +118,10 @@ module.exports = React.createClass
@ticking = false

getScrollY: ->
if window.pageYOffset != undefined
window.pageYOffset
else if window.scrollTop != undefined
window.scrollTop
if @props.parent().pageYOffset != undefined
@props.parent().pageYOffset
else if @props.parent().scrollTop != undefined
@props.parent().scrollTop
else (document.documentElement or
document.body.parentNode or
document.body).scrollTop
Expand Down