The following is an attempt to stomp out the seemingly endless FUD that circulates about Web Components, most of which is purely manufactured by attacking tangential, opinionated choices of frameworks that happen to use Web Components in a way that differs from the opinions of other frameworks.
Level Setting
Don’t let me catch you claiming a Web Component is anything but the following, or I’ll send Kam Chancellor to lay you out:
Web Components are an amalgam of APIs from two W3C specs (Custom Elements and Shadow DOM) that enable the creation of encapsulated, declarative, custom elements, which serve as standard, reusable, interoperable vehicles of discrete, stateless functionality.
Myth 1: “Web Components are at odds with Framework X”
This is probably the most fallacious statement about Web Components.
As long as a framework writes to the DOM (which all major ones do), there is no reason it can’t include Web Components or update a Custom Element with data and state changes (as opposed to an amorphous tree of rando divs).
Let’s say I have a single page React app that’s a clock, where you select a timezone and the clock UI reflects the right time – here’s what I would do:
Write the actual clock element as a Web Component. All this <x-clock>
element needs to do is understand one attribute, for example data-zone
, that specifies which timezone to display, and print out a clock UI showing the correct time. It shouldn’t be aware of anything else about the app – this shit just tells time, yo. From here you can Reactify the living hell out of everything else (your route handling, how you save the state of the selected timezone, etc.) without ever having to hard-bind the <x-clock>
element’s definition code to a framework.
When you follow this pattern, you end up with elements of discrete functionality that can survive a change in framework, data/change management library, storage adapter, etc., and still develop your app with the framework or library of your choosing. Oh, and you also get to feel like a good Web Citizen, because that <x-clock>
component you made is now available for use across the entire web, without requiring someone to have advanced programming experience or worship at the Church of Framework X.
Myth 2: “If I want to use Web Components, I have to use HTML Imports”
I’m not going to spend much time on this because it’s so blatantly false.
HTML Imports is a proposed standard for importing an HTML document and its includes into a parent context. If you want to bundle your JS and CSS files into an HTML Import, go ahead, but Web Components are just JS and CSS files that have no dependency on how you include them in an app.
Myth 3: “Web Components require me to change my packaging system”
As previously stated: Web Components simply provide you two JS APIs (Custom Elements and Shadow DOM) with which you can define new elements and register them for use in your app. This means all a Web Component requires is a JS and CSS file; for multiple components you can concatenate them together, like you would any other includes (note: if you choose write your CSS in JS, you obviously wouldn’t need a CSS file)
Because this is just regular ol’ JS and CSS, don’t go chasing new packaging waterfalls on account of Web Components, you can stick to the packaging rivers and lakes that you’re used to.
Myth 4: “Web Components aren’t declarative”
This is perhaps the most mythstifying of all, given Web Components literally enable the end user to write declarative HTML markup extended with Custom Element tags. Frameworks like React (w/ JSX) are great if you prefer them, but in terms of categorization, something like JSX is a non-native, pseudo-declarative DSL with embedded logic hooks, vs the native, purely declarative markup of Custom Element tags written in regular ol’ HTML.
Myth 5: “Web Components aren’t compatible with JSX, they only accept string values!”
If you’re using a DSL markup layer like JSX, there’s no technical reason why a system like that can’t interoperate with Web Components. A conversation on Twitter with various folks in the ecosystem prompted me to address this myth. During the conversation someone noted the basic flow of JSX to React imperative code and asked how it would look with a Web Component. Here’s the pure JSX/React we started with:
// This JSX Markup:// Turns into this imperative React code: React.createElement(Foo, { bar: 1 }),
The question was how the above code would look if JSX was to support and interoperate with Web Components. The key requirement is any object type must be able to be passed to a Web Component. This isn’t an issue, as Web Components (and HTML elements in general) natively support methods and getters/setters, in addition to attributes. This means that you could route any type of an object to a Web Component from a JSX context as follows:
// This JSX Markup:// Turns into this imperative code: var foo = document.createElement('x-foo'); foo.bar = 1;
Whether or not JSX is already written to detect and reflect data to Web Components via setters/methods is immaterial – the point is: it should be trivial to detect a Custom Element token in the JSX parser and pass data to its corresponding instance. I have inquired about where in the React/JSX lib code this data relay takes place, and I am willing to help modify the code so Web Components can be seamlessly included.
Myth Status: BUSTED
So now that we’ve got all that straight, I encourage you to never utter these myths again.
Interesting, where did you find myth 4? I’ve never seen such claims
I get confused on one point, regarding them being vehicles for stateless functionality, inputs natively have state (value property). It seems this can be dealt with in various deals to work in stateless architecture… do you have a favorite article explaining how best to think about this? Thanks.
Question for me: What is the advantage of using WCs over using code in my main framework. Writing a clock with mithril/react is pretty simple and I have all freedom to implement it to my specific use case. I don’t need zillions of options and get a 2kb code-ball that I don’t understand. It’s also hard to reuse any stuff I already build for my app (think of i18n or tooltips).
I don’t want to trade this flexibility and simpleness with some standards-driven black-box-magic-tech. I for myself would rather reduce the amount of DOM-functionality rather than increase it. Even number input field is PITA. Not to speak of forms in general.
DOM-els should be solely used for semantics and only have minimal functionality. Rest should be done in JS.
cmx66 a few reasons:
* If we all choose one of 20 libraries and frameworks to hard-bind our components to, all we have to do to democratize declarative web development is include every library and framework’s full package on one page. Everyone’s components will work…right after 10MBs of JS load and wreak perf havoc on your app.
* Let’s take bets on which will be optimized best in the long run: 1) Every rando lib/framework’s individual code paths executing in ways the browser can’t possibly pay specific attention to, or 2) a set of standard APIs and hot paths the browser understands naively, which are integrated into its privileged code and subsystems – I’ll take #2 for the win.
* What exactly is “black box” about DOM and vanilla JS code that requires 0 extra knowledge, and is written against standard API interfaces? I feel like your comment on that point is diametrically opposed to the obvious result.
tpluscode huge thread with devs from a couple frameworks who had some, shall we say…uncommon, views on the definitions of declarative and imperative.
csuwldcat cmx66 TLDR; Standardise virtual DOM.
1) Why not have 20 versions of a component? There are thousands of developers out there, why can’t we have competing versions of the underlying tech and also the solutions. How many react boilerplate projects exist to day. People never agreed on one version of a thing. Competition is good!
2) Since, as you stated, WCs are just JS and Shadow-DOM you may not optimise this without optimising the whole DOM. That will also bring advantages too VDOM libs. Also authors of VDOM-Libs like inferno.js or mithril.js have found very effective ways to manipulate the DOM. Only a few WCs-authors might go this deep into optimising that.
3) Look at select2 from jQuery. Even if you know jQuery, you can’t understand this right away. Also consider you have a select WCs and now want to add i18n or tooltips to every entry. Or you want to use a separate “template” for the entries. If you build a WC’s that is flexible enough to do this I don’t want to see the options it has to have. With VDOM this is pretty simple to build for your own and tailored to your specific use case, fast, small and maintainable.
Good post! Regarding myth 5 (and perhaps all web components): It’s imaginable that web components from different vendors would include their own template/html parsing and updating library. So then, at this point, an application would be including e.g. react, underscore, handlebars, etc., if the application is including external web components that each depend on something different.
This is an important part so I can’t count it as immaterial. The update/render api is an important part. Without a standard, each library importing their own rendering library isn’t a big win. The only win web components give you here is scoped styles.
1) You seem to be misunderstanding. I’m not against competition, in fact, it want more of it. Having 20 different versions of a component is fine, but having 20 that can be mixed, matched, tried, and tested without learning 10 different frameworks is even better.
It’s kinda like the internet and websites: we have tons of different website options for every type of app, and that’s because we have one internet and Web platform they can all be accessed through. The situation in Component Land right now isn’t like having 20 different websites and great competition built on a standard web platform layer, it’s like we have 20 different internet silos that don’t interoperate and force users to pick a silo. That sucks.
2) Yeah, not sure what you’re getting at – Web Components aren’t VDOM, I definitely suggest you leverage a VDOM library to incorporate that functionality.
3) Again, VDOM is an enhancement that you can mix with Web Components. To say “You have to pick either Web Components or VDOM”, is a completely inaccurate false choice.
csuwldcat I’d love a link 🙂
csuwldcat
Maybe you’re right.
I’m just concerned about introducing more and more stuff to the already pretty overloaded DOM-API. As stated I would rather shave things of.
Let’s look in 2-3 years how this turns out.
tpluscode in order to be respectful to the developer (who is a friend), I would rather not disclose his name.
I beleive demystifying word should be written as mystery, not myth as it originated from the first one.
In your rebuttal to myth 5, you suggest monkey-patching DOM nodes with arbitrary properties.
This seems dangerous, especially because, for any property name the developer picks, it’s possible that that name could be used in a future DOM standard.
maxheiber No, that in not accurate. For the purposes of the example (the common foo/bar filler values), I used a property `bar`, but in practice you can just as soon use any `data-` property to ensure 0 conflicts with current or future native HTML properties.
pepelsbey This was an intentional play-on-words, because I was ‘busting myths’ – get it?
csuwldcat, oh, got it now 🙂 Sorry about that, I just wasn’t sure myself if it’s a pun or mistype.
csuwldcat maxheiber `data-` attributes are part of the HTML standard, but not part of the DOM standard (https://dom.spec.whatwg.org/).
maxheiber again, your statement is not accurate: The WHATWG DOM spec you linked to is not the only place DOM API specifications surface, they appear throughout the full W3C and WHATWG HTML spec documents. In this case, the `data-` attribute specification and its matching `dataset` DOM property specification are included by both bodies:
–Â https://html.spec.whatwg.org/multipage/dom.html#dom-dataset
–Â http://w3c.github.io/html/dom.html#dom-htmlelement-dataset
I would kindly ask that you research such things more closely before making claims like this. Our interaction here is a perfect example of how FUD is created and propagated in the developer ecosystem.
csuwldcat maxheiber Thanks! Actually, I didn’t know about the other spec documents, nor did I know about the DOMElement.dataset property. I researched and just didn’t find them. Thanks the education.
csuwldcat After reading your article I could resist my self from creating this tiny library called RWC (Reactive Web Components https://github.com/tusharmath/rwc). Essentially its creates the shadow dom using a virtual dom library.
People wear watches will give the http://www.swisswatchjust.co.uk/ impression of a moment of strong sense of power, elegant image. Time and strong sense of dealing with people simply make people work more simple, successful.This watch can improve a person’s grades. Just like a small fry favorite with the gold chain, and successful people like to wear watches. Good watches exquisite workmanship, elegant appearance, can display the charm of women and men.This watch is also a sign of http://www.rolexreplicasstore.uk.com/ personal identity, man watch, female bag, said the reason is clear. A small piece of fine watches a few thousand dollars, more than 10000 yuan, able to bring expensive watches are usually very successful in the work of people.