Event Handler on React Component not invoked when React Component is rendered inside a Web Component
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 the path property of the nativeEvent (instead of the "original" target from the nativeEvent). 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)
@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. (16.0.0 WC support is borked but I fixed it on master.)
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: If I understand correctly @xastor's suggestion is closely related to #2043, which appears to be the more general approach to fix this.
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 event.composePath() in https://github.com/opichals/react/tree/add-event-target-composePath-support based on https://github.com/facebook/react/issues/9242#issuecomment-292857232
Not sure if this is still an issue with anyone; I however found the following addition to your react app entry point will work:
class CustomElement extends HTMLElement {
connectedCallback() {
// setup
const shadow = this.attachShadow({ mode: "open" });
const root = document.createElement("div");
shadow.appendChild(root);
// Making the shadow appear like document
// so react events work as normal
Object.defineProperty(root, "ownerDocument", { value: shadow });
shadow.createElement = (...args) => document.createElement(...args);
ReactDOM.render(<App />, root);
}
}
customElements.define("custom-element", CustomElement);
// DONT FORGET to add <custom-element></custom-element> to your page
The rationale here is:
- react event system does work normally
- shadow dom re-targeting is the issue
- custom re-re-targeting is complex; and does not reliably work with events that do not bubble - example focus and blur
- instead of react binding to document it should bind to the shadow. Because as far as css and js events that is the true document root of a shadow dom
- react binds event listeners to the
ownerDocumentproperty of the root element it renders into. It expects that to be document. createElementappears to be the only method missing from the shadow element, which react needs
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 ReactDOM.render to have a third options arg? which allows you better customize all global props it depends on to function e.g document.
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:
createElementNScreateTextNode
I'm surprised I didnt have to add createDocumentFragment 🤔
Actually found that react doesn't use this internally -- I suppose this is still surprising
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 createPortal() to work.
EDIT: It seems that https://github.com/davidtheclark/focus-trap-react messes things up. Removing it made createPortal() work as well.
Unfortunately customElements.define is null inside Chrome extension content scripts (possibly Firefox too, I haven't checked). See https://bugs.chromium.org/p/chromium/issues/detail?id=390807.
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:
const root = document.body.appendChild(this.doc.createElement('div'));
const shadow = root.attachShadow({ mode: 'closed' });
const container = shadow.appendChild(this.doc.createElement('div'));
Object.defineProperty(container, 'ownerDocument', { value: shadow });
shadow.createElement = (...args) => document.createElement(...args);
shadow.createElementNS = (...args) => document.createElementNS(...args);
shadow.createTextNode = (...args) => document.createTextNode(...args);
ReactDOM.render((
<button onClick={() => alert('I work!')}>Click me</button>
), container);
Still early days, but it's looking promising so far.
Can't make SyntheticEvent's of react-bootstrap package work when it's component is rendered inside shadow DOM...
Unfortunately
customElements.defineisnullinside Chrome extension content scripts (possibly Firefox too, I haven't checked). See https://bugs.chromium.org/p/chromium/issues/detail?id=390807.
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 as about shadow dom: https://www.npmjs.com/package/react-shadow