From 6434e2e8164fc39190c6fb41b1bcdc5254ec3299 Mon Sep 17 00:00:00 2001 From: Joseph Stiles Date: Thu, 5 Jun 2025 10:21:54 -0600 Subject: [PATCH] fix: Use passive event listeners --- src/downshift.js | 22 +++---- .../__snapshots__/utils.test.js.snap | 60 +++++++++++++++++++ src/hooks/utils.js | 22 +++---- 3 files changed, 84 insertions(+), 20 deletions(-) diff --git a/src/downshift.js b/src/downshift.js index 49bdda0f..755b7f80 100644 --- a/src/downshift.js +++ b/src/downshift.js @@ -1133,21 +1133,23 @@ class Downshift extends Component { } const {environment} = this.props - environment.addEventListener('mousedown', onMouseDown) - environment.addEventListener('mouseup', onMouseUp) - environment.addEventListener('touchstart', onTouchStart) - environment.addEventListener('touchmove', onTouchMove) - environment.addEventListener('touchend', onTouchEnd) + const options = {passive: true} + + environment.addEventListener('mousedown', onMouseDown, options) + environment.addEventListener('mouseup', onMouseUp, options) + environment.addEventListener('touchstart', onTouchStart, options) + environment.addEventListener('touchmove', onTouchMove, options) + environment.addEventListener('touchend', onTouchEnd, options) this.cleanup = () => { this.internalClearTimeouts() this.updateStatus.cancel() - environment.removeEventListener('mousedown', onMouseDown) - environment.removeEventListener('mouseup', onMouseUp) - environment.removeEventListener('touchstart', onTouchStart) - environment.removeEventListener('touchmove', onTouchMove) - environment.removeEventListener('touchend', onTouchEnd) + environment.removeEventListener('mousedown', onMouseDown, options) + environment.removeEventListener('mouseup', onMouseUp, options) + environment.removeEventListener('touchstart', onTouchStart, options) + environment.removeEventListener('touchmove', onTouchMove, options) + environment.removeEventListener('touchend', onTouchEnd, options) } } } diff --git a/src/hooks/__tests__/__snapshots__/utils.test.js.snap b/src/hooks/__tests__/__snapshots__/utils.test.js.snap index 1f1fbc1e..c7617690 100644 --- a/src/hooks/__tests__/__snapshots__/utils.test.js.snap +++ b/src/hooks/__tests__/__snapshots__/utils.test.js.snap @@ -5,22 +5,37 @@ exports[`utils useMouseAndTouchTracker adds and removes listeners to environment [ mousedown, [Function], + { + passive: true, + }, ], [ mouseup, [Function], + { + passive: true, + }, ], [ touchstart, [Function], + { + passive: true, + }, ], [ touchmove, [Function], + { + passive: true, + }, ], [ touchend, [Function], + { + passive: true, + }, ], ] `; @@ -30,22 +45,37 @@ exports[`utils useMouseAndTouchTracker adds and removes listeners to environment [ mousedown, [Function], + { + passive: true, + }, ], [ mouseup, [Function], + { + passive: true, + }, ], [ touchstart, [Function], + { + passive: true, + }, ], [ touchmove, [Function], + { + passive: true, + }, ], [ touchend, [Function], + { + passive: true, + }, ], ] `; @@ -55,22 +85,37 @@ exports[`utils useMouseAndTouchTracker adds and removes listeners to environment [ mousedown, [Function], + { + passive: true, + }, ], [ mouseup, [Function], + { + passive: true, + }, ], [ touchstart, [Function], + { + passive: true, + }, ], [ touchmove, [Function], + { + passive: true, + }, ], [ touchend, [Function], + { + passive: true, + }, ], ] `; @@ -80,22 +125,37 @@ exports[`utils useMouseAndTouchTracker adds and removes listeners to environment [ mousedown, [Function], + { + passive: true, + }, ], [ mouseup, [Function], + { + passive: true, + }, ], [ touchstart, [Function], + { + passive: true, + }, ], [ touchmove, [Function], + { + passive: true, + }, ], [ touchend, [Function], + { + passive: true, + }, ], ] `; diff --git a/src/hooks/utils.js b/src/hooks/utils.js index b2c583c9..890b513a 100644 --- a/src/hooks/utils.js +++ b/src/hooks/utils.js @@ -418,18 +418,20 @@ function useMouseAndTouchTracker( } } - environment.addEventListener('mousedown', onMouseDown) - environment.addEventListener('mouseup', onMouseUp) - environment.addEventListener('touchstart', onTouchStart) - environment.addEventListener('touchmove', onTouchMove) - environment.addEventListener('touchend', onTouchEnd) + const options = {passive: true} + + environment.addEventListener('mousedown', onMouseDown, options) + environment.addEventListener('mouseup', onMouseUp, options) + environment.addEventListener('touchstart', onTouchStart, options) + environment.addEventListener('touchmove', onTouchMove, options) + environment.addEventListener('touchend', onTouchEnd, options) return function cleanup() { - environment.removeEventListener('mousedown', onMouseDown) - environment.removeEventListener('mouseup', onMouseUp) - environment.removeEventListener('touchstart', onTouchStart) - environment.removeEventListener('touchmove', onTouchMove) - environment.removeEventListener('touchend', onTouchEnd) + environment.removeEventListener('mousedown', onMouseDown, options) + environment.removeEventListener('mouseup', onMouseUp, options) + environment.removeEventListener('touchstart', onTouchStart, options) + environment.removeEventListener('touchmove', onTouchMove, options) + environment.removeEventListener('touchend', onTouchEnd, options) } }, [downshiftElementsRefs, environment, handleBlur])