Skip to content

Commit a6cf3d3

Browse files
committed
Keep onTouchStart, onTouchMove, and onWheel passive
1 parent 87b3e2d commit a6cf3d3

3 files changed

Lines changed: 53 additions & 4 deletions

File tree

packages/react-dom/src/events/DOMPluginEventSystem.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,21 @@ export function listenToNativeEvent(
342342
if (domEventName === 'selectionchange') {
343343
target = (rootContainerElement: any).ownerDocument;
344344
}
345+
if (isPassiveListener === undefined) {
346+
// Browsers introduced an intervention, making these events
347+
// passive by default on document. React doesn't bind them
348+
// to document anymore, but changing this now would undo
349+
// the performance wins from the change. So we emulate
350+
// the existing behavior manually on the roots now.
351+
// https://github.com/facebook/react/issues/19651
352+
if (
353+
domEventName === 'touchstart' ||
354+
domEventName === 'touchmove' ||
355+
domEventName === 'wheel'
356+
) {
357+
isPassiveListener = true;
358+
}
359+
}
345360
// If the event can be delegated (or is capture phase), we can
346361
// register it to the root container. Otherwise, we should
347362
// register the event to the target element and mark it as
@@ -506,7 +521,7 @@ function addTrappedEventListener(
506521
};
507522
}
508523
if (isCapturePhaseListener) {
509-
if (enableCreateEventHandleAPI && isPassiveListener !== undefined) {
524+
if (isPassiveListener !== undefined) {
510525
unsubscribeListener = addEventCaptureListenerWithPassiveFlag(
511526
targetContainer,
512527
domEventName,
@@ -521,7 +536,7 @@ function addTrappedEventListener(
521536
);
522537
}
523538
} else {
524-
if (enableCreateEventHandleAPI && isPassiveListener !== undefined) {
539+
if (isPassiveListener !== undefined) {
525540
unsubscribeListener = addEventBubbleListenerWithPassiveFlag(
526541
targetContainer,
527542
domEventName,

packages/react-dom/src/events/checkPassiveEvents.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
*/
99

1010
import {canUseDOM} from 'shared/ExecutionEnvironment';
11-
import {enableCreateEventHandleAPI} from 'shared/ReactFeatureFlags';
1211

1312
export let passiveBrowserEventsSupported = false;
1413

1514
// Check if browser support events with passive listeners
1615
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
17-
if (enableCreateEventHandleAPI && canUseDOM) {
16+
if (canUseDOM) {
1817
try {
1918
const options = {};
2019
// $FlowFixMe: Ignore Flow complaining about needing a value

packages/react-dom/src/events/plugins/__tests__/SimpleEventPlugin-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,5 +504,40 @@ describe('SimpleEventPlugin', function() {
504504

505505
expect(onClick).toHaveBeenCalledTimes(0);
506506
});
507+
508+
it('registers passive handlers for events affected by the intervention', () => {
509+
container = document.createElement('div');
510+
511+
const passiveEvents = [];
512+
const nativeAddEventListener = container.addEventListener;
513+
container.addEventListener = function(type, fn, options) {
514+
if (options !== null && typeof options === 'object') {
515+
if (options.passive) {
516+
passiveEvents.push(type);
517+
}
518+
}
519+
return nativeAddEventListener.apply(this, arguments);
520+
};
521+
522+
ReactDOM.render(
523+
<div
524+
// Affected by the intervention:
525+
// https://github.com/facebook/react/issues/19651
526+
onTouchStart={() => {}}
527+
onTouchMove={() => {}}
528+
onWheel={() => {}}
529+
// A few events that should be unaffected:
530+
onClick={() => {}}
531+
onScroll={() => {}}
532+
onTouchEnd={() => {}}
533+
onChange={() => {}}
534+
onPointerDown={() => {}}
535+
onPointerMove={() => {}}
536+
/>,
537+
container,
538+
);
539+
540+
expect(passiveEvents).toEqual(['touchstart', 'touchmove', 'wheel']);
541+
});
507542
});
508543
});

0 commit comments

Comments
 (0)