Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: [Violation] Added non-passive event listener to a scroll-blocking 'wheel' event #24

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 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: 22 additions & 0 deletions packages/react-native-web/src/modules/canUsePassive/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Allows us to identify whether the browser supports passive event listener.
* Because older browsers will interpret any object in the 3rd argument of an event listener as capture=true.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: Don't think this line is necessary

Suggested change
* Because older browsers will interpret any object in the 3rd argument of an event listener as capture=true.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

*
* @returns {Boolean}
*/

export default function canUsePassive() {
let supportsPassive = false;
try {
const opts = Object.defineProperty({}, 'passive', {
// eslint-disable-next-line getter-return
get() {
supportsPassive = true;
}
});
window.addEventListener('testPassive', null, opts);
window.removeEventListener('testPassive', null, opts);
// eslint-disable-next-line no-empty
} catch (e) {}
return supportsPassive;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
type ChildListState,
type ListDebugInfo,
} from './VirtualizedListContext.js';
import canUsePassive from '../../../modules/canUsePassive';

type Item = any;

Expand Down Expand Up @@ -413,6 +414,8 @@ function getItemKey(props: Props, index: number): ?string {
return extractKey(props, item, 0);
}

const supportsPassive = canUsePassive();

/**
* Base implementation for the more convenient [`<FlatList>`](https://reactnative.dev/docs/flatlist)
* and [`<SectionList>`](https://reactnative.dev/docs/sectionlist) components, which are also better
Expand Down Expand Up @@ -829,7 +832,6 @@ class VirtualizedList extends React.PureComponent<Props, State> {
ev.target.scrollTop += targetDelta;
node.scrollTop -= leftoverDelta;
}
ev.preventDefault();
}
};

Expand Down Expand Up @@ -880,7 +882,8 @@ class VirtualizedList extends React.PureComponent<Props, State> {
setupWebWheelHandler() {
if (this._scrollRef && this._scrollRef.getScrollableNode) {
this._scrollRef.getScrollableNode().addEventListener('wheel',
this.invertedWheelEventHandler
this.invertedWheelEventHandler,
supportsPassive ? { passive: true } : false
);
} else {
setTimeout(() => this.setupWebWheelHandler(), 50);
Expand Down