Web components Fundamentals
Tags: webdev, javascript, html, webcomponents
Published: 2021-08-06 00:41:40

###What are Web Components?
Web component is a term used for four web standards that help us make custom, reusable and encapsulated html tags that can be used in web pages and applications.
**1. `<template>` elements:** With the `<template>` elements, we encapsulate html, and then render it using javascript.

>So what happens is that template elements break apart html. Whatever is contained inside the template tag doesn't get rendered at load-time. Instead javascript renders when the function is called. So a page would load very quickly, since we have part of html that doesn't load at load-time. We can also reuse these elements, so we save a lot of time.
![Screen Shot 2021-08-04 at 9.33.15 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/le5f869t7cmrnp9cdx2n.png)![Screen Shot 2021-08-04 at 9.33.32 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ald27azuinloteo2vfs4.png)
  
  

**2. Custom elements:** With custom elements, we create a javascript class, and then link it to a custom html element.

>An html document already ships default html elements. E.g `<body>`,`<header>` etc. An HTMLElement class contains all these elements. Inside a javascript file we define a class and extend the HTMLElement, with this custom class. Next, we define a custom element, and then link it to our class. We can now use this custom element inside html as many times as we like.
![Screen Shot 2021-08-04 at 3.50.16 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mupm6qcksqcilpfcjxpg.png)![Screen Shot 2021-08-04 at 3.50.24 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vfq3puj50sefl16u7jb.png)
Notice the `-` in the custom element; that is important when making custom elements, to differentiate it from default elements.

**3. Shadow DOM:** 
With a Shadow DOM we encapsulate styles, so that we can limit them to a specific area.

>A DOM, Document Object Model, is a tree like structure of a documents elements, in our case html's elements. Code and styles contained here affect one another.

![dom-screenshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m67k3aha9ldcjx8dqfso.png)

>A Shadow DOM on the other hand, is a separate DOM, and the code contained inside the Shadow DOM is separate from the main DOM, so it does not affect the code of the main DOM. We can attach a Shadow DOM to an element, using javascript, and it would only affect that element.

![Screen Shot 2021-08-05 at 10.06.12 AM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nhg3vfqmtfvkkzrpcqe.png)
 
**4. Javascript Modules:**
We can convert a javascript file to a module by exporting it. This would allow other files to import it, by using it as a dependency. 

>So a module is a file that exports its own code. Modules usually have their own scope. So we could have a modules with different functionality, that people can then share with others. So we could share a module called `mediaplayer`, that could play media; or another one called `whattimeisit`, that could display time in different time zones. Npmjs is a popular package manager that people use to share modules.
![Screen Shot 2021-08-05 at 10.53.42 AM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3oyrws8p20r3hntpzkb3.png)
 


###Why use Web Components?

**Future proof:**
There have been many frameworks that have had a dramatic rise in popularity, but have been replaced by other newer frameworks. The great thing about web components is that its usage has increased over the years.
![trends-minor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dy0wlyj6ilo3qr5zvhr9.png)![RYVwr119hwQIps5UL0u4 copy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrsoo4d3aejvduvwzcix.jpg)
 
**Can be run by any browser:**
Another great thing about Web components is, is that they were added by the World Wide Web Consortium, W3C, as a standard. The W3C defines the standards for browsers to implement. All five major browser have added support for web components. 
![which-browsers-support-web-components](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zh1lkwczfa024tukq8tj.png)

**Can be used with any framework:**
Since it is a part of code, that can be run by the browser, usage by any framework is not an issue.![1*4cFhtuq6zRDqJ6p4s2pQ6g](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eoi8n68mt358qxy99bev.jpeg)

**Write cleaner code:**
One of the best things about web components is, is that web components helps us write cleaner, and better code. This would improve performance, increase accessibility, and make it easier to maintain. ![1*OW9_FTfEMmSWpMSSQtFhIQ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ncyvnkhs2je3sjlclypr.png)
 
**Improve reusability:**
You could save a lot of time, by reusing web components. This would also mean that less developer hours are spent, and the productivity would increase a lot. ![Screen Shot 2021-08-05 at 2.42.46 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xl9kbphxz0hjc2d0r5v1.png)
 
**Improve Consistency:**
When starting a new project, there might be old components that could be reused. So amongst different projects, a consistent look and feel can be created.![1*SlAPqCmjtwvRsn0Kszi7oA](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0n41jj3hjyxe1snxo2wl.png)
**Improve Accessibility:**
By reusing old components, we make it easier for users to use a specific feature again, in a different part of the project.

Any accessibility features that have already been implemented can also be reused by us, or made available to use by others. E.g a simple search of accessibility on npmjs gives us more than 1350 packages to work with.![Screen Shot 2021-08-05 at 2.31.53 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cya69qucx07lgb4xjqs4.png)
 

###Who uses Web Components?
**Github:**
Github makes use of web components. As discussed before, an element with a `-` in between means that it is a custom element. The `include-frameworks` in Github's homepage after login, is an example.![Screen Shot 2021-08-05 at 3.02.52 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rjt2l10stns0q5h5hruw.png)![Screen Shot 2021-08-05 at 3.40.23 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0r20mdkf8s5aw38u15z.png)
 

**Salesforce:**
Salesforce also makes use of web components. An example of a custom element is `dx-instrumentation` in Salesforce's developer page.![Screen Shot 2021-08-05 at 3.17.27 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y0nz488sb820yp9kb0p.png) ![Screen Shot 2021-08-05 at 3.41.04 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ln8jebdt2c6ep00bj3i.png)
 

**EA:**
EA uses web components. Here is an example of the sims website using web components. It makes use of the iron-a11y-announcer custom element, that adds a11y to features that require on-demand announcement from screen readers.![Screen Shot 2021-08-05 at 3.35.28 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nmccyzhz62txzsh4pxg1.png)![Screen Shot 2021-08-05 at 3.41.32 PM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9c74knsqha6rd19ia21g.png)
 
  

###Links to learn more:
Here are some links that you can use to learn more about Web components:
1. [Templates](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_templates_and_slots)
2. [Custom Elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements)
3. [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM)

###Video:
And here is a video where I explain web components:
[Youtube](https://youtu.be/0UvDjhO8j94)