DEV Community

Cover image for Web components Fundamentals
Hamza Siddiq
Hamza Siddiq

Posted on • Updated on

Web components Fundamentals

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 PMScreen Shot 2021-08-04 at 9.33.32 PM

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 PMScreen Shot 2021-08-04 at 3.50.24 PM
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

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

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

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-minorRYVwr119hwQIps5UL0u4 copy

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

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

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

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

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
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

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 PMScreen Shot 2021-08-05 at 3.40.23 PM

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 Screen Shot 2021-08-05 at 3.41.04 PM

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 PMScreen Shot 2021-08-05 at 3.41.32 PM

Links to learn more:

Here are some links that you can use to learn more about Web components:

  1. Templates
  2. Custom Elements
  3. Shadow DOM

Video:

And here is a video where I explain web components:
Youtube

Top comments (0)