The Perks Of Web Components: Perks & Flaws Series – Part 2

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.

In this article:

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:

				
					<html>
  <head>
    <script src="fancyFramework.js"></script>  
  </head>
  <body>
    <div id="fancyWidget"></div>
  <script src="https://www.thinktecture.com/core/cache/min/1/c87fe83224b41ea5d782dd4f6d4cf337.js" data-minify="1"></script></body>
</html>
				
			

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:

				
					<body>
  <div id="fancyWidget">
    <span class="label">Label</span>
    <button class="increment button">+</button>
    <button class="decrement button">-</button>
  </div>
<script src="https://www.thinktecture.com/core/cache/min/1/c87fe83224b41ea5d782dd4f6d4cf337.js" data-minify="1"></script></body>
				
			

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:

				
					<html>
  <head>
    <script type="module" src="my-counter.js"></script>  
  </head>
  <body>
    <my-counter value="1" minimum="-5" maximum="5"></my-counter>
  <script src="https://www.thinktecture.com/core/cache/min/1/c87fe83224b41ea5d782dd4f6d4cf337.js" data-minify="1"></script></body>
</html>
				
			

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:

				
					<html>
  <head>
    <script type="module" src="my-counter.js"></script>
    
    <style>
      label {
        color: red !important;
      }
    </style>  
  </head>
  <body>
    <my-counter value="1" minimum="-5" maximum="5">
      
      <label>A label</label>
    </my-counter>
  <script src="https://www.thinktecture.com/core/cache/min/1/c87fe83224b41ea5d782dd4f6d4cf337.js" data-minify="1"></script></body>
</html>
				
			

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.

More articles about Web Components
Free
Newsletter

Current articles, screencasts and interviews by our experts

Don’t miss any content on Angular, .NET Core, Blazor, Azure, and Kubernetes and sign up for our free monthly dev newsletter.

EN Newsletter Anmeldung (#7)
Related Articles
AI
sg
One of the more pragmatic ways to get going on the current AI hype, and to get some value out of it, is by leveraging semantic search. This is, in itself, a relatively simple concept: You have a bunch of documents and want to find the correct one based on a given query. The semantic part now allows you to find the correct document based on the meaning of its contents, in contrast to simply finding words or parts of words in it like we usually do with lexical search. In our last projects, we gathered some experience with search bots, and with this article, I'd love to share our insights with you.
17.05.2024
Angular
SL-rund
If you previously wanted to integrate view transitions into your Angular application, this was only possible in a very cumbersome way that needed a lot of detailed knowledge about Angular internals. Now, Angular 17 introduced a feature to integrate the View Transition API with the router. In this two-part series, we will look at how to leverage the feature for route transitions and how we could use it for single-page animations.
15.04.2024
.NET
KP-round
.NET 8 brings Native AOT to ASP.NET Core, but many frameworks and libraries rely on unbound reflection internally and thus cannot support this scenario yet. This is true for ORMs, too: EF Core and Dapper will only bring full support for Native AOT in later releases. In this post, we will implement a database access layer with Sessions using the Humble Object pattern to get a similar developer experience. We will use Npgsql as a plain ADO.NET provider targeting PostgreSQL.
15.11.2023