by rishav_sharan · 6y ago · 0 comments
Every bit of code that we write in any programming language is made of composable bits which can be imported into other bits of code.
But we cannot do anything similar with html. every time any attempt at having composable component based html is made, it gets mired down in arguments, and we end up with Frankenstein monsters like Web Components, Shadow DOMS etc.
Web shouldn't be this hard. but the html governing bodies and the browsers have done a horrible of taking it forward.
HTML was originally meant to be page-based. And pretty much everything added to it (CSS, JavaScript) is scoped to the page. (Some data storage like cookies is scoped to the domain.)
So shoehorning a "component" model to what is fundamentally page-based is essentially an impossible task to do elegantly. Either you wall components off in sandboxes and break the entire premise of the page having control ("why can't I modify the style of my component anymore with the page's CSS?!"), or your components are extremely leaky and accidentally stop working because of page-level CSS and JavaScript interfering.
So we have a ton of different component models that use different tradeoffs.
Obviously if HTML/CSS/JavaScript were being built from scratch today as a platform for apps, it would probably be designed extremely differently, very much around contained components. But that's not what it grew out of historically.
And from a practical, layman’s standpoint, now that we’re all becoming “mobile first”, while it’s natural to complain when a web link points to a PDF file, the resulting app makes the experience more immersive and self-contained, enabling any average user to save that document. Just saving an HTML document with all its linked material using your phone requieres a l337 browser, Evernote, Notion, or wget from a terminal emulator with the right flags... undoubtedly it’s a chore. I agree this is all sad, but by now I don’t see this changing anytime soon.
I’ve griped about this before, but I firmly believe that the trope of JavaScript developers needing a million dependencies and constantly reinventing the wheel stems from the fact that you have to do every damn thing yourself (else install a package or 50). The platform provides almost nothing (except, of course, the most remarkable distribution platform we’ve ever seen). The amount of hoops we all jump through to make building web app UIs tolerable is insane.
Want a filterable select that doesn’t make you type the first exact n characters really fast to find what you’re looking for? Gotta build it yourself. Hope you remembered to make it accessible and responsive!
There isn’t even an in-built option for pop-out menus, a feature of GUIs as old as GUIs themselves.
At the end of day the web is now as much an application platform as it is a hypermedia platform. We need better tools.
Maybe in 10 years we can replace HTML with SVG.
hides
I'm sure that would make it much, much harder for people with accessible needs.
What I don't like is this also shows (via the render method) how you can foot gun yourself if you aren't careful. Just slabbing in everything via `this.innerHTML` will lead to gross performance problems, among other things. There's also no real diffing (only matters on more complex concerns, in a way) so you're replacing everything just to update the count.
I know we're all suppose to know better (I doubt you did this except for the express purpose of showing just how simple the Web Component API can be), but I've seen example code end up in production more times than I can count, from all over the web, so be forewarned stewards!
Personally I'd miss the JSX syntax, but this more than makes up for it by removing the entire whacky state vs. props thing. Although- how do you represent custom props here - arguments to the constructor? That'd be really elegant.
Do those styles and query selectors leak out of the component? I'm assuming they don't since that's the entire point of the idea of a component, but just want to be sure.
<form>
<button onclick="form.counter.value -= 1;return false">-</button>
<input name="counter" value="0">
<button onclick="form.counter.value = 1 + +form.counter.value;return false">+</button>
</form>
Custom elements is just a mutation observer with callbacks, can be used with DOM 0 as well: class MyCounter extends HTMLElement {
connectedCallback() {
this.attachShadow({mode: 'open'}).innerHTML = `<style>...</style><form>...</form>`
}
}
customElements.define('my-counter', MyCounter)https://github.com/rishavs/Verdin/blob/master/src/views/comp...
I just wish that one of these days I will get off my ass and actually finish something that I start :D
And the opinion has shifted from "this is the future" to "Web Component specs is aimed primarily at library authors, we don't expect people to author them by hand"
I mean it’s decidedly not React to be clear, but I’m not sure what makes it atrocious
You can't just say "React" though, you have to show how it actually integrates with the DOM that we have. "Throw away the DOM we have" isn't a realistic option either.
$('#myTable').DataTable()
automatically, there was tiny polyfill<let mytag> <p>my tag!</p> </let>
<mytag/>
Same with css. There are workarounds we use because abstraction is a requirement to code. Somehow the web components spec even seems like a workaround, for which we have many workarounds... and around and around we go :)
Now we think about layout first, then putting text in it. Html is so old back then when browsers did not even have sense of having standard. Html tag is already good though, we now see it as sort of function, attributes are params. Turns out it started with really good design, but different way of thinking.
But flash died, so did flex. (that means, the apache foundation still tries to reanimate flex with a js/html exporter, but I doubt it ever gets stable)
...
<counter xmlns="my" />
XSLT: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my">
...
<xsl:template match="//my:counter">
<form>
<button onclick="form.counter.value -= 1;return false">-</button>
<input name="counter" value="0">
<button onclick="form.counter.value = 1 + +form.counter.value;return false">+</button>
</form>
</xsl:template>
</xsl:stylesheet>
XSLT looks ugly but only because we should not edit plain text XML (same for markup). <template> is a limited version of XSLT.Mind you, there isn't exactly a 1:1 mapping between modules & web-components, with the former being 20 years old at this point.