-
Notifications
You must be signed in to change notification settings - Fork 47.6k
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
Event Handler on React Component not invoked when React Component is rendered inside a Web Component #9242
Comments
@nilshartmann Should path be composedPath() instead? https://hayato.io/2016/shadowdomv1/#getting-event-path By the way, this almost entirely solves an issue I was having where events were not being captured by a web component that encapsulated a ReactDOM. |
@DeanBDean You are right. We were using Chrome to investigate the event bubbling issue months ago, which gave us access to the event.path property. We are using the webcomponentsjs polyfill to make the shadow dom available in all browsers. Today the polyfill (shadyDom) provides us the composedPath() function, so we should maybe change it to the following: // If encapsulated in a Web Component use the composed Event path
if(nativeEvent.composedPath && nativeEvent.composedPath()) {
return nativeEvent.composedPath()[0];
} |
I'm having the same problem, is there a workaround for this currently? |
We should confirm whether it still happens on master with native web components. |
First, I have also observed this first hand with React 16.0.0 (today). Also confirmed that a fellow opened up a related issue and wrote a working temporary fix, https://www.npmjs.com/package/react-shadow-dom-retarget-events . While I do not see a use case for web components in my React applications, I am considering using React in building more complicated web components to be delivered to other platforms (I am thinking for WordPress bloggers; or similar). |
Same problem here... Created component with react and want to include it to other sites - similar as iframe. The only way it can work is to retarget events. But I think it realy hurts performace |
Does React use event delegation to listen for clicks on React components? If so I imagine the shadow boundary would get in the way of that... Just want to make sure I understand the problem :) |
The react-shadow-dom-retarget-events module works, but has a few problems of its own. If I'm correct, it picks up the native events on the shadow dom element, and then directly calls the react component event handlers (onClick, etc.. ) using those native events, instead of synthetic events. I think that because of this, stopPropagation() does not work. It would be nice to see the React team pick this issue up and provide a proper fix. Shadow dom is gaining momentum! :-) I've been thinking and maybe it would make sense to attach the global React event handlers to the element returned by getRootNode() instead of to the document? https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode |
Is there interest in a fix based on the suggestions by @nilshartmann? I'd be happy to contribute the required changes and new tests if required. Edit: Reading the lengthy discussion of #8117, which was the last implementation attempt for #2043, I guess that redesigning the event propagation system to support multiple roots is a sprawling/hard to implement change. |
Tested successfully using the |
Not sure if this is still an issue with anyone; I however found the following addition to your react app entry point will work:
The rationale here is:
This works with the create-react-app setup. Any issues one can foresee taking this approach? |
if my assumptions are correct; this issue is not a problem or an issue on the react side at least. But what could be cool is for This looks interesting and similar though: #13713 |
@renegare Your approach seemed to work for me except that there were a couple of other APIs that react depended on that I needed to add to the shadow root:
|
I think this beats maintaining a list of events. Maintain a list of document methods ... but as you can see be aware this is a hack! |
This is definitely the best solution so far. However, I still can't get EDIT: It seems that https://github.com/davidtheclark/focus-trap-react messes things up. Removing it made |
Unfortunately I'm trying to convert a Chrome extension to use React. The content script uses a shadow DOM to avoid styling conflicts from the host page. At the moment it looks like I'll have to remove the shadow DOM and do my best to override any weird styling that I come across. EDIT: I got this working by creating my own shadow root and applying the above workarounds directly:
Still early days, but it's looking promising so far. |
Can't make SyntheticEvent's of |
yeah sounds like web components are yet to be enabled within the context of an extension. But why do you need it there, out of interest? I believe an extension sandboxed so you can do as you please (kinda) without the need of a custom element. |
@renegare I'm using a shadow root because I need to inject an overlay onto particular websites, and the parent website styles will often conflict with mine. I've even had one site mutate my DOM to wrap my input fields. |
@chrisparton1991 u can get window from extension, see here https://gist.github.com/devjin0617/3e8d72d94c1b9e69690717a219644c7a |
Later edit: I originally misstated that the problem is in React Portals, but that's not true, the problem is in rc-utils, a dependency of Ant Design System. Original post However, if the parent container (passed in with getContainer) is created inside the shadowRoot (and it should be, otherwise this is not a problem), relying on the fact that Portal Wrapper does something like parent.appendChild you can rewrite parent.appendChild to also add ownerDocument :)
Hopefully this will help somebody having the same problem. Sorry for the somewhat confusing code, I pulled it out from my solution without cleanup. Maybe PortalWrapper could be updated from:
to
and that would fix the issue |
I switched from react-shadow-dom-retarget-events to renegare fix and all was working well in modern browsers. Unfortunately, this seems to be causing issues with the webcomponents pollyfill in at least edge and IE. I believe it is altering Update: It is the |
…oot node while also making it appear like a normal document as suggested in facebook/react#9242 (comment) Also adjust dimensions of shadow container as necessary.
We've built out a fairly small solution for this problem called We'll be dogfooding this in our internal projects, but would love anybody that wants to try it out to do so! Please log issues if you find them, as we have a vested interest in it being robust and useful! |
…ponents. This is ugly hack to workaround issue with event propagation when react root is inside custom web component. See facebook/react#9242 This issue caused strange behaviour of platform components inside semantic-map popovers and Mirador annotation popovers. Basically even `semantic-link` was not working properly there and instead of changing the content of the page dynamically it reloaded the whole page. Or for example it was not possible to use `mp-event-trigger` in such context. Signed-off-by: Artem Kozlov <[email protected]>
This has been fixed in React 17. Fiddle with 16: https://codesandbox.io/s/elegant-wilson-jirq9 There may still be some corner cases so feel free to file new issues if something doesn't work. |
No, it was the changes to event delegation (to attach events at the root). https://reactjs.org/blog/2020/08/10/react-v17-rc.html#changes-to-event-delegation |
That looks great! It seems like the perfect fix. Thank you! |
…ponents. This is ugly hack to workaround issue with event propagation when react root is inside custom web component. See facebook/react#9242 This issue caused strange behaviour of platform components inside semantic-map popovers and Mirador annotation popovers. Basically even `semantic-link` was not working properly there and instead of changing the content of the page dynamically it reloaded the whole page. Or for example it was not possible to use `mp-event-trigger` in such context. Signed-off-by: Artem Kozlov <[email protected]>
升级react 18吧 |
Is this fixed? |
@kudorgyozo If you look five comments up.
|
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
A React Component with an Event Handler (for example onClick) is rendered inside a Web Component. When the Component is clicked the Event does not receive the React Component (specified callback is not invoked)
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template: https://jsfiddle.net/reactjs/69z2wepo/).
You can reproduce it with Web Component example contained in the react repository (https://github.com/facebook/react/blob/master/examples/webcomponents/index.html): Replace the 'a' element with a button and add for example an onClick event handler.
You can find a modified version of the Web Component example (based on the 15.4.2 codebase - https://github.com/facebook/react/blob/v15.4.2/examples/webcomponents/index.html) here:
https://gist.github.com/nilshartmann/3a520920e5fc920bfde49e077ad3beab#file-index-html-L50
What is the expected behavior?
The event handler should be called.
For testing I have modified
getEventTarget.js
to return the target from thepath
property of thenativeEvent
(instead of the "original"target
from thenativeEvent
). With this addition it works -the Event Handler is called.
You can find the modified version also in the gist: https://gist.github.com/nilshartmann/3a520920e5fc920bfde49e077ad3beab#file-geteventtarget-js-L6
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
15.4.x and 16.x
I've tested in Chrome, Firefox and Safari. I don't know if it works in previous versions of React (don't think so)
The text was updated successfully, but these errors were encountered: