Skip to content

Commit

Permalink
Merge pull request #196 from timbielawski/feature/passive-event-liste…
Browse files Browse the repository at this point in the history
…ners

Feature/passive event listeners
  • Loading branch information
danbovey authored Dec 24, 2018
2 parents 777d63b + 7dc2dbe commit a96b855
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 18 deletions.
54 changes: 46 additions & 8 deletions dist/InfiniteScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ var InfiniteScroll = (function(_Component) {
);

_this.scrollListener = _this.scrollListener.bind(_this);
_this.eventListenerOptions = _this.eventListenerOptions.bind(_this);
_this.mousewheelListener = _this.mousewheelListener.bind(_this);
return _this;
}

Expand All @@ -104,6 +106,7 @@ var InfiniteScroll = (function(_Component) {
key: 'componentDidMount',
value: function componentDidMount() {
this.pageLoaded = this.props.pageStart;
this.options = this.eventListenerOptions();
this.attachScrollListener();
}
},
Expand All @@ -126,8 +129,40 @@ var InfiniteScroll = (function(_Component) {
value: function componentWillUnmount() {
this.detachScrollListener();
this.detachMousewheelListener();
}
},
},
{
key: 'isPassiveSupported',
value: function isPassiveSupported() {
var passive = false;

var testOptions = {
get passive() {
passive = true;
},
};

try {
document.addEventListener('test', null, testOptions);
} catch (e) {
// ignore
}
return passive;
},
},
{
key: 'eventListenerOptions',
value: function eventListenerOptions() {
var options = false;

if (this.isPassiveSupported()) {
options = {
useCapture: this.props.useCapture,
passive: this.props.passive,
};
}
return options;
}
// Set a defaut loader for all your `InfiniteScroll` components
},
{
Expand All @@ -147,7 +182,7 @@ var InfiniteScroll = (function(_Component) {
scrollEl.removeEventListener(
'mousewheel',
this.mousewheelListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture
);
}
},
Expand All @@ -162,12 +197,12 @@ var InfiniteScroll = (function(_Component) {
scrollEl.removeEventListener(
'scroll',
this.scrollListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture
);
scrollEl.removeEventListener(
'resize',
this.scrollListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture
);
}
},
Expand Down Expand Up @@ -205,17 +240,17 @@ var InfiniteScroll = (function(_Component) {
scrollEl.addEventListener(
'mousewheel',
this.mousewheelListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture,
);
scrollEl.addEventListener(
'scroll',
this.scrollListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture,
);
scrollEl.addEventListener(
'resize',
this.scrollListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture
);

if (this.props.initialLoad) {
Expand All @@ -228,7 +263,10 @@ var InfiniteScroll = (function(_Component) {
value: function mousewheelListener(e) {
// Prevents Chrome hangups
// See: https://stackoverflow.com/questions/47524205/random-high-content-download-time-in-chrome/47684257#47684257
if (e.deltaY === 1) {
if (
e.deltaY === 1 &&
(!this.props.passive || !this.isPassiveSupported())
) {
e.preventDefault();
}
}
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"url": "git://github.com/CassetteRocks/react-infinite-scroller.git"
},
"scripts": {
"build":
"mkdirp dist && babel src/InfiniteScroll.js --out-file dist/InfiniteScroll.js",
"build": "mkdirp dist && babel src/InfiniteScroll.js --out-file dist/InfiniteScroll.js",
"prepublish": "npm run build",
"test": "nyc npm run spec",
"spec": "_mocha -R spec ./test/test_helper.js --recursive test/*_test.js",
Expand All @@ -23,7 +22,11 @@
"git add"
]
},
"keywords": ["infinite", "scroll", "react"],
"keywords": [
"infinite",
"scroll",
"react"
],
"author": "CassetteRocks",
"license": "MIT",
"bugs": {
Expand Down
47 changes: 40 additions & 7 deletions src/InfiniteScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ export default class InfiniteScroll extends Component {
super(props);

this.scrollListener = this.scrollListener.bind(this);
this.eventListenerOptions = this.eventListenerOptions.bind(this);
this.mousewheelListener = this.mousewheelListener.bind(this);
}

componentDidMount() {
this.pageLoaded = this.props.pageStart;
this.options = this.eventListenerOptions();
this.attachScrollListener();
}

Expand All @@ -60,6 +63,36 @@ export default class InfiniteScroll extends Component {
this.detachMousewheelListener();
}

isPassiveSupported() {
let passive = false;

const testOptions = {
get passive() {
passive = true;
}
};

try {
document.addEventListener('test', null, testOptions);
document.removeEventListener('test', null, testOptions);
} catch (e) {
// ignore
}
return passive;
}

eventListenerOptions() {
let options = this.props.useCapture;

if (this.isPassiveSupported()) {
options = {
useCapture: this.props.useCapture,
passive: true
};
}
return options;
}

// Set a defaut loader for all your `InfiniteScroll` components
setDefaultLoader(loader) {
this.defaultLoader = loader;
Expand All @@ -74,7 +107,7 @@ export default class InfiniteScroll extends Component {
scrollEl.removeEventListener(
'mousewheel',
this.mousewheelListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture
);
}

Expand All @@ -87,12 +120,12 @@ export default class InfiniteScroll extends Component {
scrollEl.removeEventListener(
'scroll',
this.scrollListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture
);
scrollEl.removeEventListener(
'resize',
this.scrollListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture
);
}

Expand Down Expand Up @@ -124,17 +157,17 @@ export default class InfiniteScroll extends Component {
scrollEl.addEventListener(
'mousewheel',
this.mousewheelListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture
);
scrollEl.addEventListener(
'scroll',
this.scrollListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture
);
scrollEl.addEventListener(
'resize',
this.scrollListener,
this.props.useCapture
this.options ? this.options : this.props.useCapture
);

if (this.props.initialLoad) {
Expand All @@ -145,7 +178,7 @@ export default class InfiniteScroll extends Component {
mousewheelListener(e) {
// Prevents Chrome hangups
// See: https://stackoverflow.com/questions/47524205/random-high-content-download-time-in-chrome/47684257#47684257
if (e.deltaY === 1) {
if (e.deltaY === 1 && !this.isPassiveSupported()) {
e.preventDefault();
}
}
Expand Down

0 comments on commit a96b855

Please sign in to comment.