Skip to content

Commit

Permalink
When supported, mark scroll listener as passive (#45)
Browse files Browse the repository at this point in the history
* feat: Implement passive listener feature detection

* feat(scroll): When supported, mark scroll listener as passive

Closes #38
  • Loading branch information
vhfmag authored and jaredpalmer committed Nov 26, 2017
1 parent fdac745 commit 59404c6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Scroll/Scroll.tsx
Expand Up @@ -9,6 +9,7 @@ import * as React from 'react';
import { SharedRenderProps } from '../types';
import { isEmptyChildren } from '../utils';
import { throttle } from '../utils/throttle';
import { supportsPassiveListener } from '../utils/featureDetection';

export interface ScrollProps {
x: number;
Expand All @@ -35,7 +36,11 @@ export class Scroll extends React.Component<

componentDidMount() {
this.handleWindowScroll();
window.addEventListener('scroll', this.handleWindowScroll);
(window as EventTarget).addEventListener(
'scroll',
this.handleWindowScroll,
supportsPassiveListener ? { passive: true } : false
);
}

componentWillUnmount() {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/featureDetection.ts
@@ -0,0 +1,11 @@
export let supportsPassiveListener = false;

try {
const opts = Object.defineProperty({}, 'passive', {
get: function() {
supportsPassiveListener = true;
},
});
(window as EventTarget).addEventListener('testPassive', null, opts);
(window as EventTarget).removeEventListener('testPassive', null, opts);
} catch (e) {}

0 comments on commit 59404c6

Please sign in to comment.