Web components: present and future in web development
Web components are a W3C standard which allow us to break down the development of web applications into small containers that encapsulate markup, JavaScript code and styles CSS, receiving the name of components.
This allows us reuse them in different parts of our applications and also to display our services in other applications by simply using an HTML tag.
Origin of web components
Web components arose out of a proposal from Google to W3C, almost at the same time as the JavaScript MVC frameworkAngularJS, also created by the search engine. This framework introduced the concept of “directive”, in which we could create our own HTML tags to encapsulate a HTML markup with its own area of execution.
AngularJS gave us controllers, routing, filters, services, etc. and the directives were there, although to start with they were not used so much. Over time they started to spread due to third developers who shared their implementations by way of Open Source projects. In this way, in our applications we could import these projects and use them in our developments.
At the same time, W3C was working on the concept of web components that fed heavily from this concept that began in AngularJS.
Elements of a Web Component
We need several elements to create a Web Component:
– Templates
– Custom Elements
– Shadow DOM
– HTMLImports
Templates
Templates are an HTML5 element that allow us to create templates to present the data, and they are native to the browser. In our HTML we would have something like the following:
<template id=”profileTemplate”>
<div class=”profile”>
<img src=”” class=”profile__img”>
<div class=”profile__name”></div>
<div class=”profile__social”></div>
</div>
</template>
This would represent the markup for a user profile in a web application. None of the code included within the <template>will be visible in the browser until it is enabled, even if it is within the code.
To be able to enable it and inject data, we must do so via JavaScript with a similar code to the following one:
var template = document.querySelector(‘#profileTemplate’);
template.querySelector(‘.profile__name’).textContent = Mi Nombre’;
template.querySelector(‘.profile__social’).textContent = ‘Sígueme en Twitter’;
document.body.appendChild(template);
Custom Elements
Custom Elements allow us to define our own HTML element (similar to the AngularJS directives).
According to the standard, the correct way to create a Custom Element is with the JavaScript Object.create function and the HTMLElement.protoype element. Based on the above element, the code would look something like this:
<template id=”profileTemplate”>
<div class=”profile”>
<img src=”” class=”profile__img”>
<div class=”profile__name”></div>
<div class=”profile__social”></div>
</div>
</template>
<script>
var MyElementProto = Object.create(HTMLElement.prototype);
This allows us to use the <user-profile> element in our application.
Shadow DOM
Shadow DOM is the DOM that encapsulates our component. It does not belong to the original DOM of the document or web page, it is within the component that we have created. It has its own area of execution and its own CSS style. Think of it as a DOM added to the page’s primary DOM.
One way of viewing the shadow DOM is in the web inspector on the Chrome browser, by clicking on the recently-created element. This shows the DOM that encapsulates the components:
▾<user-profile>
▾#shadow-root (user-agent)
<div class=”profile”>
<img src=”” class=”profile__img”>
<div class=”profile__name”></div>
<div class=”profile__social”></div>
</div>
</user-profile>
This is because in the source code we would only see <user-profile></user-profile>
HTML Imports
Like in CSS, we can import external files with @import and in JavaScript using require (using Node.js or Browserify) or import {module} from ‘libreria.js’, with HTML Imports we can import HTML files containing the web components.
Assuming that our <user-profile> is in a file called ‘user-profile.html’, the way to import it into our application would be as follows:
<link rel=”import” href=”user-profile.html”>
Compatibility in different browsers
Like any web standard, each browser takes its time to implement it and for it to be 100% usable. One of the best sites to check whether a certain element, CSS property or JavaScript API is available in the browsers is caniuse.com
In it we can see that at the time of writing, Chrome and Opera already support all of the requirements to implement and use web components. Firefox almost supports it, except for leaving HTML Imports stable. Safari does not yet support it natively and Microsoft, in its Edge v13 version, only supports the <templates> for the time being.
But through Polyfills we can use the standard in almost any browser and, combining them with the Polymer library (also developed by Google), we can create Web Components in a straightforward manner, share them and use them in the browsers.
Using a Polymer WebComponent
The first thing we must do is import the webcomponents.js library, which is a Polyfill developed by the same Polymer team, so that the web components are compatible in browsers that do not yet support it natively.
Next, the component that we want to use is imported, for example, the Google Maps component.
And, finally, within the BODY, we can already use that component. The code would be something like this:
Which would show a map like the following one in the browser:
Creating a Web Component with Polymer
To create a web component with Polymer, we must follow the style guide and good practices that the Google team promotes. The first thing we need to do is to create an HTML file with the name of the component that we want to develop.
Within this file we import Polymer to be able to use the <dom-module> tag. The dom-module id and the ‘is’ of the Polymer function must have the same name as the webcomponent name.
Within <dom-module>, we insert the <template> tag for the HTML markup, the <script> for the JavaScript code and <style> for the CSS of the component.
Last year we saw how the development of web applications was becoming “componentized”. With the boost of React and Polymer, this new method of development is becoming more popular.
Now the focus is on small components with their own logic and which gradually form the structure of an application, instead of a complete Model-View-Controller code.
This new method of development has made popular by React, the library created by Facebook. React, unlike Polymer, does not create exportable web components by default, instead it componentizes the actual application. This is the main difference when we talk about Components and Web Components.
A Component is a piece in our application, such as a menu, a header, a navbar, an item, and so on. We break down the logic of our application into these small containers without them being exportable for another application.
On the other hand, it is understood that a Web component exports an API or a third-party service, such as <google-map> or <youtube-video>, or a payment platform like PayPal or Stripe, via a series of HTML tags.
Angular for its part is leaning toward this method of programming. Its new 2.0 version follows this method of creating components and its 1.x versions (specifically 1.4, 1.5 which is in the beta phase, and the future 1.6) are leaning toward this.
The PayPal Engineering team is developing/focusing on directives to encapsulate templating and controllers inside them. Then, in the routing, instead of giving a templateUrl with an HTML and a controller, it gives the directive we want to load for a specific URL.
This fits in with the fact that the directive function in Angular will be replaced by the component method in Angular 1.5, fulfilling the same purpose but indicating that a directive does not have to be extensive HTML content, it can be limited to a smaller component.
It also fits in with the new Angular 2 routing system, which can already be used in Angular 1.x with ngNewRouter, which is component-based.
Conclusion
Everything seems to suggest that the present and future frontend web development will be component-based. This way we break our problems down into smaller pieces, with their own style and functionality, and we put them together to form larger applications.
It is the same logic as followed in the Backend, where monolithic systems that controlled all of the application logic are giving way to microservices with small assigned tasks.
We are without doubt facing a time of constant change in the web and we need to be kept informed and up-to-date to be able to get the most out of our professional developments
Carlos Azaustre is a Telematics Engineer. He specializes in JavaScript fullstack web development. He works with AngularJS and NodeJS on a daily basis. At the moment he is CTO and CoFounder of Chefly, a sharing economy startup that helps you to make first-rate homemade meals.
Banco Bilbao Vizcaya Argentaria, S.A. owner of this portal uses cookies and/or similar technologies of its own and third parties for the purposes of personalization, analytics, behavioral advertising or advertising related to your preferences based on a profile prepared from your browsing habits (e.g. pages visited). If you wish to obtain more detailed information, consult our Cookies Policy.
Cookie settings panel
These are the advanced settings for first-party and third-party cookies. Here you can change the parameters that will affect your browsing experience on this website.
Technical Cookies (required)
These cookies are used to give you secure access to areas with personal information and to identify you when you log in.
Name
Owner
Duration
Description
gobp.lang
BBVA
1 month
Language preference
aceptarCookies
BBVA
1 year
Configuration Accepted Cookies
_abck
BBVA
1 year
Helps protect against malicious website attacks
bm_sz
BBVA
4 hours
Helps protect against malicious website attacks
ADRUM_BTs
Salesforce Marketing Cloud
Session
Required for monitoring of the service, inherent to SFMC
ADRUM_BT1
Salesforce Marketing Cloud
Session
Required for monitoring of the service, inherent to SFMC
ADRUM_BTa
Salesforce Marketing Cloud
Session
Required for monitoring of the service, inherent to SFMC
ADRUM_BT
Salesforce Marketing Cloud
Session
Required for monitoring of the service, inherent to SFMC
xt_0d95e
Salesforce Marketing Cloud
Session
Remember user preferences (if any)
__s9744cdb192d044faa1bf201d29fafd1e
Salesforce Marketing Cloud
Session
Remember user preferences (if any)
wpml_browser_redirect_test
WPML
Session
Text translation in the portal
wp-wpml_current_language
WPML
24 hours
Text translation in the portal
Analytics Cookies
They are used to track the activity or number of visits anonymously. Thanks to them we can constantly improve your browsing experience
Your browsing experience is constantly improving.
With your selection, we cannot offer you a continuously improved browsing experience.
Name
Owner
Duration
Description
AMCV_***
Adobe Analytics
Session
Unique Visitor IDs used in Cloud Marketing solutions
AMCVS_***
Adobe Analytics
2 years
Unique Visitor IDs used in Cloud Marketing solutions
demdex (safari)
Adobe Analytics
180 days
Create and store unique and persistent identifiers
sessionID
Adobe Analytics
Session
Launch's internal cookie used to identify the user
gpv_URL
Adobe Analytics
Session
Adobe Analytics plugin: getPreviousValue Capture the value of a certain variable in the following page view, in this case the prop1
gpv_level1
Adobe Analytics
Session
Cookie used to store the DataLayer levl1 of the previous page.
gpv_pageIntent
Adobe Analytics
Session
Cookie used to store the pageIntent of the previous page.
gpv_pageName
Adobe Analytics
Session
Cookie used to store the pagename of the previous page.
aocs
Adobe Analytics
Session
Cookie that stores the first values collected at the beginning of a process.
TTC
Adobe Analytics
Session
Cookie used to store the time between the App Page Visit event and the App Completed event.
TTCL
Adobe Analytics
Session
Cookie used to store the time between the LogIn event and App Completed.
s_cc
Adobe Analytics
Session
Determine if cookies are active
s_hc
Adobe Analytics
Session
Cookie used by Adobe for analytical purposes
s_ht
Adobe Analytics
Session
Cookie used by Adobe for analytical purposes
s_nr
Adobe Analytics
2 years
Determine the number of user visits
s_ppv
Adobe Analytics
Permanent
Adobe Analytics plugin: getPercentPageViewed Determine what percentage of the page a user views
s_sq
Adobe Analytics
Session
ClickMap/ActivityMap features
s_tp
Adobe Analytics
Session
Cookie used by Adobe for analytical purposes
s_visit
Adobe Analytics
2 years
Cookie used by Adobe to know when a session has been started.
Advertising Cookies
They allow the advertising shown to you to be customized and relevant to you. Thanks to these cookies, you will not see ads that you are not interested in.
The advertising is customized to you and your preferences.
Your choice means you will not see customized ads, only generic ones.
Name
Owner
Duration
Description
OT2
VersaTag
90 days
VersaTag Cookie used to store a user id and the number of user visits.
u2
VersaTag
90 days
VersaTag Cookie where the user ID is stored
TargetingInfo 2
MediaMind
1 year
Cookie that serves to assign a unique random number that generates MediaMind.
Customization Cookies
These cookies are related to general features such as the browser you use.
Your experience and content have been customized.
With your selection, we cannot offer you a continuously improved browsing experience.
Name
Owner
Duration
Description
mbox
Adobe Target
9 days
Cookie used by Adobe Target to test user experience customization.
Looks like you’re browsing from MexicoSpainArgentinaPeruColombiaBelgiumChileUSAFranceHong KongItalyPortugalUnited KingdomTurkeyUruguayVenezuelaAlemania, so let’s show you the custom content for your
location. Change
Select a country
In order to access the private area and corresponding sandbox, select the country of the APIs you want to use.