Last active
August 27, 2015 14:47
-
-
Save elpete/d397e1600b303e3e9d09 to your computer and use it in GitHub Desktop.
Add a pin to bottom scroll behavior by wrapping a component in this ScrollManager.
This file contains 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
import React, { Component, PropTypes } from 'react'; | |
const styles = { | |
scrollManager: { | |
position: 'absolute', | |
height: '100%', | |
width: '100%', | |
overflowY: 'scroll' | |
} | |
}; | |
class ScrollManager extends Component { | |
componentWillMount() { | |
this.pinToBottom = true; | |
} | |
componentDidMount() { | |
this.adjustScrollPosition(); | |
} | |
componentWillUpdate() { | |
var node = React.findDOMNode(this); | |
this.scrollToBottom = this.props.shouldScrollToBottom(node); | |
} | |
componentDidUpdate() { | |
if (this.scrollToBottom) { | |
var node = React.findDOMNode(this); | |
node.scrollTop = node.scrollHeight; | |
} | |
} | |
render() { | |
return ( | |
<div style={styles.scrollManager}> | |
{this.props.children} | |
</div> | |
); | |
} | |
} | |
const { func } = PropTypes; | |
ScrollManager.propTypes = { | |
shouldScrollToBottom: func | |
}; | |
ScrollManager.defaultProps = { | |
shouldScrollToBottom: node => ( | |
node.scrollHeight - (node.clientHeight + node.scrollTop) < 10; | |
) | |
}; | |
export default ScrollManager; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment