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

Bug: Events in shadow dom get duplicated when bubbling #24136

Open
WorldSEnder opened this issue Mar 21, 2022 · 2 comments
Open

Bug: Events in shadow dom get duplicated when bubbling #24136

WorldSEnder opened this issue Mar 21, 2022 · 2 comments
Labels
Component: DOM Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Bug

Comments

@WorldSEnder
Copy link

Events dispatching from inside a shadow-root will be handled multiple times while bubbling up the virtual DOM tree. I suspect this is because of event retargeting when crossing the boundary of the shadow dom.

React version: 17.0+, including 18-rc2

Steps To Reproduce

Click on the button in the code example below:

https://codesandbox.io/s/react-shadow-dom-event-doubling-msgnj1

The current behavior

onDivClicked is called twice per click on the button.

The expected behavior

onDivClicked should only be called once per handled click.

@WorldSEnder WorldSEnder added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Mar 21, 2022
@eps1lon
Copy link
Collaborator

eps1lon commented Mar 21, 2022

Thanks for the repro!

One native event is targetted at the shadow root child that bubbles to the shadow root. And another is targetted at the shadow root itself and bubbles from there (https://codesandbox.io/s/react-shadow-dom-event-doubling-msgnj1).

Since the shadow root is a Portal, React will bubble up a synthetic event from there even though it shouldn't because the browser is redirecting the native event with a different target.

So in this specific case React Portals really should stop propagation of events targetted at shadow root children because the browser will dispatch a new event with the shadow root itself as a target

@vjee
Copy link

vjee commented Jan 5, 2023

I ran into the same problem and before discovering this issue made a similar repro.
I'll leave it here just in case.
https://codesandbox.io/s/blissful-mclaren-nci1dr

At the moment, stopping propagation in the event handler seems to be a good workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: DOM Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug Type: Bug
Projects
None yet
Development

No branches or pull requests

3 participants