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
22 changes: 14 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component, ReactNode, CSSProperties } from 'react';
import { throttle } from 'throttle-debounce';
import { ThresholdUnits, parseThreshold } from './utils/threshold';
import { supportsPassive } from './utils/passive-event-feature-detection';

type Fn = () => any;
export interface Props {
Expand Down Expand Up @@ -80,9 +81,14 @@ export default class InfiniteScroll extends Component<Props, State> {
? this._infScroll
: this._scrollableNode || window;

const eventListenerOpts = supportsPassive() ? { passive: true } : false;

if (this.el) {
this.el.addEventListener('scroll', this
.throttledOnScrollListener as EventListenerOrEventListenerObject);
this.el.addEventListener(
'scroll',
this.throttledOnScrollListener as EventListenerOrEventListenerObject,
eventListenerOpts
);
}

if (
Expand All @@ -95,13 +101,13 @@ export default class InfiniteScroll extends Component<Props, State> {
}

if (this.props.pullDownToRefresh && this.el) {
this.el.addEventListener('touchstart', this.onStart);
this.el.addEventListener('touchmove', this.onMove);
this.el.addEventListener('touchend', this.onEnd);
this.el.addEventListener('touchstart', this.onStart, eventListenerOpts);
this.el.addEventListener('touchmove', this.onMove, eventListenerOpts);
this.el.addEventListener('touchend', this.onEnd, eventListenerOpts);

this.el.addEventListener('mousedown', this.onStart);
this.el.addEventListener('mousemove', this.onMove);
this.el.addEventListener('mouseup', this.onEnd);
this.el.addEventListener('mousedown', this.onStart, eventListenerOpts);
this.el.addEventListener('mousemove', this.onMove, eventListenerOpts);
this.el.addEventListener('mouseup', this.onEnd, eventListenerOpts);

// get BCR of pullDown element to position it above
this.maxPullDownDistance =
Expand Down
26 changes: 26 additions & 0 deletions src/utils/passive-event-feature-detection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-disable no-empty */

let cachedResult: null | boolean = null;
const dummyEventName = 'testPassive' as keyof WindowEventMap;

export function supportsPassive(): boolean {
if (cachedResult !== null) {
return cachedResult;
}
if (!window) {
return false; // to not fail if used with server-side rendering
}
try {
const opts = Object.defineProperty({}, 'passive', {
get: function() {
cachedResult = true;
},
});
window.addEventListener(dummyEventName, () => null, opts);
window.removeEventListener(dummyEventName, () => null, opts);
} catch (e) {}
if (cachedResult === null) {
cachedResult = false;
}
return cachedResult;
}