What about Web Components?
Are Web Components actually usable nowadays? Could they replace frameworks and bundlers for small to medium sites? Honest question! After only using frameworks and bundlers for too many years, I’m totally out of the loop what’s now natively possible.
I’ve built a few interactive islands on this site as custom elements, mostly for the sake of encapsulating logic. Like my navigation bar, that floats when you scroll up.
Writing this, I realize that this sentence will be outdated as soon as I change the design of this site, and future readers will think “what floating navigation bar?” If that’s the case, then: whoopsie sorry!
Using custom elements for these JavaScript islands also has the advantage that event listeners can initialize when elements mount or unmount:
document
.querySelector('main-nav')
.addEventListener('click', () => {
// Only fires when `main-nav` was mounted during
// initial page load. When it mounts later, it won't fire.
})
class MainNav extends HTMLElement {
connectedCallback() {
this.addEventListener('click', () => {
// Also fires when `main-nav` mounts later.
})
}
}
customElements.define('main-nav', MainNav)
Most of my JavaScript on this site is currently defined inside custom elements.
It’s useful! I vibe more with that. And I need it, because with swup, elements will mount and unmount when transitioning between pages. Without the custom element, I’d have to listen on a document click and then check the element inside the event listener… I don’t vibe with that.
But besides encapsulating interactivity into a class, could I use Web Components for even more? Like, could I replace much of the template engine of my static site generator with just Web Components? For encapsulating styles and markup?
And with encapsulating styles, I don’t care that much about fighting the CSS cascade. I stick to my own CSS conventions and I rarely fight against the cascade. But, let’s say I’m writing CSS as a bunch of utility classes: could I write HTML components and reuse them as custom elements, and then use the CSS utilities inside them? Without repeating the class names over and over again? Like you would do with React or Svelte and whatnot? Or another type of style encapsulation?
For my own small or medium-sized sites, I like the idea of using native web platform features. It means that I will always be able to run the site. Nothing will break because a library didn’t follow semver, or a build-tool depends on a bunch of old executables and node-gyp that refuses to compile because of other whacky dependencies.
Here’s my wishlist:
- It should work with server rendering (or static rendering). No flash of unstyled content.
- It needs to work with JavaScript disabled. I like progressive enhancement.
- I want to encapsulate markup. I want to only write the markup of a component once, and ship it once (because if I need to ship the markup multiple times, I need an external template engine again).
- Declarative templating and rendering would be cool.
- Custom attributes, specifically to easily define component variants.
- When I have a manageable amount of Web Components, I should be able to load them as individual files without performance issues. It would be cool to ship exactly what I have in development to production, without any build steps.
- Also, if a JS file could only be fetched when its custom element is actually used, in combination with some preloading hints, that would be cool.
- Being able to define my whole UI as native reusable components, even if there’s no interactivity. (As always, there are exceptions, I don’t want literally everything to be a reusable component)
- Widely available today in all browsers.
- Probably more that I forgot to add and will notice after a good walk.