β
π 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)
Related Post:
- The best positive Spotify podcasts; top MagSafe battery packs; add a hydra bot to your Discord server
- Search Engine Roundtable - Daily Search Forum Recap: March 3, 2023
- U.S. Airlinesβ January 2023 Fuel Cost per Gallon Up 4.3% from December 2022; Aviation Fuel Consumption Down 0.7% from Pre-Pandemic January 2019
- Response to Public Comment: National Transit Database Safety and Security Reporting Changes and Clarifications
- Belarusian Nobel Prize winner Bialiatski sentenced to 10 years in jail
0 Komentar untuk "[Go Make Things] Listening to events inside a Web Component (from outside the Web Component)"