In the introduction article to this series, I wrote about the motivation, why to use Web Components.
Now we are going to take a closer look at the perks of using them - from a technical and business point of view.
If you are interested in all technical features of Web Components, I would recommend a more in-depth article from my colleague.
- The Motivation for using Web Components, an Introduction
- Perks of Web Components ⬅
- Flaws of Web Components
- Data Sharing and Frameworks
Technical Perks of Web Components
In this section, we are taking a look at the technical perks, which help you as a developer to create, use, maintain, and share your precious Web Components.
Web Components rely on the browser's native component model implementation and not on other frameworks.
That makes it easy to reuse the component across multiple applications - either your in-house or external ones.
Additionally, every current SPA framework can consume Web Components and, if needed, wrap it into a framework component providing additional services.
You can develop your component once and use it everywhere you like.
If you think of good old components years ago, you may have seen code similar to this:
Taking a look at it, there is literally no semantics.
You only have a
div element with a specific
id on it.
However, you don't know anything about it.
It is not visible if that is a component at all, and if so, which one it is.
Additionally, without having more documentation, there is no hint about the component's API.
Depending on the component, you may need to include one or several stylesheets as well.
At runtime, the
fancyFramework may expand the
div with additional elements like this:
<button class="increment button">+</button>
<button class="decrement button">-</button>
You can notice that some of the selectors are generic,
button for instance.
Either your own application or another third party framework likely defines the same classes. That leads to overwriting styles and breaking your UI.
If the expansion would insert elements having a specific ID, the component is single-use.
According to the specification, an ID should be used only once per page.
Taking a look at Web Components the sample above will look like that:
<script type="module" src="my-counter.js"></script>
<my-counter value="1" minimum="-5" maximum="5"></my-counter>
Several things have changed here!
At first, we have a bundle import.
Everything is in one place, and you don't have to import anything else.
Instead of having a
div tag, we now have a
my-counter tag due to the usage of Custom Elements.
It is adding much more semantic to the code because you precisely know the component used.
Due to the attributes, it also exposes the API of the component.
So you can see that this component has a
minimum and a
Last but not least, it has a local scope due to Shadow DOM.
Neither there is style bleeding into the component, nor a style overwrite from the component.
Your UI does not break.
As mentioned in the paragraph before, a Web Component is locally scoped.
It is encapsulated because of Shadow DOM.
Consider the following sample:
<script type="module" src="my-counter.js"></script>
color: red !important;
<my-counter value="1" minimum="-5" maximum="5">
Without the usage of Shadow DOM, the style definition from the main document would bleed into the component, coloring the label red.
Even if there is another styling defined in the component itself, the
!important overwrites it.
However, since your Web Component uses Shadow DOM, no style bleeding occurs.
No matter how you try to overwrite it, the browser ignores CSS outside of the Web Component.
Except if your Web Component defines CSS variables, then you can use those variables in your main document to overwrite the ones defined in the Web Component.
And that's a good thing because you, as a component developer, are responsible for creating those CSS variables.
Basically, you build an UI API for your consumers, where you want them to be able to overwrite certain aspects, like sizes or colors.
Besides, you can also use CSS Shadow Parts for defining elements that can be styled by the user.
Again, that's good, because you can specify which elements can be styled - it is your API.
As mentioned in the introduction, if you are curious about more in-depth technical aspects of Web Components, check out my colleagues' article about native Web Components without a framework.
Sorry to interrupt your reading of this article, but it seems that you're really interested in Web Components. Did you know that we're also publishing a free monthly newsletter about this topic and more?
Business Perks of Web Components
In this section, we are exploring in which ways Web Components can help your business.
We talked about this in the technical perks section above already, but it is worth mentioning on the business side, too!
Since we can share Web Components easily, that also implies we have to develop them only once.
We have one code base (to rule them all) we need to manage instead of multiple ones, with possible re-development of already existing components.
That helps tremendously to write high quality and highly tested code.
Also, it includes that code is easier to maintain.
All in all, the overall developer experience gets better.
But, you still need proper PR review processes, linter rules, auto-code formatting tools, and further. The benefit is that everyone, every SPA framework, and every website can consume your Web Components.
Additionally, Web Components help to increase the consistency of your application, the look and feel of your brand.
Since all teams use the same components, the applications look and feel the same.
The developers can focus on creating specific features for each application without having to redevelop the brand's look and feel.
In the technical section, I have mentioned the usage of several browser standards like Shadow DOM or HTML Templates.
Utilizing them and training people on the standards makes it easier to share knowledge and developers between projects.
Languages, which are well-known to any experienced Web Developer.
In this article, we learned a lot about the perks of Web Components.
They are not only a time-saver in development but also a money-saver in business due to the high reusability and hopefully better maintainable code.
When creating Web Components, we also need to think about our APIs.
Thinking about and shaping your component's API helps to create small, reusable, and very specific Web Components.
Where perks are, there are also flaws. Find more about the downsides of using Native Web Components in the third part.
Sign up for our monthly newsletter to make sure not to miss it!
Manuel Rauber hat zum Thema Web Components ein Cheat Sheet erstellt, auf dem er kompakt alles Wissenswertes zusammengefasst hat. Melden Sie sich hier zu unseren monatlichen DevNews an und wir sende Ihnen das Cheat Sheet per E-Mail.