From 15f3e85a646b9b3bd55873fb784812233af43778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20Mo=CC=88ser?= Date: Mon, 1 Aug 2016 22:42:31 +0200 Subject: [PATCH 1/2] Add alwaysScroll option to always enable scrolling Set to false by default. --- README.md | 5 ++++- lib/AutoScroll.js | 8 +++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dd0ea41..ee194e4 100644 --- a/README.md +++ b/README.md @@ -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(/* ... */)) ``` @@ -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 diff --git a/lib/AutoScroll.js b/lib/AutoScroll.js index f3a066c..be6d99e 100644 --- a/lib/AutoScroll.js +++ b/lib/AutoScroll.js @@ -3,32 +3,34 @@ 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) { if (this._shouldScroll) { var node = this._node node.scrollTop = node.scrollHeight - this._shouldScroll = false + this._shouldScroll = alwaysScroll || false } }, 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 } }, From b40b727f0ef5a43ffcf7e13cf5e85dba4f64a8aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20Mo=CC=88ser?= Date: Mon, 1 Aug 2016 22:51:36 +0200 Subject: [PATCH 2/2] Remove alwaysScroll from componentDidUpdate Leads to scrolling all the time not only if property props change. --- lib/AutoScroll.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/AutoScroll.js b/lib/AutoScroll.js index be6d99e..92ad82e 100644 --- a/lib/AutoScroll.js +++ b/lib/AutoScroll.js @@ -23,7 +23,7 @@ module.exports = function AutoScroll (options) { if (this._shouldScroll) { var node = this._node node.scrollTop = node.scrollHeight - this._shouldScroll = alwaysScroll || false + this._shouldScroll = false } },