Skip to content

Detect React roots by checking the parent fiber #14681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/react-art/src/ReactARTHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ export function appendChildToContainer(parentInstance, child) {
child.inject(parentInstance);
}

export const appendChildToPortalContainer = appendChildToContainer;

export function insertBefore(parentInstance, child, beforeChild) {
invariant(
child !== beforeChild,
Expand Down
33 changes: 32 additions & 1 deletion packages/react-dom/src/__tests__/ReactDOMComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2677,6 +2677,23 @@ describe('ReactDOMComponent', () => {
const container = document.createElement('div');
const portalContainer = document.createElement('div');

function Component() {
return ReactDOM.createPortal(
<div onClick={() => {}} />,
portalContainer,
);
}
const root = ReactDOM.unstable_createRoot(container);
root.render(<Component />);
jest.runAllTimers();

expect(typeof portalContainer.onclick).toBe('function');
});

it('adds onclick handler to a portal root mounted via a legacy React root', () => {
const container = document.createElement('div');
const portalContainer = document.createElement('div');

function Component() {
return ReactDOM.createPortal(
<div onClick={() => {}} />,
Expand All @@ -2688,7 +2705,21 @@ describe('ReactDOMComponent', () => {
expect(typeof portalContainer.onclick).toBe('function');
});

it('does not add onclick handler to the React root', () => {
it('does not add onclick handler to a React root', () => {
const container = document.createElement('div');

function Component() {
return <div onClick={() => {}} />;
}

const root = ReactDOM.unstable_createRoot(container);
root.render(<Component />);
jest.runAllTimers();

expect(typeof container.onclick).not.toBe('function');
});

it('does not add onclick handler to a legacy React root', () => {
const container = document.createElement('div');

function Component() {
Expand Down
24 changes: 17 additions & 7 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,29 @@ export function appendChildToContainer(
parentNode = container;
parentNode.appendChild(child);
}
// This container might be used for a portal.
}

export function appendChildToPortalContainer(
container: DOMContainer,
child: Instance | TextInstance,
): void {
let parentNode;
if (container.nodeType === COMMENT_NODE) {
parentNode = (container.parentNode: any);
parentNode.insertBefore(child, container);
} else {
parentNode = container;
parentNode.appendChild(child);
}

// If something inside a portal is clicked, that click should bubble
// through the React tree. However, on Mobile Safari the click would
// never bubble through the *DOM* tree unless an ancestor with onclick
// event exists. So we wouldn't see it and dispatch it.
// This is why we ensure that non React root containers have inline onclick
// This is why we ensure that portal containers have inline onclick
// defined.
// https://github.com/facebook/react/issues/11918
const reactRootContainer = container._reactRootContainer;
if (
(reactRootContainer === null || reactRootContainer === undefined) &&
parentNode.onclick === null
) {
if (parentNode.onclick === null) {
// TODO: This cast may not be sound for SVG, MathML or custom elements.
trapClickOnNonInteractiveElement(((parentNode: any): HTMLElement));
}
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native-renderer/src/ReactNativeHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ export function appendChildToContainer(
);
}

export const appendChildToPortalContainer = appendChildToContainer;

export function commitTextUpdate(
textInstance: TextInstance,
oldText: string,
Expand Down
1 change: 1 addition & 0 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {

appendChild,
appendChildToContainer,
appendChildToPortalContainer: appendChildToContainer,
insertBefore,
insertInContainerBefore,
removeChild,
Expand Down
7 changes: 6 additions & 1 deletion packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {
commitTextUpdate,
appendChild,
appendChildToContainer,
appendChildToPortalContainer,
insertBefore,
insertInContainerBefore,
removeChild,
Expand Down Expand Up @@ -951,7 +952,11 @@ function commitPlacement(finishedWork: Fiber): void {
}
} else {
if (isContainer) {
appendChildToContainer(parent, node.stateNode);
if (parentFiber.tag === HostRoot) {
appendChildToContainer(parent, node.stateNode);
} else {
appendChildToPortalContainer(parent, node.stateNode);
}
} else {
appendChild(parent, node.stateNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ describe('ReactFiberHostContext', () => {
appendChildToContainer: function() {
return null;
},
appendChildToPortalContainer: function() {
return null;
},
supportsMutation: true,
});

Expand Down Expand Up @@ -97,6 +100,9 @@ describe('ReactFiberHostContext', () => {
appendChildToContainer: function() {
return null;
},
appendChildToPortalContainer: function() {
return null;
},
supportsMutation: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export const supportsHydration = $$$hostConfig.supportsHydration;
// -------------------
export const appendChild = $$$hostConfig.appendChild;
export const appendChildToContainer = $$$hostConfig.appendChildToContainer;
export const appendChildToPortalContainer =
$$$hostConfig.appendChildToPortalContainer;
export const commitTextUpdate = $$$hostConfig.commitTextUpdate;
export const commitMount = $$$hostConfig.commitMount;
export const commitUpdate = $$$hostConfig.commitUpdate;
Expand Down
1 change: 1 addition & 0 deletions packages/react-test-renderer/src/ReactTestHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export function resetTextContent(testElement: Instance): void {
}

export const appendChildToContainer = appendChild;
export const appendChildToPortalContainer = appendChild;
export const insertInContainerBefore = insertBefore;
export const removeChildFromContainer = removeChild;

Expand Down
1 change: 1 addition & 0 deletions packages/shared/HostConfigWithNoMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function shim(...args: any) {
export const supportsMutation = false;
export const appendChild = shim;
export const appendChildToContainer = shim;
export const appendChildToPortalContainer = shim;
export const commitTextUpdate = shim;
export const commitMount = shim;
export const commitUpdate = shim;
Expand Down