I often say that the Shadow DOM is an antipattern. I tend to catch a lot of shit for it, because for years the JS framework cult positioned the Shadow DOM as the killer feature of Web Components. But one of the biggest challenges the Shadow DOM creates for Web Components is around styling. It breaks the cascade, and makes styling things a million times harder and more repetitive than it should be. Today, we're going to look at why it sucks, and what our options are. Let's dig in! The Shadow DOM breaks the cascadeThe Cascade (the first C in CSS) is one of CSS's most powerful features. It lets you write way less CSS while providing a consistent look-and-feel throughout your site or app. Imagine a world where you needed to specify the
Instead, you can do this…
Thank you, Cascading Style Sheets! But because the Shadow DOM is encapsulated from the main DOM, the styles in your global stylesheet do not cascade into it. An exampleImagine you've got some HTML that looks like this…
And your
When it runs, the rendered HTML will looks like this…
And the How to style Web Components in the Shadow DOMThere are five different ways to address this. They all kind of suck.
Over the next few days, we'll look at each of these approaches in a bit more detail. Why this all sucksHaving a global design system kicks ass. You can load your CSS into the page, and all of your UI will pick up the styles and look cohesive. This is true even if you manage a third-party script that loads into someone else's UI. If a script I wrote adds a button to someone else's UI, I want that button to look like their UI elements and brand, not my predefined ideas of what it should look like. Cheers, Want to share this with others or read it later? View it in a browser. |
0 Komentar untuk "[Go Make Things] Styling the Shadow DOM with Web Components"