Skip to content
Open
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ $ npm i react-auto-scroll
var React = require('react')
var AutoScroll = require('react-auto-scroll')
var Component = AutoScroll({
property: 'propertyName'
property: 'propertyName',
alwaysScroll: false
})(React.createClass(/* ... */))
```

Expand All @@ -26,6 +27,8 @@ var Component = AutoScroll({

* `options.property` (string) Property to track for scrolling

* `options.alwaysScroll` (boolean) Always enable scrolling even if user has scrolled up. Set to `false` by default.

## License

MIT
6 changes: 4 additions & 2 deletions lib/AutoScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ var ReactDOM = require('react-dom')

module.exports = function AutoScroll (options) {
var property = options.property
var alwaysScroll = options.alwaysScroll || false

return function (Component) {
var displayName = Component.displayName || Component.name || 'Component'

var propTypes = {}
propTypes[property] = React.PropTypes.any
propTypes[alwaysScroll] = React.PropTypes.bool

var AutoScrollComponent = React.createClass({
componentDidMount: function componentDidMount () {
var node = this._node
node.scrollTop = node.scrollHeight
this._shouldScroll = false
this._shouldScroll = alwaysScroll || false
},

componentDidUpdate: function componentDidUpdate (prevProps) {
Expand All @@ -28,7 +30,7 @@ module.exports = function AutoScroll (options) {
componentWillUpdate: function componentWillUpdate (nextProps) {
if (this.props[property] !== nextProps[property]) {
var node = this._node
this._shouldScroll = node.scrollTop + node.offsetHeight === node.scrollHeight
this._shouldScroll = alwaysScroll || node.scrollTop + node.offsetHeight === node.scrollHeight
}
},

Expand Down