Press "P" on any slide to bring up the bullet point notes. Use the "left" and "right" arrows to navigate the deck.
Jakob Heuser

Not Today

  • A W3C spec sing-a-long
  • A soapbox rant on the abuse of -webkit prefixes
  • Dancing. Definitely no dancing

Today

  • What / Why Web Components
  • Code examples in Polymer, Ember, Angular, React
  • Responsible Component Usage
Additional resources available in the speaker notes. Just press "P".

What Are Web Components?

Why Should We Care?

It Started Simple Enough


        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
        <html>
          <head><title>My Great Page</title></head>
          <body>
            <h1>Yeah! Internet!</h1>
          </body>
        </html>
      
The jQuery UI tab widget
The jQuery UI logo
The dojo dialog dijit
The dojo logo
The YUI calendar widget
The YUI logo
The Bootstrap dropdown component
The Bootstrap logo
a screenshot of LinkedIn's markup
Simple
Screenshot of Twitter's Typeahead utility

        <twttr-typeahead source="http://mysite.com/api/">
      
The TodoMVC Logo

        <local-storage id="storage" name="my-namespace"></local-storage>
        <todo-database id="model" storageId="storage"></todo-database>
        <todo-items modelId="model">
          <todo-item>A todo</todo-item>
          <todo-item completed>A completed item</todo-item>
        </todo-items>
      

        <todo-list id="my-todos"></todo-list>
      
A return to tags marks the start of the extendable web

Tags Give Us

  • Semantics
  • Encapsulation
  • Configuration
  • DOM Friendly APIs
web components logo
W3C Logo
web components logo
Custom Elements is a bedrock API. We should be able to build all HTML elements with it.
Dimitri Glazkov
W3C Specification Co-Author

Who Wouldn't Want

  • Semantics
  • Encapsulation
  • Configuration
  • DOM Friendly APIs

CanIUse

ChromeOK--
FirefoxOKupdate to document.register needed
OperaPartialusing Blink, a step behind Chrome
SafariNone--
Internet ExplorerNone--
http://jonrimmer.github.io/are-we-componentized-yet

Global Browser Stats

Browser share statistics showing Safari and IE at a combined 63%
NetMarketShare

Web Components Today

Today's JS frameworks can get us there!

The Ember, Angular, React, and Polymer frameworks

Our Target Markup


        <my-carousel>
          <my-carousel-item>one</my-carousel-item>
          <my-carousel-item>two</my-carousel-item>
          <my-carousel-item>three</my-carousel-item>
        </my-carousel>
      
The ember logo

Ember Carousel Markup


        <script type="text/x-handlebars" data-template-name="index">
          {{#e-carousel}}
            <e-carousel-item>one</e-carousel-item>
            <e-carousel-item>two</e-carousel-item>
            <e-carousel-item>three</e-carousel-item>
          {{/e-carousel}}
        </script>
      

Web Components in Ember

  • Support back to IE6 (though performance is slow < IE 8)
  • A handlebar helper {{#e-carousel}} instantiates the component
  • A template named components/e-carousel marks the stamped out template
  • {{yield}} captures our original nodes
  • The JS convention: {{#e-carousel}} becomes App.ECarouselComponent = Ember.Component.extend...

Ember Carousel Implementation


        <script type="text/x-handlebars" data-template-name="components/e-carousel">
          <content>{{yield}}</content>
          <-- ... -->
        </script>
      

        App.ECarouselComponent = Ember.Component.extend({
          var $items = this.$('content e-carousel-item');
          $items.hide();
          $items.each(function(idx, item) {
            // ...
          });
          // ...
        });
      

Ember Example

The React logo

React Carousel Markup


        <r-carousel>
          <r-carousel-item>one</r-carousel-item>
          <r-carousel-item>two</r-carousel-item>
          <r-carousel-item>three</r-carousel-item>
        </r-carousel>
      

Web Components in React

  • All browsers (including IE6+) when using React Pure (no custom elements)
  • IE 9+ when using Reactive Elements for Polymer/X-Tags support
  • Reactive Elements for DOM implementation
  • Can use 100% React (if you want to use React top to bottom)
  • JSX allows for scoped styles through JS definitions of CSS

React Implementation


        <script type="text/jsx">
        /** @jsx React.DOM */
        var rCarousel = React.createClass({
          getInitialState: function() {
            var items = [],
                results = this.props._content.querySelectorAll('r-carousel-item');
            // ...
            return {index: 0, items: items};
          },
          render: function() {
          }
        });

        document.registerReact('r-carousel', rCarousel);
        </script>
      

React Example

The AngularJS logo

AngularJS Carousel Markup


        <ang-carousel ang-component>
          <ang-carousel-item>one</ang-carousel-item>
          <ang-carousel-item>two</ang-carousel-item>
          <ang-carousel-item>three</ang-carousel-item>
        </ang-carousel>
      

Web Components in AngularJS

  • Use 1.3+ for IE9+ and 1.2 if you need IE8 support
  • Directives with a restrict type of "E" parallel custom elements
  • A custom tag such as <ang-template for="ang-*"> can stamp out your templates
  • A bootloader can call angular.bootstrap on page load
  • The $transclude function can give you access to the original content in your directive

AngularJS Implementation


        <ang-template for="ang-carousel"><-- ... --></ang-template>
      

        angular.module('angCarousel', [])
        .directive('angCarousel', function () {
          var tmpl = document.querySelector('ang-template[for="ang-carousel"]');
          return {
            restrict: 'E', transclude: true,
            controller: function($scope, $transclude) {
              $transclude(function(originalNodes) {
              });
              // ...
            }
          };
        });
      

Angular Example

The Polymer logo

Polymer Carousel Markup


        <p-carousel>
          <p-carousel-item>one</p-carousel-item>
          <p-carousel-item>two</p-carousel-item>
          <p-carousel-item>three</p-carousel-item>
        </p-carousel>
      

Web Components in Polymer

  • Evergreen browsers only (IE 10+)
  • <polymer-element name="*" noscript> defines a custom element
  • All <script> and <style> tags are restricted in scope
  • Heavily leverages polyfilled APIs (Shadow DOM, etc)

Polymer Implementation


        <polymer-element name="p-carousel" noscript>
          <template>
            <content id="content" select="p-carousel-item"></content>
            <!-- ... -->
          </template>
          <script>
            Polymer({
              attached: function() {
                var self = this,
                    contents = this.$.content.getDistributedNodes();
                // ...
              }
            });
          </script>
        </polymer-element>
      

Polymer Example

The Ember, Angular, React, and Polymer frameworks
Components written in Angular will export to Web Components (to be used by Polymer, Ember, or any other framework/library)
Miško Hevery
AngularJS Team
Ember templates should support elements implemented in Polymer.
Yehuda Katz
Ember Team
Yo Dawg, I heard you like Web Components.
So I put a framework in your framework,
so you can script while you script.

For a Better Web

Responsible Web Component Use

An Old Friend

Macromedia Flash logo

Why Flash?

  • Has a component library
  • Allows nesting of components for DRY philosophy
  • Uses ECMAScript (just like JavaScript)
  • Is plagued by bloat
  • Has huge component hierarchies

We have a few new tricks this time

the bower.io logo
the atom.io logo

<the-end>

View these slides online @ felocity.com/owc6