Add guard around FocusWithin responder root events (#18080)
Dominic Gannaway committed
Feb 19, 2020 at 18:32 UTC
b6c94d636cb33a265671b864b97870da38d97207
1 file changed
+10
-3
packages/react-interactions/events/src/dom/Focus.js
+10
-3
@@ -36,7 +36,7 @@ type FocusState = {
36
isFocused: boolean,
37
isFocusVisible: boolean,
38
pointerType: PointerType,
39
- ...
39
+ addedRootEvents?: boolean,
40
};
41
42
type FocusProps = {
@@ -416,6 +416,7 @@ const focusResponderImpl = {
416
isFocused: false,
417
isFocusVisible: false,
418
pointerType: '',
419
+ addedRootEvents: false,
420
};
421
},
422
onMount() {
@@ -622,7 +623,10 @@ const focusWithinResponderImpl = {
623
onBeforeBlurWithin,
624
DiscreteEvent,
625
);
625
- context.addRootEventTypes(rootEventTypes);
626
+ if (!state.addedRootEvents) {
627
+ state.addedRootEvents = true;
628
+ context.addRootEventTypes(rootEventTypes);
629
+ }
630
} else {
631
// We want to propagate to next focusWithin responder
632
// if this responder doesn't handle beforeblur
@@ -660,7 +664,10 @@ const focusWithinResponderImpl = {
664
if (detachedTarget !== null && detachedTarget === event.target) {
665
dispatchBlurWithinEvents(context, event, props, state);
666
state.detachedTarget = null;
663
- context.removeRootEventTypes(rootEventTypes);
667
+ if (state.addedRootEvents) {
668
+ state.addedRootEvents = false;
669
+ context.removeRootEventTypes(rootEventTypes);
670
+ }
671
}
672
}
673
},