Web Components

Support today

@garethderioth

Medellin, December 9, 2015

Why we need them?

The web today is more like apps

 

https://mmmelon.com/es

 

Building tabs should be very easy

 

But don't!

 
<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Nunc tincidunt</a></li>
    <li><a href="#tabs-2">Proin dolor</a></li>
    <li><a href="#tabs-3">Aenean lacinia</a></li>
  </ul>
  <div id="tabs-1">
    <p>Proin elit arcu.</p>
  </div>
  <div id="tabs-2">
    <p>Morbi tincidunt.</p>
  </div>
  <div id="tabs-3">
    <p>Mauris eleifend.</p>
  </div>
</div>

<script>
$( "#tabs" ).tabs();
</script>

https://jqueryui.com/tabs/

<div id="tabstrip">
    <ul>
        <li>Tab 1</li>
        <li>Tab 2</li>
    </ul>
    <div>Content 1</div>
    <div>Content 2</div>
</div>

<script>
    $("#tabstrip").kendoTabStrip({
        animation: {
            // fade-out current tab over 1000 milliseconds
            close: {
                duration: 1000,
                effects: "fadeOut"
            },
           // fade-in new tab over 500 milliseconds
           open: {
               duration: 500,
               effects: "fadeIn"
           }
       }
    });
</script>

http://docs.telerik.com/kendo-ui/api/javascript/ui/tabstrip

<div id="demo"></div>
<script>
YUI().use('tabview', function(Y) {
    var tabview = new Y.TabView({
        children: [{
            label: 'foo',
            content: '<p>foo content</p>'
        }, {
            label: 'bar',
            content: '<p>bar content</p>'
        }, {
            label: 'baz',
            content: '<p>baz content</p>'
        }]
    });

    tabview.render('#demo');
});
</script>

http://yuilibrary.com/yui/docs/tabview/

@Component({
  selector: 'tabs',
  template: `
    <ul>
      <li *ng-for="#tab of tabs" (click)="selectTab(tab)">
        {{tab.tabTitle}}
      </li>
    </ul>
    <content></content>
  `,
  directives: [NgFor]
})
export class Tabs {
  constructor() {
    this.tabs = [];
  }

  selectTab(tab) {
    this.tabs.forEach((tab) => {
      tab.active = false;
    });
    tab.active = true;
  }

  addTab(tab: Tab) {
    if (this.tabs.length === 0) {
      tab.active = true;
    }
    this.tabs.push(tab);
  }
}

@Component({
  selector: 'tab',
  inputs: ['tabTitle: tab-title'],
  template: `
    <div [hidden]="!active">
      <content></content>
    </div>
  `
})
export class Tab {
  constructor(tabs:Tabs) {
    tabs.addTab(this);
  }
}

http://blog.thoughtram.io/angular/2015/04/09/developing-a-tabs-component-in-angular-2.html

https://github.com/pedronauck/react-simpletabs

var Tabs = require('react-simpletabs');
var App = React.createClass({
  render: function() {
    return (
      <Tabs>
        <Tabs.Panel title='Tab #1'>
          <h2>Content #1 here</h2>
        </Tabs.Panel>
        <Tabs.Panel title='Tab #2'>
          <h2>Content #2 here</h2>
        </Tabs.Panel>
        <Tabs.Panel title='Tab #3'>
          <h2>Content #3 here</h2>
        </Tabs.Panel>
      </Tabs>
    );
  }
});

React.renderComponent(<App />, mountNode);
<paper-tabs>
  <paper-tab>KNOWLEDGE</paper-tab>
  <paper-tab>HISTORY</paper-tab>
  <paper-tab>FOOD</paper-tab>
</paper-tabs>

https://elements.polymer-project.org/elements/paper-tabs

Just keep it simple!

 

What is a Component?

noun

  1. a part or element of a larger whole, especially a part of a machine or vehicle.

"an assembly plant for imported components"

https://en.wikipedia.org/wiki/Component-based_software_engineering

Component-based software engineering

It is a reuse-based approach to defining, implementing and composing loosely coupled independent components into systems.

What are Web

Components?

Web Components are a set of standards for the creation of reusable widgets or components in web documents and web applications.

Custom Elements

Define new HTML/DOM elements

Custom elements

Define new HTML

  • Declarative, readable
  • Meaningful HTML
  • Common way to extend → reusable
<paper-tabs selected=“1”>
  <paper-tab>Tab 1</paper-tab>
  <paper-tab>Tab 2</paper-tab>
  <paper-tab>Tab 3</paper-tab>
</paper-tabs>
const tabs = document.querySelector('paper-tabs');

tabs.addEventListener('iron-activate', function() {
  console.log(this.selected);
});

Templates

Native client-side templating

HTML Templates

Native client-side templating

  • Use DOM to scaffold DOM → no XSS

  • Parsed, not rendered

  • Content is inert until cloned/used

  • Doc fragment → not part of the page

<template>
  <div class=“comment”>
    <img src=“image.png”>
  </div>
  <script>...</script>
</template>

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template

Shadow DOM

DOM/CSS scoping

<video src=“foo.webm” controls></video>
<video src=“foo.webm” controls></video>

Actually Shadow DOM

HTML Imports

Loading web components

<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>

Bootstrap dependecies

<link href="bootstrap.html" rel="import">

Using HTML imports

Vulcanize

vulcanize target.html > build.html

grunt-vulcanize

gulp-vulcanize

vulcanize-loader

Custom Elements

Create new HTML elements and extend existing ones

Templates

Native templating in the browser

Shadow DOM

Scoped CSS with encapsulated markup

HTML Imports

Load custom element definitions and resources

Browser Support

http://jonrimmer.github.io/are-we-componentized-yet/

Current support

Microsoft Edge

https://blogs.windows.com/msedgedev/2015/07/14/bringing-componentization-to-the-web-an-overview-of-web-components/

https://blogs.windows.com/msedgedev/2015/07/15/microsoft-edge-and-web-components/

"Web components have a lot to offer the community. We’re excited about the potential for the technology to dramatically simplify large-scale web development while simultaneously reinforcing good declarative patterns that are already familiar in HTML."

Mozilla Firefox

https://hacks.mozilla.org/2015/11/an-update-on-web-components-and-firefox/

  • Only <template> is natively available.
  • The first iteration of the new consensus-based Shadow DOM is expected to ship in the first half of 2016. You can read Anne’s and Wilson’s posts for more details.
  • There was an initial implementation of Custom Elements, based on prototypes, which is also the version Blink shipped, but Apple’s Ryosuke Niwa is fleshing out some experiments to come up with new approaches that use the ES6 class syntax instead. There won’t be active work on Custom Elements on Firefox until consensus is reached.
  • HTML Imports are not shipping, as we want to wait to see what developers do with ES6 modules. There was an early unfinished implementation which will be removed.

https://developer.mozilla.org/en-US/docs/Web/Web_Components/Status_in_Firefox

Apple Webkit

https://webkit.org/blog/4096/introducing-shadow-dom-api/

"To complement shadow DOM, we’re also interested in custom elements. The custom elements API allows authors to associate a JavaScript class with a particular element name in HTML documents, and it’s a great way to attach shadow DOM and other custom behaviors idiomatically."

 

"To conclude, we’re really excited about bringing a major feature of Web Components to WebKit, and we’ll keep you posted about more features coming your way."

Templates

https://mozilla.github.io/platatus/#html-templates

https://webkit.org/status/#feature-template-element

https://dev.windows.com/en-us/microsoft-edge/platform/status/templateelement

https://www.chromestatus.com/features/5207287069147136

Shadow DOM

https://mozilla.github.io/platatus/#shadow-dom

https://webkit.org/status/#feature-shadow-dom

https://dev.windows.com/en-us/microsoft-edge/platform/status/shadowdom

https://www.chromestatus.com/features/4507242028072960

Custom Elements

https://mozilla.github.io/platatus/#custom-elements

https://webkit.org/status/#feature-custom-elements

https://dev.windows.com/en-us/microsoft-edge/platform/status/customelements

https://www.chromestatus.com/features/4642138092470272

HTML Imports

https://mozilla.github.io/platatus/#html-imports

https://webkit.org/status/#feature-html-imports

https://dev.windows.com/en-us/microsoft-edge/platform/status/htmlimports

https://www.chromestatus.com/features/5144752345317376

Thanks

Web Components Support today

By Guillermo Rodas

Web Components Support today

Web Components Support today for a series of FE talks

  • 555