Component-based UI is all the rage these days. Did you know that the web has its own native component module that doesn't require the use of any li...
For further actions, you may consider blocking this person and/or reporting abuse
Benny; thank you so much for these excellent articles!
I do have a question for you. Now that you've been into litHtml and litElements for a few years now, do you still feel this is the best way to go with respect to web components?
It all looks very good to me, but I don't see widespread adoption. I don't really care what the adoption rates look like if the framework is hands down the best. I'm willing to put time into it if it is the best one.
Any advice?
Hi John, thanks for the kind words
Yes, I do consider
LitElement
to be the best option for making web components in September 2020, for these reasons:Platform
Of all the major ways to make a web component that I'm aware of,
LitElement
strikes the best balance between providing features the web platform doesn't (reactive updates, data->ui templating), while still respecting and leaning on existing APIs. ALitElement
factors, reads, and behaves exactly what it is - a spicyHTMLElement
.That also means that more of the knowledge you gain from working with
LitElement
is transferable. I've often had the experience working with developers familiar with a certain popular web framework of them asking me about "how to do X with lit", only to realize after considering their question that they really wanted to know how to do X with the DOM, or CSS, or JavaScript.To be certain,
LitElement
has some opinions of its own, notably asynchronous rendering andupdateComplete
(which is actually kind of a super power), and you'll have fun getting to know how to take advantage of them, but most of the action here is web standards.Simplicity
To paraphrase @webpadawan - when Vaadin was evaluating
LitElement
for adoption, one of the selling points for them was that it was simple enough to grok on its own that if they ever needed to, they could fork and maintain their own version without too much trouble. lit-html andLitElement
have relatively few moving parts. With some dedication and a pot of coffee, you could probably get through the codebase in an afternoon. The github.com/Polymer/lit-element has about ~1700 lines of idiomatic TypeScript. github.com/Polymer/lit-html has about 3471 (2080 if you don't count the directives). And the next major versions will be smaller and faster, with more stuff opt-in.Adoption
I'm unfortunately still aware that a certain type of personality in the industry would prefer we all think that no one's using web components, but they're gaining adoption among small and large companies. Now, npm download stats don't tell us as much as we think they do, but nonetheless, lit-element is gaining on its peers
Community
Come join us in the Polymer Slack (I know, slack... what can we do). It's a welcoming, helpful and passionate community.
TL;DR
So yeah,
LitElement
is where it's at, and I hope you have a tonne of fun learning and using it.Just curious Benny; how long have you been working with it?
It just seems almost too good to be true, but then again it's coming from the strong folks at Google, who have more than proved themselves with Angular and other things like the V8 Engine and Node.js.
I liked React in the beginning but not so much now. I think it's highly opinionated.
I've been using web components since polymer 1. You can think of LitElement like polymer 4.
With LitElement and a nice modern toolchain your going to get a very similar developer experience, but with less overhead.
Check out modern-web.dev and open-wc.org for more
Hi Benny,
Thanks for this article. I have a question, I have a use case where I have to write a module which should work on React, AngularJS and Angular 2+.
So i thought to create a web component for it and use it on all other frameworks. I was wondering that how to deploy the web component code and use it in other frameworks.
It would be really helpful of you if you could help me with any example.
Thanks in advance.
You're spot on. You should definitely publish a web component for this case.
Take a look at open-wc's recommendations on publishing
open-wc.org/guides/developing-comp...
Thank you Benny. At the moment I'm exploring which framework or library to use, so far have explored lit and stenciljs. I'm getting attracted towards stencil.js as I can get community help for stencil more than lit.
Thank you for your reply. I will check this documentation and then will finalize which one to use. :)
join the Lit and Friends Slack for community support
lit.dev/slack-invite/
Congratullations! Excellent article! You wrote "Unlike Polymer elements, with their two-way-binding templates, lit elements are particularly well suited to the types of one-way data flows popularized by the React/Redux pattern and others". Do you mean that lit elements isn't well suited for a webcompnent where the user enters data? You mentioned stock. I can imagine easily a webcomponent exposing stock status as an one-way (only from server to front). And if you was webcomponent for a shopping (from front to server)? Wouldn't you recommend lit-html? If so, what do you recomend if I want to create a webcomponent to be part of my corporarte webcomponet library and used for user inputs?
Just the opposite. Because of the one-way data flow, lit-html encourages you to explicitly and declaratively handle user input.
Polymer-Style Two-Way
At any given moment, what does
theValue
represent? Is it the state held by the parent and passing down to the child? Is it the new input being sent up to the parent by<my-input>
?One-Way Binding with lit-html
Here, the developer knows exactly what's happenning.
theValue
is only ever passed down to<my-input>
.input
events are handled byonInput
(which might set the state in the parent, or in a state container, or whatever you choose)So for your company's component library, I recommend either implementing a decorator component:
Or implementing
<input>
in the root of<my-input>
, and listening for the composedchange
event on that. Make sure to handle your state internally, e.g.this is a great article. the cons part needs to be updated. since you've written this, both lit-html and lit-element are stable
Thanks Florin, I've updated the content
Thanks, I'll be publishing some updates to this article shortly
I am still confused on why I will want to convert properties to attributes and vice versa. Can you give me one use case?
Hey, sure
So, properties exist on the DOM, which is the browser's JavaScript object representation of the Document, but they don't exist at all in the document's HTML markup.
One big difference between the two is that (for the most part), CSS can't see the DOM, it can only see the markup.
Here, we use
reflect: true
in the property descriptor forXElement#schwifty
to indicate that setting the property should reflect to the attribute.That could be useful for a document author, for example with this css:
Another use case along these lines could be setting a
disabled
attribute, or setting ARIA attributes based on an element's DOM state. I set theerror
attribute based on a caught error'smessage
property in<stripe-elements>
, as a convenience while debugging.You can similarly think of cases where a component author would specifically not want to leak internal state outwards to the document, like when some intermediate value is observed so that it causes re-render, while remaining a private 'implementation detail' of the element:
Thank you!
Small edit to the second example: set
content
descriptor withattribute: false
, It can only be set with a property nowHi, Benny. Great post. I have a question: is there a package like React's Enzyme for Web Components to use with Jest? Or what option could I choose for TDD?
Take a look at open-wc.org/testing
Thanks Benny! I ended up with Karma and Mocha :)
Great! It's what I'm using for Apollo Elements and so far so good