I am trying to raise events out of a webponent, but it does.
<my-ponent id="xyz" bez="hallo" hello="myScript()"></my-ponent>
<script>
xyz.addEventListener("hello", function(event) {
console.log(event.detail.name);
});
</script>
Neither the html-tag "hello" does raise the event, nor the event-listener does.
The web ponent looks like this:
var button=document.createElement("button");
button.innerHTML=cap;
button.addEventListener('click', () => {
console.log("click");
button.dispatchEvent(new CustomEvent("hello", {
detail: { name: "John" }
}));
});
shadow.appendChild(button);
Can anyone help me please to find the mistake? Thanks a lot.
Code-Fiddle here: /
I am trying to raise events out of a webponent, but it does.
<my-ponent id="xyz" bez="hallo" hello="myScript()"></my-ponent>
<script>
xyz.addEventListener("hello", function(event) {
console.log(event.detail.name);
});
</script>
Neither the html-tag "hello" does raise the event, nor the event-listener does.
The web ponent looks like this:
var button=document.createElement("button");
button.innerHTML=cap;
button.addEventListener('click', () => {
console.log("click");
button.dispatchEvent(new CustomEvent("hello", {
detail: { name: "John" }
}));
});
shadow.appendChild(button);
Can anyone help me please to find the mistake? Thanks a lot.
Code-Fiddle here: https://jsfiddle/b43uqsLp/2/
Share Improve this question asked Jan 25, 2021 at 18:08 KreativgeistKreativgeist 871 silver badge6 bronze badges2 Answers
Reset to default 5There is some information missing in the other answer
- the button
click
listener is inside shadowDOM, soposed
has no use - default listeners like
click
are not stopped by shadowDOM - !!! CustomEvents require both
posed:true
andbubbles:true
to escape Custom Elements with 'open' shadowDOM. - do not attach listeners in the
connectedCallback
unless you are 100% certain that is what you want; theconnectedCallback
runs again when DOM elements are moved in the document super()
returns and sets the 'this', andattachShadow()
returns and sets thethis.shadowRoot
reference, no need to use your own variables.
run super first means you can't access 'this' before it is created; but you can run any JS you want before the super() call
This JSFiddle: https://jsfiddle/CustomElementsExamples/qody0u4n/
shows posed
and bubbles
behaviour, with extra listeners on the document
document.addEventListener("click", (evt) => log(evt, 'document'));
document.addEventListener("MyEvent", (evt) => log(evt, 'document'));
<my-ponent id=ONE></my-ponent>
<my-ponent id=TWO posed>
<my-ponent id=INSIDETWO posed></my-ponent>
</my-ponent>
<my-ponent id=THREE posed bubbles>
<my-ponent id=INSIDETHREE posed bubbles></my-ponent>
</my-ponent>
notes
none of the MyEvents are caught by element ONE or the document
The document only receives the
CustomEvent("MyEvent")
when bothposed
andbubbles
are setThe
posed
event does not stop at the shadowRoot boundary! It is halted at the Custom Element boundary. In the JSfiddle there are additionalDOMlisteners
onmy-ponent
to demonstrate this.
Also see: https://pm.dartus.fr/blog/a-plete-guide-on-shadow-dom-and-event-propagation/
The problem occurs because of the Shadow DOM (try to inspect your ponent and you will see what I mean):
Good news, it's really simple to fix - just propagate the event across the shadow DOM into the regular DOM via posed: true
property in your CustomEvent's options:
button.dispatchEvent(new CustomEvent("hello", {
detail: { name: "John" },
posed: true // Like this
}));
Here is JSFIDDLE.