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.
54 Shares
# Shared by Kristofer Joseph on Thursday, November 9th, 2023 at 6:23pm
# Shared by Eric A. Meyer on Thursday, November 9th, 2023 at 6:23pm
# Shared by Aslak Raanes on Thursday, November 9th, 2023 at 6:24pm
# Shared by E.W. Doc Parris on Thursday, November 9th, 2023 at 6:24pm
# Shared by The Spicy Web on Thursday, November 9th, 2023 at 6:24pm
# Shared by Stephen Bell on Thursday, November 9th, 2023 at 6:24pm
# Shared by Stedi :bassguitar: 🎸 🎼 🦅 🚴 on Thursday, November 9th, 2023 at 6:24pm
# Shared by Adrian McEwen on Thursday, November 9th, 2023 at 6:24pm
# Shared by rem on Thursday, November 9th, 2023 at 6:24pm
# Shared by David G. Smith on Thursday, November 9th, 2023 at 6:51pm
# Shared by John Allsopp on Thursday, November 9th, 2023 at 6:51pm
# Shared by Diewy on Thursday, November 9th, 2023 at 6:51pm
# Shared by Hermann Dettmann on Thursday, November 9th, 2023 at 6:51pm
# Shared by Zach Leatherman on Thursday, November 9th, 2023 at 6:51pm
# Shared by tomhazledine on Thursday, November 9th, 2023 at 6:51pm
# Shared by Thomas Michael Semmler on Thursday, November 9th, 2023 at 6:51pm
# Shared by Peter Richardson on Thursday, November 9th, 2023 at 7:27pm
# Shared by Jesper on Thursday, November 9th, 2023 at 7:27pm
# Shared by Pavel Zinoviev 👾 on Thursday, November 9th, 2023 at 7:27pm
# Shared by Eric on Thursday, November 9th, 2023 at 7:27pm
# Shared by Tim van der Lippe on Thursday, November 9th, 2023 at 8:00pm
# Shared by Richard Livsey on Thursday, November 9th, 2023 at 8:00pm
# Shared by Sona on Thursday, November 9th, 2023 at 8:00pm
# Shared by Peter Janes on Thursday, November 9th, 2023 at 8:00pm
# Shared by Simon Skoczylas on Thursday, November 9th, 2023 at 8:00pm
# Shared by Tiota Sram on Thursday, November 9th, 2023 at 8:24pm
# Shared by Dave Rupert on Thursday, November 9th, 2023 at 9:00pm
# Shared by Thomas Peklak on Thursday, November 9th, 2023 at 9:00pm
# Shared by Symfony Station :symfony: on Thursday, November 9th, 2023 at 9:00pm
# Shared by Simon MacDonald on Thursday, November 9th, 2023 at 10:32pm
# Shared by Matt Singletary on Thursday, November 9th, 2023 at 10:59pm
# Shared by Holger Hellinger on Thursday, November 9th, 2023 at 10:59pm
# Shared by Simon Praetorius on Thursday, November 9th, 2023 at 11:59pm
# Shared by Nicöd·e on Friday, November 10th, 2023 at 12:36am
# Shared by Jon on Friday, November 10th, 2023 at 1:09am
# Shared by Alan Dávalos on Friday, November 10th, 2023 at 3:28am
# Shared by Michael Sean Becker on Friday, November 10th, 2023 at 3:59am
# Shared by Warren Buckley on Friday, November 10th, 2023 at 7:36am
# Shared by Téotime Pacreau on Friday, November 10th, 2023 at 8:41am
# Shared by RT bot :blobcheer: on Friday, November 10th, 2023 at 10:43am
# Shared by Patrick Brosset on Friday, November 10th, 2023 at 1:10pm
# Shared by Bertrand Delacretaz on Friday, November 10th, 2023 at 1:10pm
# Shared by Chris Taylor on Friday, November 10th, 2023 at 1:10pm
# Shared by Carlos Noguera on Friday, November 10th, 2023 at 1:37pm
# Shared by Timo Häkkinen on Friday, November 10th, 2023 at 2:13pm
# Shared by Bruce B Anderson on Monday, November 13th, 2023 at 12:30am
# Shared by Mario on Monday, November 13th, 2023 at 5:53am
# Shared by georapbox on Tuesday, November 14th, 2023 at 9:38pm
# Shared by Felipe Kinoshita on Wednesday, November 15th, 2023 at 7:44am
# Shared by HappiePlant :flow: on Wednesday, November 15th, 2023 at 10:37am
# Shared by divdev on Wednesday, November 15th, 2023 at 7:26pm
# Shared by Evil Jim O’Donnell on Sunday, November 19th, 2023 at 11:13am
# Shared by 北市真 on Sunday, November 19th, 2023 at 3:17pm
# Shared by Halvor William Sanden on Wednesday, November 22nd, 2023 at 8:27am