Web application development is a pretty crowded field of technology. There are different types of frameworks, libraries, and tools. While developing web apps, the main objective is delivering high-quality user interfaces (UI) with encapsulated components. Show Therefore, when you use frameworks like React, Vue, Angular, or any other front-end framework, you break everything in your UI up into components. However, we can do something similar without even using a framework or a library, and that’s plain JavaScript (Vanilla JavaScript). This article will talk about what web components are and why they are helpful. Finally, it will take you through how to create a web component from scratch. Table of contentsPrerequisitesWeb components are a fantastic and interactive guide. Therefore a good understanding of HTML, CSS, and JavaScript is needed. What are web componentsWeb components are sets of specifications that add functionalities and features to web pages and applications. For example, they allow reusability and encapsulation of HTML elements. The web components enable you to build functions that can be reused on a different web platform, application, or page. Web components consist of three leading technologies to create custom elements that can be reused when you like. They include:
Web browsers including chrome, firefox, and edge offer support for web components. That means technologies, i.e. (Custom Elements, Shadow DOM, and HTML templates) are supported fully. This image from webcomponents.org shows the current web components browser support. Why use web componentsCode reusability has proven to be useful, and before the initiation of web components, it was not easy to implement a reusable user interface that works across diverse frameworks and projects. Web Components allow web developers to build reusable UI’s using built-in web APIs. This means we can create a web component for one project and carry it to another with no additional coding. Creating a web component with vanilla JavaScriptThis tutorial creates a web component with or without the help of any framework or libraries. We are going to create an employee card web component. It will contain a user image, name, id, job title, phone, and email. Below is the user image we will use: We will begin by creating an 0 files. Then copy and paste this code:
To create an employee card web component, create a 1 file. Let us name it 0 copy and paste this code there:
The code above shows how to create an employee card web component, so let us understand each part step by step. Create the template
The first step is creating an HTML content template element 3. The 3 element holds HTML that is not rendered immediately after running a page.Load the template with content
The template element is stored in a 5 variable and linked with 6 properties. The 7 property sets the HTML content on the element. Therefore, we can add the HTML we want to display on the screen, such as 8, 9, and more HTML elements.Create the HTML element
We create a class 0 that extends the HTMLElement class. The HTMLElement represents all the HTML elements.Encapsulate the HTML element
We have an empty class. Next, we create a constructor and call the base class 1 method to inherit the features of a class.A shadow DOM is created, i.e, 2 and becomes the encapsulated part of our web component. It keeps our component’s behaviour independent and from the rest of the Html.You can have your shadow DOM mode closed or open. This means whether you can access shadow DOM via JavaScript in your components. Web Components enable us to use shadow DOM, a feature built into the browser. So if child elements are added to a shadow DOM of a component, they are wrapped inside a shadow root.
The web component API then gets attributes name and avatar to be displayed on the page. Lifecycle methods
There are four callback methods defined inside the custom elements. They enable the code to run when events occur in an element. Let us take a look at some of the callback methods:
Creating custom elements
Our web component has been created, but there is one more step remaining: creating custom elements. We use the JavaScript class 0 while creating the custom elements. Whereby the class inherits the properties of 8.Finally, to test if we created an employee card web component, open the ConclusionWith this tutorial, you have learned how to build a simple web component with vanilla JavaScript. Hopefully, you have understood web components, why they are useful, and how to build one. Furthermore, creating a web component can be achieved using frameworks such as react.js and vue.js. I, therefore, encourage you to keep exploring and experimenting. |