@@ -44,6 +44,7 @@ type PressProps = {
4444type PointerType = '' | 'mouse' | 'keyboard' | 'pen' | 'touch' ;
4545
4646type PressState = {
47+ addedRootEvents : boolean ,
4748 isActivePressed : boolean ,
4849 isActivePressStart : boolean ,
4950 isLongPressed : boolean ,
@@ -300,6 +301,7 @@ function dispatchPressEndEvents(
300301 deactivate ( context , props , state ) ;
301302 }
302303 }
304+ removeRootEventTypes ( context , state ) ;
303305}
304306
305307function isAnchorTagElement ( eventTarget : EventTarget ) : boolean {
@@ -394,7 +396,6 @@ function unmountResponder(
394396) : void {
395397 if ( state . isPressed ) {
396398 dispatchPressEndEvents ( context , props , state ) ;
397- context . removeRootEventTypes ( rootEventTypes ) ;
398399 }
399400}
400401
@@ -411,15 +412,35 @@ function dispatchCancel(
411412 } else {
412413 state . ignoreEmulatedMouseEvents = false ;
413414 dispatchPressEndEvents ( context , props , state ) ;
414- context . removeRootEventTypes ( rootEventTypes ) ;
415415 }
416416 }
417417}
418418
419+ function addRootEventTypes (
420+ context : ReactResponderContext ,
421+ state : PressState ,
422+ ) : void {
423+ if ( ! state . addedRootEvents ) {
424+ state . addedRootEvents = true ;
425+ context . addRootEventTypes ( rootEventTypes ) ;
426+ }
427+ }
428+
429+ function removeRootEventTypes (
430+ context : ReactResponderContext ,
431+ state : PressState ,
432+ ) : void {
433+ if ( state . addedRootEvents ) {
434+ state . addedRootEvents = false ;
435+ context . removeRootEventTypes ( rootEventTypes ) ;
436+ }
437+ }
438+
419439const PressResponder = {
420440 targetEventTypes,
421441 createInitialState ( ) : PressState {
422442 return {
443+ addedRootEvents : false ,
423444 didDispatchEvent : false ,
424445 isActivePressed : false ,
425446 isActivePressStart : false ,
@@ -447,7 +468,6 @@ const PressResponder = {
447468
448469 if ( props . disabled ) {
449470 dispatchPressEndEvents ( context , props , state ) ;
450- context . removeRootEventTypes ( rootEventTypes ) ;
451471 state . ignoreEmulatedMouseEvents = false ;
452472 return ;
453473 }
@@ -500,7 +520,7 @@ const PressResponder = {
500520 ) ;
501521 state . isPressWithinResponderRegion = true ;
502522 dispatchPressStartEvents ( context , props , state ) ;
503- context . addRootEventTypes ( rootEventTypes ) ;
523+ addRootEventTypes ( context , state ) ;
504524 } else {
505525 // Prevent spacebar press from scrolling the window
506526 if ( isValidKeyPress ( nativeEvent . key ) && nativeEvent . key === ' ' ) {
@@ -630,7 +650,6 @@ const PressResponder = {
630650 }
631651 }
632652 }
633- context . removeRootEventTypes ( rootEventTypes ) ;
634653 } else if ( type === 'mouseup' && state . ignoreEmulatedMouseEvents ) {
635654 state . ignoreEmulatedMouseEvents = false ;
636655 }
0 commit comments