Web components have been around for quite a while, but it feels like they’re having a bit of a moment right now.
It turns out that the best selling point for web components was “wait and see.” For everyone who didn’t see the benefit of web components over being locked into a specific framework, time is proving to be a great teacher.
It’s not just that web components are portable. They’re also web standards, which means they’ll be around as long as web browsers. No framework can make that claim. As Jake Lazaroff puts it, web components will outlive your JavaScript framework.
At this point React is legacy technology, like Angular. Lots of people are still using it, but nobody can quite remember why. The decision-makers in organisations who chose to build everything with React have long since left. People starting new projects who still decide to build on React are doing it largely out of habit.
Others are making more sensible judgements and, having been bitten by lock-in in the past, are now giving web components a go.
If you’re one of those people making the move from React to web components, there’ll certainly be a bit of a learning curve, but that would be true of any technology change.
I have a suggestion for you if you find yourself in this position. Try not to bring React’s mindset with you.
I’m talking about the way React components are composed. There’s often lots of props doing heavy lifting. The actual component element itself might be empty.
If you want to apply that model to web components, you can. Lots of people do. It’s not unusual to see web components in the wild that look like this:
<my-component></my-component>
The custom element is just a shell. All the actual power is elsewhere. It’s in the JavaScript that does all kinds of clever things with the shadow DOM, templates, and slots.
There is another way. Ask, as Robin does, “what would HTML do?”
Think about composibility with existing materials. Do you really need to invent an entirely new component from scratch? Or can you use HTML up until it reaches its limit and then enhance the markup?
I don’t think we should see web components like the ones you might find in a huge monolithic React app: your Button or Table or Input components. Instead, I’ve started to come around and see Web Components as filling in the blanks of what we can do with hypertext: they’re really just small, reusable chunks of code that extends the language of HTML.
Dave talks about how web components can be HTML with superpowers. I think that’s a good attitude to have. Instead of all-singing, all-dancing web components, it feels a lot more elegant to use web components to augment your existing markup with just enough extra behaviour.
Where does the shadow DOM come into all of this? It doesn’t. And that’s okay. I’m not saying it should be avoided completely, but it should be a last resort. See how far you can get with the composibility of regular HTML first.
Eric described his recent epiphany with web components. He created a super-slider custom element that wraps around an existing label and input type="range":
You just take some normal HTML markup, wrap it with a custom element, and then write some JS to add capabilities which you can then style with regular CSS! Everything’s of the Light Side of the Web. No need to pierce the Vale of Shadows or whatever.
When you wrap some existing markup in a custom element and then apply some new behaviour with JavaScript, technically you’re not doing anything you couldn’t have done before with some DOM traversal and event handling. But it’s less fragile to do it with a web component. It’s portable. It obeys the single responsibility principle. It only does one thing but it does it well.
Jim created an icon-list custom element that wraps around a regular ul populated with li elements. But he feels almost bashful about even calling it a web component:
Maybe I shouldn’t be using the term “web component” for what I’ve done here. I’m not using shadow DOM. I’m not using the templates or slots. I’m really only using custom elements to attach functionality to a specific kind of component.
I think what Eric and Jim are doing is exemplary. See also Zach’s web components.
At the end of his post, Eric says he’d like a nice catchy term for these kinds of web components. In Dave’s catalogue of web components, they’re called “element extensions.” I like that. It’s pretty catchy.
Or we could call them “HTML web components.” If your custom element is empty, it’s not an HTML web component. But if you’re using a custom element to extend existing markup, that’s an HTML web component.
React encouraged a mindset of replacement: “forgot what browsers can do; do everything in a React component instead, even if you’re reinventing the wheel.”
HTML web components encourage a mindset of augmentation instead.
Responses
Related posts
Installing web apps
BeforeInstallPromptEvent vs. navigator.install
Web App install API
It’s kind of ridiculous that this functionality doesn’t exist yet.
Applying the four principles of accessibility
Here’s how I interpret the top-level guidance in the Web Content Accessibility Guidelines.
Hanging punctuation in CSS
A little fix for Safari.
Of the web
Baldur Bjarnason has written my mind.
Related links
Web Backstories: Shadow DOM | Igalia
Eric Meyer and Brian Kardell chat with Jay Hoffmann and Jeremy Keith about Shadow DOM’s backstory and long origins
I enjoyed this chat, and it wasn’t just about Shadow DOM; it was about the history of chasing the dream of encapsulation on the web.
Tagged with podcast audio chat webcomponents shadowdom worldwideweb history encapsulation standards browsers frontend development
How to build a simple Camera component - Frontend News #4
A step-by-step guide to wrapping up a self-contained bit of functionality (a camera, in this case) into a web component.
Mind you, it would be nice if there were some thought given to fallbacks, like say:
<simple-camera>
<input type="file" accept="image/*">
</simple-camera>
Tagged with webcomponents camera component mediadevices apis mediastream canvas video javascript code frontend development
005: Service workers - Web Components Club
I strongly recommend that you read Going Offline by Jeremy Keith. Before his book, I found the concept of service workers quite daunting and convinced myself that it’s one of those things that I’ll have to set aside a big chunk of time to learn. I got through Jeremy’s book in a few hours and felt confident and inspired. This is because he’s very good at explaining concepts in a friendly, concise manner.
Tagged with goingoffline serviceworkers book review learning offline javascript webcomponents frontend development
Shadow DOM: fast and encapsulated styles – Monica Dinculescu
Monica explains how Shadow DOM could be the perfect answer for scoping CSS:
We didn’t have style encapsulation, so we started naming things “the right way” with BEM, so that we didn’t accidentally stomp over each other’s styles. We wanted to be able to author CSS from inside a JavaScript component, so we started using CSS-in-JS. We needed all these tools, because “the platform” (read: the browsers that be) wasn’t there, and building these tools showed that there was a need to move forward. For style encapsulation, Shadow DOM is the platform moving forward.
Although, in a way, Shadow DOM is also another flavour of CSS-in-JS:
Before you complain that using a Shadow DOM and Web Components means that it absolutely requires JavaScript: this is true.
Tagged with shadowdom css scoping styling encapsulation standards webcomponents frontend development
How Will Web Components Change CSS Architecture? - Snook.ca
Depending on how you’re currently structuring your CSS and class attributes, web components might not make all that much of a difference to your workflow.
Tagged with css webcomponents process architecture html classes structure frontend development extensibility components
Previously on this day
6 years ago I wrote Bookshop
Manually machine tagging books as a kind of mindless meditation.
15 years ago I wrote Responsible responsive images
A future-friendly approach to mobile-first responsive design.
19 years ago I wrote Berlin, day 4
Less expo, more Berlin.
20 years ago I wrote Put your money where Joe Clark’s mouth is
Joe finds me patronising.
22 years ago I wrote Party on, dudes
I’m a torn geek tonight. I was tempted to stay in and watch the new show on BBC that should be called Walking With Planets. In the end, I decided to come out to a WiFi pub for a Firefox release party.
22 years ago I wrote Alive, alive-o!
Maybe it’s just because I was visiting the city recently, but I’m getting a real kick out of this Dublin-based blog, Twenty Major.
23 years ago I wrote Photoshop actions
I’ve put together a new article. It’s basically a rundown of some Photoshop actions I use to create nice photographic effects.
24 years ago I wrote Wow!
When I got up yesterday morning, Jessica asked me if had dreamt nice dreams.
25 years ago I wrote Kaese Spaetzle
Catherine, our drummer, is coming over tomorrow night. Chris and Karin are going to cook up some kaese spaetzle.
25 years ago I wrote Satellite on a shoestring going strong
Nasa could learn a thing or two from the U.S. Naval Academy Alumni Association. One month after the clever folks in Maryland launched a satellite built for just $50,000 (instead of the originally anticipated $1,000,000), the satellite shows no sign of deg