Web Components are going to change everything you think you know about building for the web. For the first time, the web will have low level APIs allowing us to not only create our own HTML tags but also encapsulate logic and CSS. No more global stylesheet soup or boilerplate code! It’s a brave new world where everything is an element.
In my talk from DotJS, I walk through what Web Components have to offer and how to build them using modern tooling. I’ll show you Yeoman, a workflow of tools to streamline creating web-apps using Polymer, a library of polyfills and sugar for developing apps using Web Components in modern browsers today.
Create custom elements & install elements created by others
In this talk you will learn:
- About the four different specs composing Web Components: Custom Elements, Templates, Shadow DOM and HTML imports.
- How to define your own custom elements and install elements created by others using Bower
- Use modern front-end tooling (Yeoman) to scaffold an application using Polymer with generator-polymer
- How Polymer super changes creating web components.
For example, to install Polymer's Web Component polyfills and the library itself, you can run this one liner:
bower install --save Polymer/platform Polymer/polymer
This adds a
bower_components folder and adds the above packages.
--save adds them to your app's bower.json file.
Later, if you wanted to install Polymer's accordion element you could run:
bower install --save Polymer/polymer-ui-accordion
and then import it into your application:
<link rel="import" href="bower_components/polymer-ui-accordion/polymer-ui-accordion.html">
To save time, scaffolding out a new Polymer app with all the dependencies you need, boilerplate code and tooling for optimizing your app can be done with Yeoman with this other one liner:
I also recorded a 30 minute bonus walkthrough of the Polymer Jukebox app I show in the talk.
Covered in the bonus video:
- What the “everything is an element” mantra means
- How to use Bower to install Polymer’s Platform polyfills and elements
- Scaffolding our Jukebox app with the Yeoman generator and sub-generators
- Understanding the platform features scaffolded out via boilerplate
- How I functionally ported over an Angular app over to Polymer.
We also make use of Yeoman sub-generators for scaffolding our new Polymer elements. e.g to create the boilerplate for an element
foo we run:
yo polymer:element foo
which will prompt us for whether we would like the element automatically imported, whether a constructor is required and for a few other preferences.
The latest sources for the app shown in both talks are now up on GitHub. I’ve refactored it a little further to be more organized and a little more easy to read.
Preview of the app:
A few other articles that are worth checking out on the subject: