ING ❤️ Web Components
Tags: litelement, lithtml, webcomponents, javascript
Published: 2019-03-17 00:20:00

---
title: ING ❤️ Web Components
published: true
description: ING ❤️ Web Components
tags: litelement, lithtml, webcomponents, javascript
---
# ING ❤️ Web Components

> Originally posted on the [ING tech blog](https://medium.com/ing-blog).

Over the last several years, the increasing popularity of component-based architecture for web development massively changed the way developers built user interfaces. This model moved away from the more traditional [MVC](https://www.wikiwand.com/en/Model-view-controller) approach to UI development by chunking up interfaces into (you guessed it) **components**!


### Component-Based Architecture

The way **CBA (Component-Based Architecture)** works is to separate the parts of your interface into their own independent components, putting an emphasis on reusability and single-responsibility. An interface can consist of different types of components: from simple "leaf" components like buttons or inputs, to more complex compound components such as a log-in form, which itself consists of a number of other components. 

![button component](https://cdn-images-1.medium.com/max/1600/1*z2Y7Bar0Q1SR2s0Op5zmMQ.png)
> A simple UI button component, used as: `<ing-button>`

In this way entire features like app views can be defined as their own components, while consuming and reusing simpler components.

![feature debits component](https://cdn-images-1.medium.com/max/1600/1*7r4xPmi2jJ6Pxs3zso_G3A.png)
> A Direct Debits feature component, used as: `<ing-orange-direct-debits>` 

So how exactly do Web Components play into this?
Web Components are a set of unified web standards that allow us to write the kinds of components we've been discussing in a way that should work across browsers and frameworks.

Historically, there were a few attempts of standardizing a component model in the past, like Microsoft's HTML Components (HTC) in 1998 and Mozilla's XML Binding Language (XBL) in 2001. The first public draft, called Web Components v0 for the modern Web Components standards was published in 2012. In 2014, Chrome and Opera added support for the v0 spec. In 2016, the v1 spec was published, reflecting the consensus reached among browser vendors over the features and APIs of web components.

Although the browser standardization process takes some time, once accepted, browser standards tend to ensure longevity.

## ING ❤️ Browser Standards

So let's take a look at the standards that allow us to write Web Components:

- **Custom Elements:**
  
The Custom Elements specification enables us to define and use new types of DOM elements in a document. We're basically telling the browser, "Hi browser 👋, here's a new kind of HTML element, and here's what to do with it!".
<br/>

- **ES Modules:**
  
The v0 web components specification provided [HTML Imports](https://developer.mozilla.org/en-US/docs/Web/Web_Components/HTML_Imports) as the standard way to publish components and import them into our projects. Sadly, that specification failed to gain traction among browser vendors, so it was deprecated. 

Meanwhile, [JavaScript Modules](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/) were standardized and implemented across browsers. Since they provide browser-native to share reusable and self-contained bits of code, it was a natural fit for web components.
<br/>

- **HTML Templates:**
  
The HTML Templates specification lets us write reusable chunks of DOM. A HTML Template is a _content fragment_, and it's contents will not be processed by the parser untill we tell it to. 
<br/>

- **Shadow DOM:**
  
Shadow DOM allows us to encapsulate the markup and styles in our Web Components; anything inside a _shadowRoot_ will not be affected or overwritten by styling from outside, and none of our styling will leak outside our components.


## ING ❤️ Browser Support

With [Edge announcing](https://blogs.windows.com/windowsexperience/2018/12/06/microsoft-edge-making-the-web-better-through-more-open-source-collaboration/) their intent to adopt the Chromium engine, soon all major browsers will natively support Web Components 🎉.

![browser support](https://cdn-images-1.medium.com/max/1600/1*km__Z06XweMFH38XkvVEJQ.png)

Fortunately, Web Components are fully [polyfill-able](https://github.com/webcomponents/webcomponentsjs), which allows us to achieve backward compatibility for older browsers that _don't_ natively support Web Components, such as Internet Explorer. This can, however, make polyfilled browsers performance slower.


## ING ❤️ Practical Demos!

Alright, enough talk of standards and specifications, time to get our hands dirty and look at some code!

Here's an example of how we could write a very simple Web Component:

![code example](https://i.imgur.com/htBJ7c1.png)

We can now simply use our component like so:

![demo](https://cdn-images-1.medium.com/max/1600/1*Qtil0digniyZQdiC1TJ8yw.png)

And it'll show up in the DOM as:

![ing loves web components](https://cdn-images-1.medium.com/max/1600/1*j0HaRy0CRyqwbzvi2q9ktw.png)

A common [misconception](https://dev.to/bennypowers/lets-build-web-components-part-8-mythbusters-edition-3la) about Web Components is that they can only take string attributes to pass down data. While it is true that attributes can only be strings as a limitation of the HTML specification, fortunately, we can simply implement getters and setters if we want to pass down some rich data like objects and arrays like so:

![props](https://cdn-images-1.medium.com/max/1600/1*jiKAPYBovfp9V3Eo1d7t8Q.png)

And we can then set the property using javascript:

![set props](https://cdn-images-1.medium.com/max/1600/1*FizXPYfLTVkCIZY8RPF3QA.png)

Excellent, we now have a fully functioning Web Component! And what's great about this is that this code will run in any* modern browser, you can copy and paste this code into your browser's console, add the `<ing-demo>` tag to your HTML, and it'll work! More so, we can now consistently use the same component across all of our applications.

> \* Edge once they've switched to chromium, expected early 2019

What's less great about this is, is that writing code like this will very quickly become tedious, hard to read, and hard to maintain. Imagine if we have a component that has 10 possible properties. We'd have to write 10 getters, 10 setters, and what if we want to _reflect_ our properties to attributes as well? That's going to be a bunch of work, and a whole lot of boiler plate code. Web Components are low level _by design_, and intended as a collection of standards that do very specific things that the platform didn't allow yet. So that means we might need some type of abstraction that makes all this easier for us, and this is exactly why:
  

## ING ❤️ The Polymer Project

Not only has Google been one of the main driving forces for Web Components, they also provide excellent libraries that help us create them. For a long time, the Polymer library was _the_ synonym for Web Components, and played a major part in the adaptation of Web Component usage. 

Now that the standards have matured, it has become much easier to develop Web Components, and many libraries and frameworks now support both the creation and consumption of Web Components, such as:
- [Vue](https://vuejsdevelopers.com/2018/05/21/vue-js-web-component/)
- [Angular](https://angular.io/guide/elements)
- [Svelte](https://svelte.technology/)
- [Skate](https://github.com/skatejs/skatejs)
- [Stencil](https://stenciljs.com/)

If you're interested in using Web Components in your favourite framework, you can visit [custom-elements-everywhere](https://custom-elements-everywhere.com/). Most frameworks* support the consumption of Web Components without any hiccups.

> \* Sadly, React requires a few extra steps to make Web Components work, since it cannot listen for DOM events coming from Custom Elements without the use of a workaround, and will pass data exclusively in the form of attributes, and not properties.

At ING, we ❤️ to use the [Polymer Classic](https://polymer-library.polymer-project.org/) library and the new [LitElement](https://lit-element.polymer-project.org/) and [lit-html](https://lit-html.polymer-project.org/) libraries for developing our Web Components. Let's take a look at how we would rewrite our `<ing-loves>` component using LitElement:

![lit](https://cdn-images-1.medium.com/max/1600/1*dlGPLmojovwifG7IqI3Cvw.png)

LitElement's static properties getter makes it incredibly easy to manage both our properties _and_ attributes, and makes it easy to _declaratively_ render our HTML. More so, we just went down from 35 lines of code in our original example, to a measly 14 lines of code.

## ING 💔 Bugs

Although Web Components have been around for a little while, many JavaScript libraries or browser extensions don't support websites built with Web Components. Most of the time, the reason for this is that many extensions or libraries are not built with Shadow DOM in mind. If you need a refresher; Shadow DOM allows us to encapsulate our components. In some sense this is great, because it means that Shadow DOM is doing it's job correctly, but it's also a disaster because we love to use our favourite extensions! Fortunately, this is mostly a matter of time; as Web Components gain more and more traction, more and more libraries and extensions will start supporting Web Components.

Some time ago, we experienced a similar type of problem where the [Custom Elements polyfill didn't play nicely](https://github.com/webcomponents/custom-elements/issues/137) with the LastPass browser extension. This lead to users being unable to use the LastPass extension to log in to our banking application.

## ING ❤️ The Future

Web Components have \*almost\* been adopted in every major browser, but that certainly doesn't mean the end of it. There are many more exciting specifications and features to be added that will only increase the benefits of using Web Components. Here's a short list of what to look forward to in the future:

- **Scoped Custom Element Definitions** - [[explainer]](https://gist.github.com/justinfagnani/d67d5a5175ec220e1f3768ec67a056bc)
  
A valid criticism against Web Components is that Custom Elements are defined in a global registry, which could cause name collisions and all kinds of trouble. Scoped Custom Element Definitions allow the ability to construct CustomElementRegistry's, and use a given Shadow Root as a scope for those definitions, making it easy to avoid name-clashes.
<br/>

- **CSS Shadow Parts** - [[explainer]](https://www.w3.org/TR/css-shadow-parts-1/)
  
Shadow DOM is great for encapsulating our styles and markup, and protect it from being overwritten by any outside styling. But... what if we actually _want_ to overwrite some styles? The CSS Shadow Parts specification allows us to style purposely exposed elements in a shadow root from outside of the Web Component.
<br/>

- **Accessibility Object Model** - [[explainer]](https://github.com/WICG/aom/blob/gh-pages/explainer.md#custom-elements-apis)
  
The Accessibility Object Model aims to improve browser APIs for assistive technology. Currently, Web Components are forced to use ARIA to declare their default semantics. This causes ARIA attributes which are really implementation details to "leak" into the DOM.<sup>[1](https://github.com/WICG/aom/blob/gh-pages/explainer.md#motivating-use-cases)</sup> The Accessibility Object Model specification makes it easier to specify accessibility attributes and properties for our Web Components, and ultimately allow our Web Components to be more accessible for our users..

As a bank, it's not only important that our applications work in any browser, it's also important that our applications are _accessible_ for all of our users. If you're interested in accessibility, stay tuned for a blogpost on accessibility by our own accessibility guru himself: [@erikkroes](https://twitter.com/erikkroes)!
<br/>

And many, many more great additions are being added to a browser near you! Some honorable mentions: [Template Instantiation](https://github.com/w3c/webcomponents/blob/gh-pages/proposals/Template-Instantiation.md), [HTML and CSS Modules](https://github.com/w3c/webcomponents/issues/645), and [Constructable Style Sheets](https://wicg.github.io/construct-stylesheets/).

## That's all, folks!

👊
🎤

That'll be all from me for this blog post, I'd like to thank you for reading, and if you have any questions about Web Components, or if you have any feedback, criticism or questions, feel free to reach out on [Twitter](https://twitter.com/passle_). I'd also like to thank [Benny Powers](https://twitter.com/PowersBenny) from [Forter](https://www.forter.com/) for taking the time to review and his helpful comments and feedback ✌. 

## Additional Resources

If you're interested in a more technical and hands on blogpost on Web Components, you can check out:
- [Web Components: from Zero to Hero](https://dev.to/thepassle/web-components-from-zero-to-hero-4n4m) by yours truly
- [Let's Build Web Components!](https://dev.to/bennypowers/lets-build-web-components-part-1-the-standards-3e85) by [Benny Powers](https://twitter.com/PowersBenny)