-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
Events not registered inside shadow dom #10422
Comments
I published a workaround as an npm package: https://www.npmjs.com/package/react-shadow-dom-retarget-events |
The workaround does not properly work with onChange. When writing <input onChange={this.handleChange} /> onChange will only be triggered on blur, which makes sense, since this is the normal behavior. Can anyone point me to how React does this so I can implement it in my workaround? |
Hey there, thanks for the Issue. My first inclination is to say that this is probably the intended behavior. Event bubbling has always been sort of an iffy encapsulation prospect, and I think the goal of the shadow-dom is to allow strict encapsulation of events. My feeling is that this falls under the same sort of category as interop with say jQueries event system (which react can't do), but I wonder what others think. @aweary @nhunzaker In terms of how React's onChange is implemented its very integrated into react's event system in the ChangeEventPlugin |
Yeah, integrating with Shadow DOM events is a hard problem. The event system registers listeners at the top-level, relying on bubbling to know when to handle an event. Since the Shadow DOM events don't bubble to the top-level, we can't see them. Assuming you want to render an isolated React application inside the Shadow DOM, we might be able to add a check detecting whether an element is in a Shadow DOM or not. Then we could register the top-level listeners on the shadow root instead, but I suspect its easier said than done. |
It would be great if we could add support for this. I'm trying to combine Web Components with React and Styled Components to create some UI elements that work standalone or in a larger React application. |
I think this is a duplicate of #9242. |
This should be fixed in React 17 RC, at least for the general case. |
Hehe, so React 17 really does something! 😉 |
PS: Thanks ✌️ |
As it says here: https://reactjs.org/docs/web-components.html > Events emitted by a Web Component may not properly propagate through a > React render tree. You will need to manually attach event handlers to > handle these events within your React components. The advice is to do manual event management. Yuck! This blog: https://itnext.io/react-and-web-components-3e0fca98a593 Explains why: > Another feature of the Shadow DOM is that events are retargeted and > encapsulated in the shadow. > > This is the intended behavior of a Shadow DOM, however, this conflicts > with how React works. And it suggests installing this hack: https://github.com/spring-media/react-shadow-dom-retarget-events Fortunately, everything works in React 17: facebook/react#10422 (comment) As explained here, they got rid of the delegation: https://reactjs.org/blog/2020/08/10/react-v17-rc.html
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
When you render a react component inside a shadow DOM, events will not be dispatched in react. I.e. you do something like this:
but nothing happens. Someone investigated this already, got to the bottom of it and wrote a work around: https://stackoverflow.com/questions/37866237/click-event-not-firing-when-react-component-in-a-shadow-dom
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.
Following the official docs (https://facebook.github.io/react/docs/web-components.html ):
https://jsfiddle.net/84v837e9/187/
What is the expected behavior?
Events should be dispatched even when the react app is rendered in a shadow dom
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
All browsers, tested with react 15.
The text was updated successfully, but these errors were encountered: