Web Components as pattern libraries

The strongest argument for Web Components is the pattern library: a component is invoked as a custom element rather than as a class convention. Bootstrap makes you write <ul class="nav nav-tabs">; Bulma makes you write <div class="tabs">; a design system can instead ship <designsystem-tabs>.

Shoelace is a full pattern library built this way, using the sl namespace — its tabs are <sl-tab-group>. Under the hood it is based on Stencil, and each component carries a co-located template and stylesheet. That component model is one benefit; Shadow DOM is the other.

Style isolation, and its cost

Shadow DOM gives you platform-level isolation. A tab's styling can live in a plain .tab class, generic name and all, without leaking out to other elements on the page or letting outside CSS seep in.

The same walls apply at other libraries. Ionic's <ion-tabs> elements work without any other framework (Angular, React and Vue are supported alongside its own Stencil), and Ionic recently added Shadow Parts support. Brandy Carney describes both sides of encapsulation:

Shadow DOM is useful for preventing styles from leaking out of components and unintentionally applying to other elements. For example, we assign a .button class to our ion-button component. If an Ionic Framework user were to set the class .button on one of their own elements, it would inherit the Ionic button styles in past versions of the framework. Since ion-button is now a Shadow Web Component, this is no longer a problem.

However, due to this encapsulation, styles aren't able to bleed into inner elements of a Shadow component either. This means that if a Shadow component renders elements inside of its shadow tree, a user isn't able to target the inner element with their CSS.

CSS custom properties do penetrate the Shadow DOM, but "variablizing" every piece of a design system was rejected as unwise. The chosen mechanism is the part attribute: an inner element is marked <div part="icon"> and styled from outside with custom-component::part(icon) { }. Those hooks are a reasonable path for a pattern library, though the selectors don't behave as CSS authors expect: no conditional selection, no selecting children, no cascade — a straight reach through the membrane to grab one element or nothing.

The library-dependency problem

Andrea Giammarchi raises a related complaint about the current state of Web Components:

Every single library getting started, including mine, suggest we should import the library in order to define what [sic] supposed to be a "portable Custom Element".

Google points to LitElement, Microsoft to FASTElement, Stencil to its own Component, hyperHTML to its own Component. Nobody is just using "raw" Web Components. The irony is that Web Components are meant to be native platform technology, usable without buying into a particular tool — yet adopting one tool reproduces the lock-in you'd have with React. Giammarchi suggests smaller libraries such as this one; a pattern library that used no library at all would be the more interesting outcome.

The naming is equally unsettled: FAST calls itself an "interface system" and then a "UI framework" in consecutive sentences on its homepage, Shoelace calls itself a "library," and "design system" remains the broadest and most common term — FAST even uses it in code for the wrapper element that controls the theme.