From 02879435d9fb402b53a8c846b6be936cd5ec18ce Mon Sep 17 00:00:00 2001 From: Mathieu M-Gosselin Date: Wed, 8 Jul 2015 14:51:58 -0400 Subject: [PATCH 1/2] Added support to use the document scroll instead of the element scroll. --- src/component.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/component.js b/src/component.js index dd26c4a..20e3a7a 100644 --- a/src/component.js +++ b/src/component.js @@ -4,12 +4,14 @@ */ var React = require('react'); +var documentOffset = require('document-offset'); var ContinuousScroll = React.createClass({ propTypes: { hasMore: React.PropTypes.bool.isRequired, loadMore: React.PropTypes.func.isRequired, isLoading: React.PropTypes.bool.isRequired, + useDocument: React.PropTypes.bool, threshold: React.PropTypes.number, loader: React.PropTypes.element, disablePointer: React.PropTypes.number, @@ -35,8 +37,9 @@ var ContinuousScroll = React.createClass({ return; var el = this.getDOMNode(); + var currentScroll = this.props.useDocument ? document.body.scrollTop + documentOffset(el).top : el.scrollTop + el.offsetHeight; - if (el.scrollTop + el.offsetHeight + this.props.threshold < el.scrollHeight) + if(currentScroll + this.props.threshold < el.scrollHeight) return; this.props.loadMore(); @@ -56,13 +59,11 @@ var ContinuousScroll = React.createClass({ }, componentDidMount: function () { this.listenScroll(); - + // About setTimeout: fluxxor enforce flux principle; dispatching an action during and action would trigger an error setTimeout(this.onScroll); }, componentDidUpdate: function () { - var el = this.getDOMNode(); - // when component update need to check if loaded children height are bigger than threshold else load more // About setTimeout: fluxxor enforce flux principle; dispatching an action during and action would trigger an error setTimeout(this.onScroll); @@ -71,11 +72,15 @@ var ContinuousScroll = React.createClass({ this.unlistenScroll(); }, listenScroll: function () { - this.getDOMNode().addEventListener('scroll', this.onScroll); + var el = this.props.useDocument ? window : this.getDOMNode(); + + el.addEventListener('scroll', this.onScroll); window.addEventListener('resize', this.onScroll); }, unlistenScroll: function () { - this.getDOMNode().removeEventListener('scroll', this.onScroll); + var el = this.props.useDocument ? window : this.getDOMNode(); + + el.removeEventListener('scroll', this.onScroll); window.removeEventListener('resize', this.onScroll); }, componentWillReceiveProps: function (nextProps) { From 9f6a847dafbdc83b38019661d653a24e8c6a7e0f Mon Sep 17 00:00:00 2001 From: Mathieu M-Gosselin Date: Thu, 9 Jul 2015 11:28:26 -0400 Subject: [PATCH 2/2] Documented the `useDocument` property. --- README.md | 1 + package.json | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index 04c7dca..0027151 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ propTypes: { hasMore: React.PropTypes.bool.isRequired, // if there is more to load loadMore: React.PropTypes.func.isRequired, // callback to load more isLoading: React.PropTypes.bool.isRequired, // indicate if a loading is ongoing + useDocument: React.PropTypes.bool, // if true, the scrolling is calculated based on the document and not the element, default false threshold: React.PropTypes.number, // pixel threshold, default 1000 loader: React.PropTypes.component, // displayed loader component, default React.DOM.div(null, 'Loading...') diff --git a/package.json b/package.json index 74560ba..7c20021 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "author": "@nrako", "license": "MIT", "dependencies": { + "document-offset": "^1.0.4" }, "devDependencies": { "react": "^0.12.0"