@LostInBrittany#JSC2015 #BeyondPolymer
Speaker :
Horacio Gonzalez - @LostInBrittany
Beyond Polymer
Mixing Webcomponents
@LostInBrittany#JSC2015 #BeyondPolymer
Horacio Gonzalez
@LostInBrittany
Cityzen Data
http://cityzendata.com
Spaniard lost in Brittany,
developer, dreamer and all-
around geek
@LostInBrittany#JSC2015 #BeyondPolymer
Introduction
Because I love to tell old stories
@LostInBrittany#JSC2015 #BeyondPolymer
Polymer tour
@LostInBrittany#JSC2015 #BeyondPolymer
Web components == Revolution
Image: bu.edu
@LostInBrittany#JSC2015 #BeyondPolymer
Build a world brick by brick
Images: BitRebels & Brickset
@LostInBrittany#JSC2015 #BeyondPolymer
Variations of the same questions
But does it really works with
<inser techno here/>?
And what about the other
web components technologies?
@LostInBrittany#JSC2015 #BeyondPolymer
The questions deserved answers
So I decided to prepare a new talk
@LostInBrittany#JSC2015 #BeyondPolymer
I prepared everything before BreizhCamp
J'étais tranquille, j'étais pénard...
@LostInBrittany#JSC2015 #BeyondPolymer
And the Google made me a gift
The gift of living interesting moments...
@LostInBrittany#JSC2015 #BeyondPolymer
During a week sleep was optional
And tiredness began to take its toll
@LostInBrittany#JSC2015 #BeyondPolymer
But it worked!
Everything was rewritten in time
And people seemed to like it!
@LostInBrittany#JSC2015 #BeyondPolymer
Will it work today?
I had prepared a rather traditional slidedeck
Comparing Polymer with X-Tag and Bosomic
Explaining how to make them work together
@LostInBrittany#JSC2015 #BeyondPolymer
But I decided a new approach
The truth is in the code
I FIND YOUR LACK OF SOURCE CODE
DISTURBING
@LostInBrittany#JSC2015 #BeyondPolymer
Web Components
Reinventing the wheel... and this time
making it round
@LostInBrittany#JSC2015 #BeyondPolymer
Example : the Google+ button
● If you want to place a Google+ button in your page
<!-- Place this tag where you want the +1 button to render. -->
<div class="g-plusone" data-annotation="inline" data-width="300"
></div>
<!-- Place this tag after the last +1 button tag. -->
<script type="text/javascript">
(function() {
var po = document.createElement('script');
po.type = 'text/javascript';
po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(po, s);
})();
</script>
@LostInBrittany#JSC2015 #BeyondPolymer
Example : the Google+ button
● And what I would like is simple
<g:plusone></g:plusone>
@LostInBrittany#JSC2015 #BeyondPolymer
Example : the Google+ button
● To be fair, Google already makes it simpler
<script type="text/javascript" src="https://apis.google.com/js/plusone.js">
</script>...
<g:plusone></g:plusone>
● They create directives with JS to emulate components
○ AngularJS approach
○ Respecting the spirit of the future standard
○ Working in current browsers
Totally non standard...
@LostInBrittany#JSC2015 #BeyondPolymer
Another example : the RIB
● If you're French, you know what a RIB is
○ A banking account identification number
● To show a RIB in HTML:
○ All styling & surface control must be done elsewhere by CSS and JS
● What I would like
○ A semantic tag
○ With encapsulated styling and surface controlling
<div class="rib">58496 87451 00014500269 74</div>
<x-rib banque="58496" guichet="87451" compte="00014500269" cle="74" />
@LostInBrittany#JSC2015 #BeyondPolymer
But we already can do that!
● In most modern frameworks we can already do components, in
a way or another
○ And all those ways are different!
○ Using different JavaScript libraries
○ Almost no component sharing between frameworks
● W3C's works aim to make a standard way
○ Supported natively by all browsers
○ Allowing for component reuse
@LostInBrittany#JSC2015 #BeyondPolymer
W3C standard
● Web Components standard is being worked at W3C
○ We all know what this means
■ Clue : HTML5
They will work for years, bickering and fighting
Browsers and devs will use the WiP document
@LostInBrittany#JSC2015 #BeyondPolymer
The 4 pillars of the Web Components
● Templates
● Shadow DOM
● Custom Elements
● Imports
@LostInBrittany#JSC2015 #BeyondPolymer
Templates
Image : Instructables
The clone wars
@LostInBrittany#JSC2015 #BeyondPolymer
● Reusable blocks in the DOM with a new tag
● Content inside the tag is parsed but not interpreted
○ HTML not shown
○ Resources are not loaded
○ Scripts not executed
● Create the elements in page by cloning the template
The <template> tag
<template id="commentTemplate">
<div>
<img src="">
<div class="comment-text"></div>
</div>
</template>
@LostInBrittany#JSC2015 #BeyondPolymer
Shadow DOM
Image: Springfield Punx
Join the shadowy side,
young padawan
@LostInBrittany#JSC2015 #BeyondPolymer
Encapsulation
● Each component should have
○ Public interface
○ Private inner code
● When using a component
○ You manipulate the interface only
○ You don't need to know anything about inner code
○ No conflict between inner code and outside code
@LostInBrittany#JSC2015 #BeyondPolymer
Your browser cheats on you
● Considerer this simple slider
○ How does the browser deal with it?
■ With HTML, CSS and JS!
○ It has a movable element, I can recover it's position
■ Why cannot see it in DOM tree?
Browsers hide DOM sub-trees for standard components
They have a public interface and hidden inner code
That's Shadow DOM!
<input id="foo" type="range">
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
</video>
Image: Springfield Punx
@LostInBrittany#JSC2015 #BeyondPolymer
My name is DOM, Shadow DOM
● Shadow DOM: ability of the browser to
○ Include a DOM subtree into the rendering
○ But not into the main document DOM tree
● In Chome you can see the Shadow DOM
○ By activating the option in Inspector
@LostInBrittany#JSC2015 #BeyondPolymer
Looking into the Shadow
● For the slider :
@LostInBrittany#JSC2015 #BeyondPolymer
Shadow DOM is already here
● Browser use it everyday...
○ For their inner needs
○ Not exposed to developers
● Web Components makes Shadow DOM available
○ You can use it for your own components
Image: Springfield Punx
@LostInBrittany#JSC2015 #BeyondPolymer
Example
@LostInBrittany#JSC2015 #BeyondPolymer
Custom Elements
Image: The Brick Blogger
Elemental mayhem !
@LostInBrittany#JSC2015 #BeyondPolymer
Custom elements : the HTML side
● An element encloses template, lifecycle and behaviour
○ Templates done with <template> tag
<!-- Template Definition -->
<template id="template">
<style>
...
</style>
<div id="container">
<img src="http://webcomponents.org/img/logo.svg">
<content select="h1"></content>
</div>
</template>
<!-- Custom Element usage -->
<x-component>
<h1>This is Custom Element</h1>
</x-component>
@LostInBrittany#JSC2015 #BeyondPolymer
Custom elements : the JavaScript side
● An element encloses template, lifecycle and behaviour
○ JavaScript to define behaviour and register the element
var proto = Object.create(HTMLElement.prototype);
proto.createdCallback = function() {
// Adding a Shadow DOM
var root = this.createShadowRoot();
// Adding a template
var template = document.querySelector('#template');
var clone = document.importNode(template.content, true);
root.appendChild(clone);
}
var XComponent = document.registerElement('x-component', {
prototype: proto
});
@LostInBrittany#JSC2015 #BeyondPolymer
Imports
Image: Brikipedia
Because you hate long files
@LostInBrittany#JSC2015 #BeyondPolymer
● Custom elements can be loaded from external files
○ Using the link tag:
○ Only <decorator> and <element> are interpreted
○ The DOM of this document is available to script
○ Documents are retrieved according to cross-origin policies
Imports
<link rel="import" href="goodies.html">
@LostInBrittany#JSC2015 #BeyondPolymer
Can I use?
Image: Christoph Hauf
If not, why are you telling us all this sh*t?
@LostInBrittany#JSC2015 #BeyondPolymer
Are we componentized yet?
@LostInBrittany#JSC2015 #BeyondPolymer
WebComponents.org
@LostInBrittany#JSC2015 #BeyondPolymer
Polymer
Webcomponents for today's web
@LostInBrittany#JSC2015 #BeyondPolymer
● A Google project
○ Introduced in Google I/O 2013
○ New type of library for the web
○ Built on top of Web Components
○ Designed to leverage the evolving web platform
Version 1.0 just released last week at Google IO
Oh yeah!
Polymer
@LostInBrittany#JSC2015 #BeyondPolymer
● What does it means ?
○ Polymer is comprised of two efforts :
■ A paper platform to give Web Component
capabilities to modern browsers
● Shims for all modern browsers
● Shared with Mozilla x-tag project
■ A next-generation web framework built upon
this core platform
● Called Polymer Elements
Polymer
@LostInBrittany#JSC2015 #BeyondPolymer
Polymer
@LostInBrittany#JSC2015 #BeyondPolymer
Polymer elements
@LostInBrittany#JSC2015 #BeyondPolymer
● Principes:
○ Everything is a component
■ Encapsulation is needed for a component oriented application
○ Extreme pragmatism
■ Boilerplate is bad
■ Anything repetitive should be re-factored into a component
● Handled by Polymer itself or
● Added into the browser platform itself
Polymer
@LostInBrittany#JSC2015 #BeyondPolymer
● Principes:
○ Salt to taste
■ Use as much or as little of the framework as you wish.
● You want polyfills only : load polymer-
all/platform/platform.js
● You want extra bits : load polymer-
all/polymer/polymer.js
○ Polymer elements
Polymer
@LostInBrittany#JSC2015 #BeyondPolymer
● Platform technologies are already functional
○ You can use it to add templates, shadow DOM, custom elements and imports
to your app
● Lots of examples in the site
Polymer
@LostInBrittany#JSC2015 #BeyondPolymer
WebComponents
beyond Polymer
A whole world
@LostInBrittany#JSC2015 #BeyondPolymer
● X-Tag is a small JavaScript library
○ created and supported by Mozilla
○ that brings Web Components capabilities
○ to all modern browsers
● Polymer vs X-Tag?
○ Different features and approaches
○ Mozilla and Google worked together
■ building the shared polyfills platform
○ More barebones approach
X-Tag?
@LostInBrittany#JSC2015 #BeyondPolymer
● AngularJS directives allow to create custom elements
○ Same spirit, different implementation
AngularJS?
<!doctype html>
<html>
<head>
<title>Directive Test</title>
<script type="text/javascript" src="jquery.min.js" ></script>
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="mydirectives.js"></script>
</head>
<body ng-app>
<div test-elem></div>
</body>
</html>
@LostInBrittany#JSC2015 #BeyondPolymer
Google Web Components
From small widgets to full-scoped apps
@LostInBrittany#JSC2015 #BeyondPolymer
Mozilla Brick
@LostInBrittany#JSC2015 #BeyondPolymer
Bosonic
@LostInBrittany#JSC2015 #BeyondPolymer
Web components polyfill
Making developers life a bit easier
@LostInBrittany#JSC2015 #BeyondPolymer
Web components polyfill library
Set of polyfills built on top of the
Web Components specifications
@LostInBrittany#JSC2015 #BeyondPolymer
What's in webcomponents.js?
● Web Components
○ Custom Elements .
○ HTML Imports
○ Shadow DOM
● DOM
○ WeakMap
○ Mutation Observers
@LostInBrittany#JSC2015 #BeyondPolymer
Your own webcomponents!
Building directly over webcomponents.js
@LostInBrittany#JSC2015 #BeyondPolymer
Or webcomponent libraries
Also built on top of webcomponents.js
@LostInBrittany#JSC2015 #BeyondPolymer
Many webcomponents libraries
Based on the same polyfill
@LostInBrittany#JSC2015 #BeyondPolymer
Mixing webcomponents
My challenge today
@LostInBrittany#JSC2015 #BeyondPolymer
Objectifs
Using webcomponents from different
libraries in an AngularJS webapp
@LostInBrittany#JSC2015 #BeyondPolymer
Will it work?
or
@LostInBrittany#JSC2015 #BeyondPolymer
Base project: Angular Beers!
https://github.com/LostInBrittany/angular-beers
@LostInBrittany#JSC2015 #BeyondPolymer
Let's show the code!
Image: dcplanet.fr
Coding is a superpower, my friends
https://github.com/LostInBrittany/beyond-polymer
@LostInBrittany#JSC2015 #BeyondPolymer
angular-polymer-beers
Because directives are subpar webcomponents
@LostInBrittany#JSC2015 #BeyondPolymer
angular-polymer-xtag-beers
Polymer and X-tags play along nicely
@LostInBrittany#JSC2015 #BeyondPolymer
angular-polymer-react-beers
Thanks to Mathieu Ancelin (@TrevorReznik)
@LostInBrittany#JSC2015 #BeyondPolymer
angular-polymer-xtag-react-beers
And they all are happy...
@LostInBrittany#JSC2015 #BeyondPolymer
Thank you !

Beyond Polymer - JUG Summer Camp - 2015-09-18