Skip to content

Commit

Permalink
Merge pull request #4437 from JedWatson/add-non-passive-event-listeners
Browse files Browse the repository at this point in the history
Declare event listeners as non-passive to remove browser warnings
  • Loading branch information
JedWatson authored Mar 4, 2021
2 parents 88cb46a + 7891f32 commit 711967a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-pianos-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-select': patch
---

Set event listeners to be non-passive to remove Chrome console warnings
8 changes: 5 additions & 3 deletions packages/react-select/src/internal/useScrollCapture.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import { useCallback, useEffect, useRef } from 'react';
import { supportsPassiveEvents } from '../utils';

const cancelScroll = (event: SyntheticEvent<HTMLElement>) => {
event.preventDefault();
Expand Down Expand Up @@ -101,15 +102,16 @@ export default function useScrollCapture({
// bail early if no element is available to attach to
if (!el) return;

const notPassive = supportsPassiveEvents ? { passive: false } : false;
// all the if statements are to appease Flow 😢
if (typeof el.addEventListener === 'function') {
el.addEventListener('wheel', onWheel, false);
el.addEventListener('wheel', onWheel, notPassive);
}
if (typeof el.addEventListener === 'function') {
el.addEventListener('touchstart', onTouchStart, false);
el.addEventListener('touchstart', onTouchStart, notPassive);
}
if (typeof el.addEventListener === 'function') {
el.addEventListener('touchmove', onTouchMove, false);
el.addEventListener('touchmove', onTouchMove, notPassive);
}
},
[onTouchMove, onTouchStart, onWheel]
Expand Down
19 changes: 19 additions & 0 deletions packages/react-select/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,22 @@ export function isMobileDevice() {
return false;
}
}

// ==============================
// Passive Event Detector
// ==============================

// https://github.com/rafgraph/detect-it/blob/main/src/index.ts#L19-L36
let passiveOptionAccessed = false;
const options = {
get passive() {
return (passiveOptionAccessed = true);
},
};

if (document.addEventListener && document.removeEventListener) {
document.addEventListener('p', noop, options);
document.removeEventListener('p', noop, false);
}

export const supportsPassiveEvents: boolean = passiveOptionAccessed;

0 comments on commit 711967a

Please sign in to comment.