What is Web Components
Tags: webcomponents, customelement, webdev, frontend
Published: 2020-08-05 11:50:35

---
title: What is Web Components
published: true
description: Web components is one of my favorite browser APIs and also one of the least talked about. It grants many features to developers and allows us to write extremely reusable components that are extremely declarative and easy to consume.
tags: WebComponents, CustomElement, WebDev, FrontEnd
---

While being the future of web development, web components are also my new favorite web paradigm and also one of the least talked about. It grants many features to developers and allows us to write extremely reusable components that are declarative and easy to consume. Let's take a look at it today. 

# The Specification
Web components is a suite of standard web specifications. Their existence means developers can create light-weight, reusable and powerful UI components for the web. Consuming a web component could be as simple as importing a JavaScript module and writing a HTML tag. The main specifications are:

1. [Custom Elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) - allows developers to write custom HTML tags (more on this later)
2. [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM) - provides a black box for developers to write obstructed logic
3. [HTML Template](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_templates_and_slots) - kinda cool, but it has limited use case

I will talk mainly about Custom Elements today since it is the pillar that holds up the web component specifications.

# Some things about Custom Elements

## Platform Agnostic 👍
Custom elements are implemented with a JavaScript class and consumed by simply writing a HTML tag. This means you can use them anywhere. You can use them on your bare index.html file or within [AngularJS](https://angular.io/), [ReactJS](https://reactjs.org/), [VueJS](https://vuejs.org/), literally anything else that runs on a .html file.

## No tooling required 👍
In essence, everything on the Front-end can be categorized into 3 types of logic: HTML, JavaScript and CSS styles. Custom elements embrace this sense of minimalism. To write a custom element, all we need is a JS class to declare a class (the implementation), a single line of JavaScript to define the custom element (the declaration) and calling `<my-new-custom-element />` on the index.html file (the consumption). Unlike React or Angular, the tooling is optional. We can introduce tooling for optimization or specify types or linting, but they are essentially optional.

## Easy consumption 👍
Since custom elements are just user-defined HTML tags, they are incredibly easy to consume. We can just use them as normal HTML elements, passing configurations as attributes and not having to worry about the underlying complexity and implementation.

## Too lightweight 👎
Out of the box, custom elements only offers [a handful of lifecycle events](https://ultimatecourses.com/blog/lifecycle-hooks-in-web-components). This is a very slim selection as compared to the React components of the world. To combat this, tooling such as [StencilJS](https://stenciljs.com/) and [Polymer](https://www.polymer-project.org/) exist to provide a higher-level API while being very performant since they eventually compiles down to a Custom Element.

# Who else is using Web components?

## Github
In a blog post from 2018 titled ["Removing jQuery from GitHub.com frontend"](https://github.blog/2018-09-06-removing-jquery-from-github-frontend/), GitHub engineers talked about how [certain UI widgets](https://github.blog/2018-09-06-removing-jquery-from-github-frontend/#custom-elements) are implemented with Web Components.

## Ionic framework and design systems in general
[Digital design systems](https://ionicframework.com/docs#framework-compatibility) are the perfect use case for web components. Once it is written, the individual UI components should as much as possible, across as many platforms as feasible. Writing or exporting them to Web Components means they can be used anywhere regardless of front end framework. I wrote a piece about my internship experience at Staizen and mentioned how they utilize StencilJS (a web component based abstraction) to write their design system while exporting the components to ReactJS apps. Check it out [here](https://xingzhi.dev/blog/what-did-i-do-this-past-summer-break/).

# How future proof is this?

Front-end frameworks are constantly changing. This is one of the biggest gotcha that all front-end developers face. As a consequence, we often see developers showcasing their blogs on [dev.to](https://dev.to/), talking about how they write their blogs without a JavaScript framework. I understand where they are coming from and this is where web components shine. Since web components are browser specifications adopted by [W3C](https://w3c.github.io/webcomponents/) and [WHATWG](https://wiki.whatwg.org/wiki/Component_Model), they are here to stay. This means custom elements I write today will still be maintainable for quite a while with minimal effort. I cannot say the same about my tooling-heavy React applications. 

---

# Additional Reads

https://developers.google.com/web/fundamentals/web-components/customelements

https://css-tricks.com/an-introduction-to-web-components/

https://www.robinwieruch.de/web-components-tutorial

https://stenciljs.com/docs/introduction

https://stenciljs.com/docs/overview

---

*This blog post was originally posted on [xingzhi.dev](https://xingzhi.dev/blog/what-is-web-components/)*