Skip to content
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

Modern Event System: revise ancestor logic #18886

Merged
merged 1 commit into from May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 18 additions & 13 deletions packages/react-dom/src/events/DOMModernPluginEventSystem.js
Expand Up @@ -417,13 +417,13 @@ export function dispatchEventForPluginEventSystem(
// sub-tree for that root and make that our ancestor instance.
let node = targetInst;

while (true) {
mainLoop: while (true) {
if (node === null) {
return;
}
const nodeTag = node.tag;
if (nodeTag === HostRoot || nodeTag === HostPortal) {
const container = node.stateNode.containerInfo;
let container = node.stateNode.containerInfo;
if (isMatchingRootContainer(container, targetContainerNode)) {
break;
}
Expand All @@ -449,18 +449,23 @@ export function dispatchEventForPluginEventSystem(
grandNode = grandNode.return;
}
}
const parentSubtreeInst = getClosestInstanceFromNode(container);
if (parentSubtreeInst === null) {
return;
}
const parentTag = parentSubtreeInst.tag;
// getClosestInstanceFromNode can return a HostRoot or SuspenseComponent.
// So we need to ensure we only set the ancestor to a HostComponent or HostText.
if (parentTag === HostComponent || parentTag === HostText) {
ancestorInst = parentSubtreeInst;
// Now we need to find it's corresponding host fiber in the other
// tree. To do this we can use getClosestInstanceFromNode, but we
// need to validate that the fiber is a host instance, otherwise
// we need to traverse up through the DOM till we find the correct
// node that is from the other tree.
while (container !== null) {
const parentNode = getClosestInstanceFromNode(container);
if (parentNode === null) {
return;
}
const parentTag = parentNode.tag;
if (parentTag === HostComponent || parentTag === HostText) {
node = ancestorInst = parentNode;
continue mainLoop;
}
container = container.parentNode;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment about which nodes you expect to see here. One issue is that this might be a HostRoot, but this can also be a SuspenseComponent of a dehydrated boundary. Not sure if this is correct in that case or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we hit a SuspenseComponent, shouldn't we also continue traversing up to find other containers as they'll still be represented correctly in the DOM, right? I tried to "break" this with dehydrated boundaries, but couldn't, so assumed the heuristcs remained consistent for this case as is for HostComponent/HostText.

}
node = parentSubtreeInst;
continue;
}
node = node.return;
}
Expand Down
Expand Up @@ -224,6 +224,95 @@ describe('DOMModernPluginEventSystem', () => {
expect(log[5]).toEqual(['bubble', buttonElement]);
});

it('handle propagation of click events between disjointed roots #2', () => {
const buttonRef = React.createRef();
const button2Ref = React.createRef();
const divRef = React.createRef();
const spanRef = React.createRef();
const log = [];
const onClick = jest.fn(e => log.push(['bubble', e.currentTarget]));
const onClickCapture = jest.fn(e =>
log.push(['capture', e.currentTarget]),
);

function Child() {
return (
<div
ref={divRef}
onClick={onClick}
onClickCapture={onClickCapture}>
Click me!
</div>
);
}

function Parent() {
return (
<button
ref={button2Ref}
onClick={onClick}
onClickCapture={onClickCapture}
/>
);
}

function GrandParent() {
return (
<button
ref={buttonRef}
onClick={onClick}
onClickCapture={onClickCapture}>
<span ref={spanRef} />
</button>
);
}

// We make a wrapper with an inner container that we
// render to. So it looks like <div><span></span></div>
// We then render to all three:
// - container
// - parentContainer
// - childContainer

const parentContainer = document.createElement('div');
const childContainer = document.createElement('div');

ReactDOM.render(<GrandParent />, container);
ReactDOM.render(<Parent />, parentContainer);
ReactDOM.render(<Child />, childContainer);

parentContainer.appendChild(childContainer);
spanRef.current.appendChild(parentContainer);

// Inside <GrandParent />
const buttonElement = buttonRef.current;
dispatchClickEvent(buttonElement);
expect(onClick).toHaveBeenCalledTimes(1);
expect(onClickCapture).toHaveBeenCalledTimes(1);
expect(log[0]).toEqual(['capture', buttonElement]);
expect(log[1]).toEqual(['bubble', buttonElement]);

// Inside <Child />
const divElement = divRef.current;
dispatchClickEvent(divElement);
expect(onClick).toHaveBeenCalledTimes(3);
expect(onClickCapture).toHaveBeenCalledTimes(3);
expect(log[2]).toEqual(['capture', divElement]);
expect(log[3]).toEqual(['bubble', divElement]);
expect(log[4]).toEqual(['capture', buttonElement]);
expect(log[5]).toEqual(['bubble', buttonElement]);

// Inside <Parent />
const buttonElement2 = button2Ref.current;
dispatchClickEvent(buttonElement2);
expect(onClick).toHaveBeenCalledTimes(5);
expect(onClickCapture).toHaveBeenCalledTimes(5);
expect(log[6]).toEqual(['capture', buttonElement2]);
expect(log[7]).toEqual(['bubble', buttonElement2]);
expect(log[8]).toEqual(['capture', buttonElement]);
expect(log[9]).toEqual(['bubble', buttonElement]);
});

it('handle propagation of click events between disjointed comment roots', () => {
const buttonRef = React.createRef();
const divRef = React.createRef();
Expand Down