Article Series
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.
Reusability
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.
Semantics
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:
Label
You can notice that some of the selectors are generic, label
or 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:
Several things have changed here! At first, we have a bundle import. Importing the my-counter
JavaScript module also includes the necessary stylesheets. 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 value
, a minimum
and a maximum
.
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.
Encapsulation
As mentioned in the paragraph before, a Web Component is locally scoped. It is encapsulated because of Shadow DOM. Consider the following sample:
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.
Business Perks of Web Components
In this section, we are exploring in which ways Web Components can help your business.
Reusability
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.
Browser Standards
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. It does not happen anymore that someone cannot help in a project, because he is an Angular developer, but the project is using React.If both use Web Components, your tools are JavaScript, HTML, and CSS. Languages, which are well-known to any experienced Web Developer.
Summary
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. On the one hand, we have the JavaScript APIs. On the other hand, there are CSS APIs with Shadow Parts and CSS variables. Thinking about and shaping your component’s API helps to create small, reusable, and very specific Web Components.