When custom elements are the right call
Whether a native web component is worth writing depends heavily on context. The template layer isn't especially strong, there is no built-in state management, and projects that already pull in another component library have little reason to reach for the platform instead. A rough decision list looks like this:
- No other JavaScript framework with a component model is in use
- Templating stays simple
- Re-rendering doesn't need to be highly performant
- No state management is required
Tooling exists to fill some of those gaps, but adopting tooling in order to use native components tends to push developers toward a framework anyway — and stacking a framework on top of overlapping tooling is unappealing.
The counterweight is that custom elements are genuinely native: nothing to download, and the Shadow DOM offers encapsulation that frameworks can't really replicate. You also get to define your own HTML element with your own API. That makes design systems the natural fit — <our-tabs active-tab="3"> is a safer, more durable interface than handing people a block of markup to paste. Refactors propagate everywhere at once.
A code block as a test case
A "code block" component meets most of the criteria. Attributes can drive behavior while the code itself lives as element content and degrades gracefully as fallback. State isn't needed. Syntax highlighting is a large, unwieldy stack of CSS that benefits from being isolated in the Shadow DOM. And a "click to copy" button is a nice bit of added functionality — enough to make it feel like something people would actually use.
It's not production ready, and it isn't published to npm. The implementation uses Prism.js for highlighting.
The CSS-Tricks <circle-text> component is a prior example of the same approach: accept a radius as a parameter, take content as content, and emit an <svg> that does the job.
Open questions from building it
- Third-party dependencies. Copying Prism.js wholesale into the component would isolate it further but seems absurd; documenting the dependency may be the more sensible route.
- Styling. Styling custom elements still lacks a good story, even with the Shadow DOM's advantages.
- Content plumbing. Pulling pre-formatted text into a template is awkward. It's presumably possible without a
<pre>inside the custom element, but reading content from that tag is far easier — at the cost of a slightly less friendly API than a bare<code-block>. - Attribute passing. Should you expose
data-lang="CSS"and translate it toclass="language-css"in the template to suit Prism, or pass attributes through untouched? The latter was chosen. - Lifecycle timing.
connectedCallbackis effectively the only hook, so DOM manipulation ideally happens before the finalshadowRoot.appendChild(node);. In this implementation Prism instead runs over theshadowRootafter it's appended, which works but likely forces repaints from injected spans. Doing it in advance is preferable. - Unescaped input. The component would be nicer if you could drop in raw HTML and have it escaped for highlighting, but then the fallback renders that HTML — potentially unsafe. One option: put the HTML in HTML comments and treat a leading
<!--as a special case.
Sponsor CSS-Tricks!
We've got lots of ways to help you connect with the greatest front-end community in all the land.
CSS-Tricks Newsletter
Ads are self-hosted



