Skip to content

Instantly share code, notes, and snippets.

@elpete
Last active August 27, 2015 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpete/d397e1600b303e3e9d09 to your computer and use it in GitHub Desktop.
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.
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