134 points · by mmcclure · 2y ago · 104 comments
In terms of building a web app where you control the environment end-to-end, I don't think there's any inherent upside to using Web Components over React.
You nailed it that we are shipping SDKs with visual components (like a video player) that need to be compatible across all kinds of frontends.
Instead of N number of SDKs to maintain where N is every front end framework, we have 2: a web component, and a React wrapper around the web component. Maybe in the (near) future we only have to maintain 1.
The upside is that web components will last longer than react components.
There was a great blog post about this very thing and why someone chose them over react components in their use case (making their code migration-proof across updates and stack changes): https://jakelazaroff.com/words/web-components-will-outlive-y...
How much are you betting? React components have been around longer and are more widely established, so personally I'd expect them to last longer.
If you're prepared to put money down on it, I'd wager $500USD on longbets that Web Components will still function in 20 years.
And I'd wager another $500USD on longbets that it will be easier to port a web component written in 2024 into a new project written in 2044 than it will be to port a react component written in 2024 into a new project written in 2044.
The reason why I'm so confident is that Web Components are already a standard.
What does it mean to "last longer" when it comes to your own codebase? And why would web components help with that?
...which is the point the parent comment was making.
is the crux for me. When I clone one of my Vue projects from a few years ago and try to run it, it is always broken. My (new) node version is incompatible with a Vue core library, then updating the core library breaks some other libraries and requires config file changes.
What about using web components in a server rendered application without needing to resort to running JavaScript in the server?
In my opinion, fundamental problems like these really ought to be addressed before things become part of the web platform.
Web Components trace their history to trying to explain how the web platform itself works. HTML is also a flat namespace, and the goal was to empower developers to do all the same things the platform does and in the same way.
We hotly debated namespaces when designing the API and in the end decided to omit them but require at least one dash. You can think of the prefix kind of like the namespace in that sense. And in practice with reasonable prefixing practices we find folks rarely (if ever) collide, the same as ObjC.
(Not that things couldn't be better: https://github.com/WICG/webcomponents/blob/gh-pages/proposal...)
But then, all languages since Objective-C seem to agree that namespacing is a good idea. There's no debate there. Even JavaScript has modules now. So I can't help but wonder what makes web components unique in this regard.
Ah. I feel like the demos and examples that I've seen never have the first word as the pseudo-namespace so I had no idea that was the intention.
Fundamental enough to be included in the list of things to still fix for web components: https://w3c.github.io/webcomponents-cg/2022.html#scoped-elem...
Simple as that.
The problem with web components is that they've never had a templating/reactivity story, which means if you wanted to build one reactively you'd have to ship it with a framework, and shipping a separate framework per-component is just unworkable. So they don't make sense internally to an app, and they don't really make sense for a large library of components. They only really make sense for a single hand-crafted widget that a company wants to ship to various consumers who may be using different frameworks
My dream is still that browsers come up with an actual reactive-templating API for HTML. If they did that, we might finally be able to get rid of frameworks for many common use-cases
The worrying: we might be setting up for a scenario where newer developers are bewildered by mixed paradigms within what was (at least once upon a time) a simple library
Full disclosure I work for Mux (OP), so as a team we're web component fans, but I'm personally still almost exclusively writing React. So...personally, saying this with love, but add this to all the new esoteric hooks in React 19 and it feels like React might be losing the plot.
Which standards?
I keep saying: even web components people finally admitted that they need 20 more standards to solve issues (largely caused by web components themselves) that literally no other framework has: https://w3c.github.io/webcomponents-cg/2022.html
There's a reason very few frameworks (including the new ones) target web components by default and do their own thing. And their authors have been very clear as to why (think Svelte, Vue, Solid etc.).
They are not ignored. They just don't exist or, indeed, are solved by other, better means (or indeed better web specs like CSS scoping etc.). Let me quote directly from the doc:
--- start quote ---
It's worth noting that many of these pain points are directly related to Shadow DOM's encapsulation. While there are many benefits to some types of widely shared components to strong encapsulation, the friction of strong encapsulation has prevented most developers from adopting Shadow DOM, to the point of there being alternate proposals for style scoping that don't use Shadow DOM. We urge browser vendors to recognize these barriers and work to make Shadow DOM more usable by more developers.
--- start quote ---
To put simply, "many of these problems exist only because web components exist, and they are so bad that developers actively avoid web components and seek other solutions".
Here is a list of issues that literally don't exist anywhere except web components (so there's nothing to ignore):
- Composed Selection
"Since existing Selection APIs can represent only a single selection in a document, bound to a single Range, the need for an API to represent ranges over a composed tree has been generally acknowledged since at least 2015."
- Constructable Stylesheets & adoptedStyleSheets
The only way for web components can share stylesheets without duplicating them
- Cross-root ARIA
"Shadow boundaries prevent content on either side of the boundary from referencing each other via ID references. ID references being the basis of the majority of the accessibility patters outlines by aria attributes, this causes a major issue in developing accessible content with shadow DOM."
- CSS Properties and values inside shadow root
- Anything related to declarative templates: Declarative CSS Modules, Declarative Custom Elements, Declarative Shadow DOM
- DOM Parts
"Enable batched updates to the DOM, usable by library/framework view engines."
This is actually useful. Why the hell it's tied to Web Components? No one knows. But yeah, all/most frameworks have a solution to this, and web components are useless as a foundation for web frameworks without it
- Form-Associated Custom Elements
Yup, you still can't make a custom button that behaves like a submit button in web components
- Lazy custom element definitions
Web components are eagerly rendered whether you want it or not. I don't think there's a single framework that doesn't have lazy loading/rendering/rendering on demand
- Open styling of shadow roots
- Styling children of slotted content
- Theming
So is react losing the plot or just naturally it's ideas are being folded into the browser natively?
All of this has “just worked” with React since it very first release.
Web Components are great for distributing components. Not for building the internals of an app. In fact, for moderately complex web components it often pays to build the internals of the web component with a framework like React or Svelte.
Make a Llama 3 based search as the home page :) And fully support AI/LLMs in the browser :)
Say you have a component library where each component depends on a base set of styles and you don't want to load duplicate styles for each component.
Is there a best practice around this yet?
https://lit.dev/docs/components/styles/#inheriting-styles-fr...
https://lit.dev/docs/components/styles/#sharing-styles
Already quite some time ago I made a little library which makes this easy:
They have proposed it. They have also explained why the existing proposal for customisable built ins is a loaded gun waiting to explode.
> To date they have done neither
Tell me you know nothing about the topic without telling me that
That is laughable. I've been following this since finding that my code extending built-in elements for progressive enhancement in the Hotwire framework was dead in the water on Safari and no polyfill could resuscitate it because the browser infrastructure was fundamentally missing. In reality, Apple have sat on the pot groaning for a decade without taking a dump, and the stated reason in 2016 was this, entirely:
rniwa> The problem here is that UA internally checks local name e.g. `element.localName == aTag` to do various things, and all of that code needs to be updated to account for the fact there could be an instance of `HTMLAnchorElement` whose local name is not `a`.
Thus frankly placing it in the too-hard-basket, a refreshing piece of honesty from a company that's usually in love with its own bullshit. You don't have to look hard at the WebKit source code to grok that, yes, it surely would require a massive chunk of refactoring. Still, here's the updated response, from 2023, after a flamebait thread on WebKit/standards-positions that attracted a mountain of withering criticism: rniwa> The position has been established.
[WebKit locked as off-topic]
Which I took to mean, "all of you whinging bastards can get fucked, it's our browser".So it's fundamentally an effort-reward gap for Apple that has become a dug-in entrenched position over time as more product managers flail around hoping to justify it. Pretending an obviously commercial/resourcing decision is due to <bunch of technical reasons> is a common way to avoid committing resources, but any engineer who actually believes that fiction is a damn fool. I'm reminded of Cisco's hissy fit with the IEEE over PoE. Apple aren't dumb enough to miss that refusing to implement part of a published standard erodes their claim of stewardship over that standard, and if a major customer really wanted it, they'd flip-flip in a heartbeat.
> Tell me you know nothing about the topic without telling me that
Aw, you're gonna hurt my feelings.
Its not.
> and the stated reason in 2016 was this, entirely:
It's not the entirety of the reason
> Which I took to mean, "all of you whinging bastards can get fucked, it's our browser"
Since you keep completely misrepresenting and lying about Webkit's position, sure.
I honestly couldn't care about your vitriol in the rest of your message, or any other messages.
Adieu.
Technically you can just export a string with the name of your web component:
export const MyButton = 'my-button'
import MyButton from './MyButton'
...
return <MyButton />
But you do have to make sure there is no name clash. And since this is more of a convention any web components libs you pick up will probably not do this.In my 13 years there are a lot of technologies I've never used. Maybe there was opportunities where you could have, or maybe things are working just fine for the problems you're solving and you don't need it, which is fine too.
So a new way of dealing with certain problems is required, which requires a mindset change. For most developers it doesn't add value right away to break out of their framework cage and share their components with each other. But eventually we will all benefit from web components because they will work in any environment without them ever breaking. You won't be able to say that about traditional framework components.
Anecdotally it feels like they arrived right as headless/self-styled component libraries started to pick up steam, and things inverted to the point that not being able to have your "external" styles easily apply to the component became more of a problem than them being affected by random stray styles.
I wonder if that is the true reason OLE type systems have failed on the web, as there aren’t enough agreed upon preexisting widgets for it not to descend into an inconsistent horror show.
This is the inherent tension. It requires good web component authoring to expose:
1. `part`s that can be accessed by application-level CSS
2. slots for the application developer to inject html
3. CSS custom properties (--variable) -- these pierce the shadow DOM
The web component authors have to be very intentional about these things. There are good examples and bad examples and I think people are still learning how to do this well.
In practice, this update is great for usability. But it's a sign that web components are still relevant, extending my time dealing with poorly built ones.
More broadly speaking I have found myself getting thrashed by the ever evolving "best practices" in the React ecosystem. First: write everything using class components, and then a couple years later everything should be written with hooks.
I think the benefit of web components (for certain things) is that the APIs and the standards move slowly. Something you write today isn't going to feel obsolete in only a couple years.
If you need an equivalent Expedient Field Hack for a web component, remember it's an HTMLElement subclass and ends up in the DOM. So you can get the object back out of the DOM as 'just another internal element' and gutwrench it from there (or, depending, subclass it and register your subclass, or insert an extra entry into its prototype chain, or ...).
I mean, yes, those are all horrible, but so's digging into the guts of a react component you don't own, sometimes taking a dependency on the internals of somebody else's code is the least horrible option.
... it occurs to me that given a JS Function's .toString() returns the source code, on an e.g. lit-html component you could probably even grab the render function, yank the html`...` call out of it, regexp that to tweak the mark up, and then use an indirect eval to stuff it back in.
But really, I think "you now have the element object, monkey patch it to taste" is probably quite sufficient and far, far less ridiculous than that last paragraph, I just couldn't resist sharing the horrifying thought :)
Yes, ad-hoc fixing styles and behavior from the outside is nice. But when every internal style/class/element is technically part of the API surface, you quickly end up with extremely brittle code.
Suddenly every tiny change (bug fixes, refactors, new feature) in the component library has the potential to break the app in unexpected ways, and the result is an inability to upgrade. Happens all the time.
With shadow DOM, this problem basically go away (depending on how it’s implemented)
Such as what?
The only issue with web components is shadow dom makes styling a pain so you need to work without the shadow dom and implement slots yourself.
Personally I wouldn't mind server rendered features like what react does but in the setting of not javascript.