by mdhb · 2y ago · 0 comments
You have two options:
1. you can insert a <link rel=“stylesheet”> tag the same way you might in the <head>
2. Native CSS modules
Example 1: https://developer.chrome.com/docs/css-ui/declarative-shadow-...
Example 2: https://web.dev/articles/css-module-scripts
Part of the problem with web components is that the component can only style the slotted element and not its descendants. There’s all kinds of weird edge cases that make styling web components painful.
What web developers were asking for was scoped CSS and HTML templates. How it became what it is today I don’t know, but it’s been massively overcomplicated while at the same time having really awkward limitations.
There is a simple polyfill however in the meantime which means you can actually use it today. https://github.com/guybedford/es-module-shims
It shouldn’t be a shock that people might feel very differently about those two organisations based on their previous actions and all.
Another way to style elements that are inside the Shadow DOM (from the outside) is to use the `::part(partname)` pseudo-selector, but you need to set the `part="partname"` attribute on elements inside the shadow DOM in order to allow them to be styled from outside with the pseudo-selector.
Another approach I found which is useful when building components which work with slotted template tags is to project the slotted template's content into another slotted element (e.g. a div) provided by the user the from outside. The second slotted element acts as a viewport which holds the component's rendered HTML output. So the component requires the user to provide both a <template slot="item"></template> element and also a <div slot="viewport"></div> element to project the rendered output into. I wrote a post about it:
https://dev.to/jondubois/web-components-the-template-viewpor...
So, I could include Tailwind in all my components and simply use the classes as I do without components?