GeezerVison is showing github.com — Keep onTouchStart, onTouchMove, and onWheel passive (#19654) · react/react@dd651df
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/DOMPluginEventSystem.js +18-2Lines changed: 18 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,6 +53,7 @@ import { | 53 | 53 | enableLegacyFBSupport, | 54 | 54 | enableCreateEventHandleAPI, | 55 | 55 | enableScopeAPI, | | 56 | + enablePassiveEventIntervention, | 56 | 57 | } from 'shared/ReactFeatureFlags'; | 57 | 58 | import { | 58 | 59 | invokeGuardedCallbackAndCatchFirstError, | @@ -342,6 +343,21 @@ export function listenToNativeEvent( | 342 | 343 | if (domEventName === 'selectionchange') { | 343 | 344 | target = (rootContainerElement: any).ownerDocument; | 344 | 345 | } | | 346 | + if (enablePassiveEventIntervention && isPassiveListener === undefined) { | | 347 | + // Browsers introduced an intervention, making these events | | 348 | + // passive by default on document. React doesn't bind them | | 349 | + // to document anymore, but changing this now would undo | | 350 | + // the performance wins from the change. So we emulate | | 351 | + // the existing behavior manually on the roots now. | | 352 | + // https://github.com/facebook/react/issues/19651 | | 353 | + if ( | | 354 | + domEventName === 'touchstart' || | | 355 | + domEventName === 'touchmove' || | | 356 | + domEventName === 'wheel' | | 357 | + ) { | | 358 | + isPassiveListener = true; | | 359 | + } | | 360 | + } | 345 | 361 | // If the event can be delegated (or is capture phase), we can | 346 | 362 | // register it to the root container. Otherwise, we should | 347 | 363 | // register the event to the target element and mark it as | @@ -506,7 +522,7 @@ function addTrappedEventListener( | 506 | 522 | }; | 507 | 523 | } | 508 | 524 | if (isCapturePhaseListener) { | 509 | | - if (enableCreateEventHandleAPI && isPassiveListener !== undefined) { | | 525 | + if (isPassiveListener !== undefined) { | 510 | 526 | unsubscribeListener = addEventCaptureListenerWithPassiveFlag( | 511 | 527 | targetContainer, | 512 | 528 | domEventName, | @@ -521,7 +537,7 @@ function addTrappedEventListener( | 521 | 537 | ); | 522 | 538 | } | 523 | 539 | } else { | 524 | | - if (enableCreateEventHandleAPI && isPassiveListener !== undefined) { | | 540 | + if (isPassiveListener !== undefined) { | 525 | 541 | unsubscribeListener = addEventBubbleListenerWithPassiveFlag( | 526 | 542 | targetContainer, | 527 | 543 | domEventName, | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/checkPassiveEvents.js +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | */ | 9 | 9 |
| 10 | 10 | import {canUseDOM} from 'shared/ExecutionEnvironment'; | 11 | | -import {enableCreateEventHandleAPI} from 'shared/ReactFeatureFlags'; | 12 | 11 |
| 13 | 12 | export let passiveBrowserEventsSupported = false; | 14 | 13 |
| 15 | 14 | // Check if browser support events with passive listeners | 16 | 15 | // https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support | 17 | | -if (enableCreateEventHandleAPI && canUseDOM) { | | 16 | +if (canUseDOM) { | 18 | 17 | try { | 19 | 18 | const options = {}; | 20 | 19 | // $FlowFixMe: Ignore Flow complaining about needing a value | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/plugins/__tests__/SimpleEventPlugin-test.js +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -504,5 +504,44 @@ describe('SimpleEventPlugin', function() { | 504 | 504 |
| 505 | 505 | expect(onClick).toHaveBeenCalledTimes(0); | 506 | 506 | }); | | 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 | + if (gate(flags => flags.enablePassiveEventIntervention)) { | | 541 | + expect(passiveEvents).toEqual(['touchstart', 'touchmove', 'wheel']); | | 542 | + } else { | | 543 | + expect(passiveEvents).toEqual([]); | | 544 | + } | | 545 | + }); | 507 | 546 | }); | 508 | 547 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/ReactFeatureFlags.js +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -132,3 +132,6 @@ export const deferRenderPhaseUpdateToNextBatch = true; | 132 | 132 | export const decoupleUpdatePriorityFromScheduler = false; | 133 | 133 |
| 134 | 134 | export const enableDiscreteEventFlushingChange = false; | | 135 | + | | 136 | +// https://github.com/facebook/react/pull/19654 | | 137 | +export const enablePassiveEventIntervention = true; |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.native-fb.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -49,6 +49,7 @@ export const enableNewReconciler = false; | 49 | 49 | export const deferRenderPhaseUpdateToNextBatch = true; | 50 | 50 | export const decoupleUpdatePriorityFromScheduler = false; | 51 | 51 | export const enableDiscreteEventFlushingChange = false; | | 52 | +export const enablePassiveEventIntervention = true; | 52 | 53 |
| 53 | 54 | // Flow magic to verify the exports of this file match the original version. | 54 | 55 | // eslint-disable-next-line no-unused-vars | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.native-oss.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,6 +48,7 @@ export const enableNewReconciler = false; | 48 | 48 | export const deferRenderPhaseUpdateToNextBatch = true; | 49 | 49 | export const decoupleUpdatePriorityFromScheduler = false; | 50 | 50 | export const enableDiscreteEventFlushingChange = false; | | 51 | +export const enablePassiveEventIntervention = true; | 51 | 52 |
| 52 | 53 | // Flow magic to verify the exports of this file match the original version. | 53 | 54 | // eslint-disable-next-line no-unused-vars | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.test-renderer.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,6 +48,7 @@ export const enableNewReconciler = false; | 48 | 48 | export const deferRenderPhaseUpdateToNextBatch = true; | 49 | 49 | export const decoupleUpdatePriorityFromScheduler = false; | 50 | 50 | export const enableDiscreteEventFlushingChange = false; | | 51 | +export const enablePassiveEventIntervention = true; | 51 | 52 |
| 52 | 53 | // Flow magic to verify the exports of this file match the original version. | 53 | 54 | // eslint-disable-next-line no-unused-vars | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.test-renderer.native.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,6 +48,7 @@ export const enableNewReconciler = false; | 48 | 48 | export const deferRenderPhaseUpdateToNextBatch = true; | 49 | 49 | export const decoupleUpdatePriorityFromScheduler = false; | 50 | 50 | export const enableDiscreteEventFlushingChange = false; | | 51 | +export const enablePassiveEventIntervention = true; | 51 | 52 |
| 52 | 53 | // Flow magic to verify the exports of this file match the original version. | 53 | 54 | // eslint-disable-next-line no-unused-vars | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.test-renderer.www.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,6 +48,7 @@ export const enableNewReconciler = false; | 48 | 48 | export const deferRenderPhaseUpdateToNextBatch = true; | 49 | 49 | export const decoupleUpdatePriorityFromScheduler = false; | 50 | 50 | export const enableDiscreteEventFlushingChange = false; | | 51 | +export const enablePassiveEventIntervention = true; | 51 | 52 |
| 52 | 53 | // Flow magic to verify the exports of this file match the original version. | 53 | 54 | // eslint-disable-next-line no-unused-vars | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.testing.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,6 +48,7 @@ export const enableNewReconciler = false; | 48 | 48 | export const deferRenderPhaseUpdateToNextBatch = true; | 49 | 49 | export const decoupleUpdatePriorityFromScheduler = false; | 50 | 50 | export const enableDiscreteEventFlushingChange = false; | | 51 | +export const enablePassiveEventIntervention = true; | 51 | 52 |
| 52 | 53 | // Flow magic to verify the exports of this file match the original version. | 53 | 54 | // eslint-disable-next-line no-unused-vars | |
|