@@ -44,6 +44,7 @@ type PressProps = {
44
44
type PointerType = '' | 'mouse' | 'keyboard' | 'pen' | 'touch' ;
45
45
46
46
type PressState = {
47
+ addedRootEvents : boolean ,
47
48
isActivePressed : boolean ,
48
49
isActivePressStart : boolean ,
49
50
isLongPressed : boolean ,
@@ -300,6 +301,7 @@ function dispatchPressEndEvents(
300
301
deactivate ( context , props , state ) ;
301
302
}
302
303
}
304
+ removeRootEventTypes ( context , state ) ;
303
305
}
304
306
305
307
function isAnchorTagElement ( eventTarget : EventTarget ) : boolean {
@@ -394,7 +396,6 @@ function unmountResponder(
394
396
) : void {
395
397
if ( state . isPressed ) {
396
398
dispatchPressEndEvents ( context , props , state ) ;
397
- context . removeRootEventTypes ( rootEventTypes ) ;
398
399
}
399
400
}
400
401
@@ -411,15 +412,35 @@ function dispatchCancel(
411
412
} else {
412
413
state . ignoreEmulatedMouseEvents = false ;
413
414
dispatchPressEndEvents ( context , props , state ) ;
414
- context . removeRootEventTypes ( rootEventTypes ) ;
415
415
}
416
416
}
417
417
}
418
418
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
+
419
439
const PressResponder = {
420
440
targetEventTypes,
421
441
createInitialState ( ) : PressState {
422
442
return {
443
+ addedRootEvents : false ,
423
444
didDispatchEvent : false ,
424
445
isActivePressed : false ,
425
446
isActivePressStart : false ,
@@ -447,7 +468,6 @@ const PressResponder = {
447
468
448
469
if ( props . disabled ) {
449
470
dispatchPressEndEvents ( context , props , state ) ;
450
- context . removeRootEventTypes ( rootEventTypes ) ;
451
471
state . ignoreEmulatedMouseEvents = false ;
452
472
return ;
453
473
}
@@ -500,7 +520,7 @@ const PressResponder = {
500
520
) ;
501
521
state . isPressWithinResponderRegion = true ;
502
522
dispatchPressStartEvents ( context , props , state ) ;
503
- context . addRootEventTypes ( rootEventTypes ) ;
523
+ addRootEventTypes ( context , state ) ;
504
524
} else {
505
525
// Prevent spacebar press from scrolling the window
506
526
if ( isValidKeyPress ( nativeEvent . key ) && nativeEvent . key === ' ' ) {
@@ -630,7 +650,6 @@ const PressResponder = {
630
650
}
631
651
}
632
652
}
633
- context . removeRootEventTypes ( rootEventTypes ) ;
634
653
} else if ( type === 'mouseup' && state . ignoreEmulatedMouseEvents ) {
635
654
state . ignoreEmulatedMouseEvents = false ;
636
655
}
0 commit comments