GeezerVison is showing github.com — [react-interactions] Add Portal propagation configuration (#16889) · react/react@d6d83d7
File treeExpand file treeCollapse file tree Open diff view settings react-interactions/events/src/dom
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/DOMEventResponderSystem.js +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,7 +12,7 @@ import { | 12 | 12 | PASSIVE_NOT_SUPPORTED, | 13 | 13 | } from 'legacy-events/EventSystemFlags'; | 14 | 14 | import type {AnyNativeEvent} from 'legacy-events/PluginModuleType'; | 15 | | -import {HostComponent, ScopeComponent} from 'shared/ReactWorkTags'; | | 15 | +import {HostComponent, ScopeComponent, HostPortal} from 'shared/ReactWorkTags'; | 16 | 16 | import type {EventPriority} from 'shared/ReactTypes'; | 17 | 17 | import type { | 18 | 18 | ReactDOMEventResponder, | @@ -451,9 +451,12 @@ function traverseAndHandleEventResponderInstances( | 451 | 451 | isPassiveSupported, | 452 | 452 | ); | 453 | 453 | let node = targetFiber; | | 454 | + let insidePortal = false; | 454 | 455 | while (node !== null) { | 455 | 456 | const {dependencies, tag} = node; | 456 | | - if ( | | 457 | + if (tag === HostPortal) { | | 458 | + insidePortal = true; | | 459 | + } else if ( | 457 | 460 | (tag === HostComponent || tag === ScopeComponent) && | 458 | 461 | dependencies !== null | 459 | 462 | ) { | @@ -465,7 +468,8 @@ function traverseAndHandleEventResponderInstances( | 465 | 468 | const {props, responder, state} = responderInstance; | 466 | 469 | if ( | 467 | 470 | !visitedResponders.has(responder) && | 468 | | - validateResponderTargetEventTypes(eventType, responder) | | 471 | + validateResponderTargetEventTypes(eventType, responder) && | | 472 | + (!insidePortal || responder.targetPortalPropagation) | 469 | 473 | ) { | 470 | 474 | visitedResponders.add(responder); | 471 | 475 | const onEvent = responder.onEvent; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/events/__tests__/DOMEventResponderSystem-test.internal.js +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,7 @@ function createEventResponder({ | 28 | 28 | onMount, | 29 | 29 | onUnmount, | 30 | 30 | getInitialState, | | 31 | + targetPortalPropagation, | 31 | 32 | }) { | 32 | 33 | return React.unstable_createResponder('TestEventResponder', { | 33 | 34 | targetEventTypes, | @@ -37,6 +38,7 @@ function createEventResponder({ | 37 | 38 | onMount, | 38 | 39 | onUnmount, | 39 | 40 | getInitialState, | | 41 | + targetPortalPropagation, | 40 | 42 | }); | 41 | 43 | } | 42 | 44 |
| @@ -1034,4 +1036,51 @@ describe('DOMEventResponderSystem', () => { | 1034 | 1036 | }, | 1035 | 1037 | ]); | 1036 | 1038 | }); | | 1039 | + | | 1040 | + it('should not propagate target events through portals by default', () => { | | 1041 | + const buttonRef = React.createRef(); | | 1042 | + const onEvent = jest.fn(); | | 1043 | + const TestResponder = createEventResponder({ | | 1044 | + targetEventTypes: ['click'], | | 1045 | + onEvent, | | 1046 | + }); | | 1047 | + const domNode = document.createElement('div'); | | 1048 | + document.body.appendChild(domNode); | | 1049 | + const Component = () => { | | 1050 | + const listener = React.unstable_useResponder(TestResponder, {}); | | 1051 | + return ( | | 1052 | + <div listeners={listener}> | | 1053 | + {ReactDOM.createPortal(<button ref={buttonRef} />, domNode)} | | 1054 | + </div> | | 1055 | + ); | | 1056 | + }; | | 1057 | + ReactDOM.render(<Component />, container); | | 1058 | + dispatchClickEvent(buttonRef.current); | | 1059 | + document.body.removeChild(domNode); | | 1060 | + expect(onEvent).not.toBeCalled(); | | 1061 | + }); | | 1062 | + | | 1063 | + it('should propagate target events through portals when enabled', () => { | | 1064 | + const buttonRef = React.createRef(); | | 1065 | + const onEvent = jest.fn(); | | 1066 | + const TestResponder = createEventResponder({ | | 1067 | + targetPortalPropagation: true, | | 1068 | + targetEventTypes: ['click'], | | 1069 | + onEvent, | | 1070 | + }); | | 1071 | + const domNode = document.createElement('div'); | | 1072 | + document.body.appendChild(domNode); | | 1073 | + const Component = () => { | | 1074 | + const listener = React.unstable_useResponder(TestResponder, {}); | | 1075 | + return ( | | 1076 | + <div listeners={listener}> | | 1077 | + {ReactDOM.createPortal(<button ref={buttonRef} />, domNode)} | | 1078 | + </div> | | 1079 | + ); | | 1080 | + }; | | 1081 | + ReactDOM.render(<Component />, container); | | 1082 | + dispatchClickEvent(buttonRef.current); | | 1083 | + document.body.removeChild(domNode); | | 1084 | + expect(onEvent).toBeCalled(); | | 1085 | + }); | 1037 | 1086 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/events/src/dom/Focus.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -293,6 +293,7 @@ function unmountFocusResponder( | 293 | 293 |
| 294 | 294 | const focusResponderImpl = { | 295 | 295 | targetEventTypes, | | 296 | + targetPortalPropagation: true, | 296 | 297 | rootEventTypes, | 297 | 298 | getInitialState(): FocusState { | 298 | 299 | return { | @@ -430,6 +431,7 @@ function unmountFocusWithinResponder( | 430 | 431 |
| 431 | 432 | const focusWithinResponderImpl = { | 432 | 433 | targetEventTypes, | | 434 | + targetPortalPropagation: true, | 433 | 435 | rootEventTypes, | 434 | 436 | getInitialState(): FocusState { | 435 | 437 | return { | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-interactions/events/src/dom/Keyboard.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -180,6 +180,7 @@ function dispatchKeyboardEvent( | 180 | 180 |
| 181 | 181 | const keyboardResponderImpl = { | 182 | 182 | targetEventTypes, | | 183 | + targetPortalPropagation: true, | 183 | 184 | getInitialState(): KeyboardState { | 184 | 185 | return { | 185 | 186 | isActive: false, | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/ReactTypes.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -96,6 +96,7 @@ export type ReactEventResponder<E, C> = { | 96 | 96 | $$typeof: Symbol | number, | 97 | 97 | displayName: string, | 98 | 98 | targetEventTypes: null | Array<string>, | | 99 | + targetPortalPropagation: boolean, | 99 | 100 | rootEventTypes: null | Array<string>, | 100 | 101 | getInitialState: null | ((props: Object) => Object), | 101 | 102 | onEvent: | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/createEventResponder.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,6 +22,7 @@ export default function createEventResponder<E, C>( | 22 | 22 | onRootEvent, | 23 | 23 | rootEventTypes, | 24 | 24 | targetEventTypes, | | 25 | + targetPortalPropagation, | 25 | 26 | } = responderConfig; | 26 | 27 | const eventResponder = { | 27 | 28 | $$typeof: REACT_RESPONDER_TYPE, | @@ -33,6 +34,7 @@ export default function createEventResponder<E, C>( | 33 | 34 | onUnmount: onUnmount || null, | 34 | 35 | rootEventTypes: rootEventTypes || null, | 35 | 36 | targetEventTypes: targetEventTypes || null, | | 37 | + targetPortalPropagation: targetPortalPropagation || false, | 36 | 38 | }; | 37 | 39 | // We use responder as a Map key later on. When we have a bad | 38 | 40 | // polyfill, then we can't use it as a key as the polyfill tries | |
|