☀️🧠Summer of Learning Sale! 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! In my video on building a modern web app with vanilla Web Components, I mentioned that I believe the Shadow DOM is an antipattern when using Web Components. I had a few folks in the comments ask me why. After all, the Shadow DOM is touted as one of the premiere features of working with Web Component. And certain features, like slots, require the Shadow DOM to work. I've built a lot of Web Components over the last couple of years, and in my experience, the Shadow DOM creates more problems than it fixes. Let's first talk about the Shadow DOM's benefits…
Now let's talk about the disadvantages…
And those benefits? You can achieve "good enough" version of them without the Shadow DOM. The custom elements you use to create your Web Component provide a styling hook that can scope your styles to just the Web Component.
This let's you take advantage of the cascade and write significantly less CSS. But what about external styles colliding with your component? Frankly, that sounds like an issue with how the global stylesheet is designed. Throwing more JS at the problem instead of writing better CSS isn't the happy path forward. And while the It does require you to use the So yea… I think the Shadow DOM is an antipattern that makes Web Components measurably worse in nearly all circumstances. I'm sure it has some edge-case uses, but it would be a measure of last resort for me, not a starting point. Cheers, Want to share this with others or read it later? View it in a browser. |
0 Komentar untuk "[Go Make Things] The Shadow DOM is an antipattern"