Skip to content

Commit

Permalink
Modern Event System: register onMouseEnter for portals (#18720)
Browse files Browse the repository at this point in the history
  • Loading branch information
trueadm authored Apr 23, 2020
1 parent 4e3545f commit 30cee2f
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/react-art/src/ReactARTHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,7 @@ export function beforeActiveInstanceBlur() {
export function afterActiveInstanceBlur() {
// noop
}

export function preparePortalMount(portalInstance: any): void {
// noop
}
8 changes: 8 additions & 0 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ import {
enableSuspenseServerRenderer,
enableDeprecatedFlareAPI,
enableFundamentalAPI,
enableModernEventSystem,
} from 'shared/ReactFeatureFlags';
import {TOP_BEFORE_BLUR, TOP_AFTER_BLUR} from '../events/DOMTopLevelEventTypes';
import {listenToEvent} from '../events/DOMModernPluginEventSystem';

export type Type = string;
export type Props = {
Expand Down Expand Up @@ -1098,3 +1100,9 @@ export function makeOpaqueHydratingObject(
valueOf: attemptToReadValue,
};
}

export function preparePortalMount(portalInstance: Instance): void {
if (enableModernEventSystem) {
listenToEvent('onMouseEnter', portalInstance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,30 @@ describe('EnterLeaveEventPlugin', () => {

ReactDOM.render(<Parent />, container);
});

it('should work with portals outside of the root', () => {
const divRef = React.createRef();
const onMouseLeave = jest.fn();

function Component() {
return (
<div onMouseLeave={onMouseLeave}>
{ReactDOM.createPortal(<div ref={divRef} />, document.body)}
</div>
);
}

ReactDOM.render(<Component />, container);

// Leave from the portal div
divRef.current.dispatchEvent(
new MouseEvent('mouseout', {
bubbles: true,
cancelable: true,
relatedTarget: document.body,
}),
);

expect(onMouseLeave).toHaveBeenCalledTimes(1);
});
});
4 changes: 4 additions & 0 deletions packages/react-native-renderer/src/ReactFabricHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,7 @@ export function beforeActiveInstanceBlur() {
export function afterActiveInstanceBlur() {
// noop
}

export function preparePortalMount(portalInstance: Instance): void {
// noop
}
4 changes: 4 additions & 0 deletions packages/react-native-renderer/src/ReactNativeHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,7 @@ export function beforeActiveInstanceBlur() {
export function afterActiveInstanceBlur() {
// noop
}

export function preparePortalMount(portalInstance: Instance): void {
// noop
}
4 changes: 4 additions & 0 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
afterActiveInstanceBlur() {
// NO-OP
},

preparePortalMount() {
// NO-OP
},
};

const hostConfig = useMutation
Expand Down
4 changes: 4 additions & 0 deletions packages/react-reconciler/src/ReactFiberCompleteWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
mountFundamentalComponent,
cloneFundamentalInstance,
shouldUpdateFundamentalComponent,
preparePortalMount,
} from './ReactFiberHostConfig';
import {
getRootHostContainer,
Expand Down Expand Up @@ -973,6 +974,9 @@ function completeWork(
case HostPortal:
popHostContainer(workInProgress);
updateHostContainer(workInProgress);
if (current === null) {
preparePortalMount(workInProgress.stateNode.containerInfo);
}
return null;
case ContextProvider:
// Pop provider fiber
Expand Down
4 changes: 4 additions & 0 deletions packages/react-reconciler/src/ReactFiberCompleteWork.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
mountFundamentalComponent,
cloneFundamentalInstance,
shouldUpdateFundamentalComponent,
preparePortalMount,
} from './ReactFiberHostConfig';
import {
getRootHostContainer,
Expand Down Expand Up @@ -973,6 +974,9 @@ function completeWork(
case HostPortal:
popHostContainer(workInProgress);
updateHostContainer(workInProgress);
if (current === null) {
preparePortalMount(workInProgress.stateNode.containerInfo);
}
return null;
case ContextProvider:
// Pop provider fiber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const makeClientIdInDEV = $$$hostConfig.makeClientIdInDEV;
export const makeServerId = $$$hostConfig.makeServerId;
export const beforeActiveInstanceBlur = $$$hostConfig.beforeActiveInstanceBlur;
export const afterActiveInstanceBlur = $$$hostConfig.afterActiveInstanceBlur;
export const preparePortalMount = $$$hostConfig.preparePortalMount;

// -------------------
// Mutation
Expand Down
4 changes: 4 additions & 0 deletions packages/react-test-renderer/src/ReactTestHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,7 @@ export function beforeActiveInstanceBlur() {
export function afterActiveInstanceBlur() {
// noop
}

export function preparePortalMount(portalInstance: Instance): void {
// noop
}

0 comments on commit 30cee2f

Please sign in to comment.