🎃 A scary good deal! Join the Lean Web Club today and get 1 month free, plus 30% off for the first three months after that. Click here to join for free now! Earlier this week, I got an email from someone asking how to listen to events inside a Web Component from other scripts. For example, imagine you have Web Component that validates form fields…
Somewhere else in your script, you want to detect JavaScript actually provides you with a way to do this already, right out of the box, using event delegation and event bubbling. Inside your handler, the
But… this doesn't work once you use the Shadow DOM. Let's say you take the advice of JS framework evangelists and move your nice "global HTML" into the Shadow DOM where it's "safe" and "encapsulated."
Now, your event delegation listener never fires because the Shadow DOM is isolated from the main DOM. To get it around it, you need to add custom events to your Web Component that outside scripts can listen to. If it sounds silly to recreate something the browser gave you for free already in exchange for what seems like minimal to no benefit at all… it is! This is why I say that the Shadow DOM is an antipattern. It has occasional uses. They're generally overstated. Cheers, Want to share this with others or read it later? View it in a browser. |
Beranda » Tanpa Label » [Go Make Things] Listening to events inside a Web Component (from outside the Web Component)
0 Komentar untuk "[Go Make Things] Listening to events inside a Web Component (from outside the Web Component)"