const domNode = ReactDOM.findDOMNode(this);
LOL, "Let's query the whole (virtual)DOM"
and they still claim React is fast?
Reverse your logic, detect clicks inside your component.
It is 2021, we no longer support IE
Event.composedPath() gives you all DOM nodes the click
Event passed.
And do read the focusout
Event and :focus-within
CSS answers
at: How do I detect a click outside an element?
I added some nested shadowDOMs to show composedPath
gives you all DOM nodes.
Remember: CustomEvents need composed:true
as option setting, for the Event to escape shadowDOM.
Native Events like click
always escape shadowDOM
And remember... React never supported the native Event model.
https://custom-elements-everywhere.com/libraries/react/results/results.html
You have to write a React wrapper for each and every native Web Component
That is like buying a can of soup, and always having to buy a canopener with it.
<style>
body { cursor:pointer }
::part(shadowdiv) { /* modern CSS shadowParts to style 'parts' inside shadowDOM */
color: white;
font: 14px Arial;
width: 80%;
}
</style>
<custom-element></custom-element>
<script>
window.CEdepth = 0;
window.addEventListener("click",e=>console.clear());
customElements.define("custom-element", class extends HTMLElement {
constructor() {
super().attachShadow({mode:'open'});
if (window.CEdepth++ < 3) {
this.id = window.CEdepth;
window.addEventListener("click", (evt) => {
let inside = evt.composedPath().includes(this);
console.log("clicked inside #"+this.id, ":", inside);
});
this.style.display = "inline-block";
this.style.padding = "1em";
this.style.background = ["red", "blue", "green"][this.id - 1];
this.shadowRoot.innerHTML = `<div part='shadowdiv'>Web Component #${this.id}`
+`<custom-element><b/></custom-element><div/>`;
}
}
})
</script>