Thinking Through Styling Options for Web Components
Publikováno: 16.1.2020
Where do you put styles in web components?
I'm assuming that we're using the Shadow DOM here as, to me, that's one of the big draws of a web component: a platform thing that is a uniquely powerful thing the platform can do. So this is about defining styles for a web component in a don't-leak-out way, and less so a way to get global styles to leak in (although that's very interesting as well, which can be done via … Read article
The post Thinking Through Styling Options for Web Components appeared first on CSS-Tricks.
Where do you put styles in web components?
I'm assuming that we're using the Shadow DOM here as, to me, that's one of the big draws of a web component: a platform thing that is a uniquely powerful thing the platform can do. So this is about defining styles for a web component in a don't-leak-out way, and less so a way to get global styles to leak in (although that's very interesting as well, which can be done via custom properties which we'll look at later in the article).
If you're building the template inside the JavaScript — which is nice because of template literals and how we can sprinkle our data into the template nicely — you need access to those styles in JavaScript.
const template = `
<style>${styles}</style>
<div class="${class}">
<h2>${title}</h2>
${content}
</div>
`;
Where does that style variable come from? Maybe also a template literal?
const style = `
:host {
background: white;
}
h2 {
font: 900 1.5rem/1.1 -system-ui, sans-serif;
}
`;
I guess that's fine, but it makes for a big messy block of code just dunked somewhere in the class where you're trying to build this web component.
Another way is to <template>
the template and make a <style>
block part of it.
<template id="card-template">
<style>
:host {
background: white;
}
h2 {
font: 900 1.5rem/1.1 -system-ui, sans-serif;
}
</style>
<div id="card-hook">
<h2 id="title-hook"></h2>
<p id="desc-hook"></p>
</div>
</template>
I can see the appeal with this because it keeps HTML in HTML. What I don't love about it is that you have to do a bunch of manual shadowRoot.querySelector("#title-hook").innerHTML = myData.title;
work in order to flesh out that template. That doesn't feel like a convenient template. I also don't love that you need to just chuck this template somewhere in your HTML. Where? I dunno. Just chuck it in there. Chuck it.
The CSS is moved out of the JavaScript too, but it just moved from one awkward location to another.
If we wanted to keep the CSS in a CSS file, we can sorta do that like this:
<template id="card-template">
<style>
@import "/css/components/card.css";
</style>
<div id="card-hook">
<h2 id="title-hook"></h2>
<p id="desc-hook"></p>
</div>
</template>
(The use of <link rel="import" type="css" href="">
is deprecated, apparently.)
Now we have @import
which is an extra HTTP Request, and notorious for being a performance hit. An article by Steven Lambert says it clocked in at half a second slower. Not ideal. I don't suppose it would be much better to do this instead:
class MyComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
fetch('/css/components/card.css')
.then(response => response.text())
.then(data => {
let node = document.createElement('style');
node.innerHTML = data;
document.body.appendChild(node);
});
}
// ...
}
Seems like that would potentially be a Flash-of-Unstyled-Web-Component? I guess I should get off my butt and test it.
Now that I'm digging into this again, it seems like ::part
has gotten some steam (explainer). So I can do...
const template = `
<div part="card">
<h2>${title}</h2>
${content}
</div>
`;
...then write styles in a global stylesheet that only apply inside that Shadow DOM, like:
my-card::part(card) {
background: black;
color: white;
}
...which has a smidge of browser support, but maybe not enough?
These "part" selectors can only touch the exact element it's connected to. You'd have to do all your styling by applying a part name to every single DOM node and then styling each entirely on its own. That's no fun, particularly because the appeal of the Shadow DOM is this isolated styling environment in which we're supposed to be able to write looser CSS selectors and not be worried our h2 { }
style is going to leak all over the place.
Looks like if native CSS modules becomes a thing, that will be the most helpful thing that could happen.
import styles from './styles.css';
class MyElement extends HTMLElement {
constructor() {
this.attachShadow({mode: open});
this.shadowRoot.adoptedStyleSheets = [styles];
}
}
I'm not sure, however, if this is any sort of performance boost. Seems like it would be a wash between this and @import
. I have to say I prefer the clarity and syntax with native CSS modules. It's nice to be writing JavaScript when working with JavaScript.
Constructable Stylesheets also look helpful for sharing a stylesheet across multiple components. But the CSS modules approach looks like it could also do that since the stylesheet has already become a variable at that point.
The post Thinking Through Styling Options for Web Components appeared first on CSS-Tricks.