38
© 2014 Orchard Harvest Redmond – WA - 2014 Future of Widgets? Web Components, Polymer and Orchard Steve Taylor - Avastec

Orchard Harvest 2014 - The Future of Widgets?

Embed Size (px)

DESCRIPTION

Presented on June 9th 2014 at the Orchard Harvest 2014 event which was held at Microsoft's HQ in Redmond, WA. Many technologies, past and present have tried to "componentize" HTML. Each with their own implementations and approaches. Web Components are a standard implementation backed by the W3C that aim to tackle this. Polymer is a library from Google that sits on top of Web Components and makes the creation of custom elements a lot easier. The presentation, talks on how Web Components & Polymer can be used within Orchard CMS.

Citation preview

Page 1: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Future of Widgets?Web Components, Polymer and Orchard

Steve Taylor - Avastec

Page 2: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Ch 14: Adding Behaviors and HTML Components

Page 3: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

“Element behaviors are one of the most significant new capabilities in Microsoft Internet Explorer 5.5.

They provide the capability to define custom elements, which can be used in the same way as normal HTML elements in a Web page. “

source: MDSN - About Element Behaviors

Page 4: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

HTML Components Example

<public:component tagname="checkers"> <public:property name="boardWidth" /> <public:method name="newGame()" /> <public:attach event="onmouseover" onevent="mouseover()" /></public:component>

<script language="Javascript"> function newGame() { // insert code to initialize a new game here }

function mouseover() { // insert code to handle mouseover events }</script>

HTML

Page 5: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

ASP.NET Server Controls

<asp:button runat="server" id="Button1" />

ASP.NET

<input type="button" id="Button1" />

HTML

Page 6: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

AngularJS Directives<script> var app = angular.module('myapp', []);

app.directive('helloWorld', function () { return { restrict: 'E', replace: 'true', template: '<h3>Hello World!</h3>' }; });</script>

JavaScript

<hello-world /> HTML

<h3>Hello World!</h3> HTML

Page 7: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Web Components

Page 8: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

“Web Components comprises of a set of emerging standards which are aimed at making encapsulated and reusable HTML widgets or components.”

Page 9: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

In a nutshell...

<awesome-button text="Pure Awesome!"></awesome-button>

HTML

Page 10: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Bootstrap Carousel<div id="carousel-example-generic" class="carousel slide" data-ride="carousel"> <!-- Indicators --> <ol class="carousel-indicators"> <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li> <li data-target="#carousel-example-generic" data-slide-to="1"></li> ... </ol>

<div class="carousel-inner"> <div class="item active"> <img src="..." alt="..."> <div class="carousel-caption"> ... </div> </div> ... </div>

<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a> <a class="right carousel-control" href="#carousel-example-generic" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a></div>

HTML

Page 11: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Custom Carousel Element

<my-carousel indicators="true"> <my-slide>...</my-slide> <my-slide>...</my-slide> <my-slide>...</my-slide></my-carousel>

HTML

Page 12: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Web Components consist of:

Custom Elements, which let authors define their own elements, with new tag names and new script interfaces.

HTML Templates, which define chunks of markup that are inert but can be activated for use later.

Shadow DOM, which encapsulates a DOM subtree for more reliable composition of user interface elements.

HTML Imports, which defines how templates and custom elements are packaged and loaded as a resource.

Page 13: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Custom Elements

Page 14: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Creating Custom Elements

var awesomeButton = document.registerElement('awesome-button');// Registered elements become a HTMLElement instead of a HTMLUnknownElement

JavaScript

<element name="awesome-button" constructor="awesomeButton"> ...</element>

HTML

<awesome-button></awesome-button>HTML

Page 15: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Extending Existing Elements

var awesomeButton = document.registerElement('awesome-button', { prototype: Object.create(HTMLButtonElement.prototype)});

JavaScript

<element name="awesome-button" extends="button" constructor="awesomeButton"> ...</element>

HTML

<button is="awesome-button"></button>HTML

Page 16: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Lifecycle Event Callbacks

createdCallback

attachedCallback

detachedCallback

attributeChangedCallback(attrName, oldVal, newVal)

Page 17: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

HTML Templates

Page 18: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Declaring a Template

<template> <button>Pure Awesome!</button></template>

HTML

Parsed but not renderedScripts not executedMedia not loaded or playedHidden from main document

Page 19: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Using a Template

<element name="awesome-button"> <template> <button>Pure Awesome!</button> </template> <script>...</script></element>

HTML

Page 20: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Shadow DOM

Page 21: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Using Shadow DOM

<awesome-button> #document-fragment <button>Pure Awesome!</button></awesome-button>

Composed Element

<awesome-button>...</awesome-button><script> var shadow = document.querySelector('awesome-button').createShadowRoot(); shadow.innerHTML = '<button>Pure Awesome!</button>';</script>

Custom Element

Page 22: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Shadow DOM is All Around Us

Page 23: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Using Shadow DOM with Templates<element name="awesome-button"> <template>

<style> :host { background-color: red; } </style> <button>Pure Awesome!</button> </template> <script> var template = document.querySelector('template'); this.register({ prototype: { createdCallback: function() { this.createShadowRoot().appendChild(template.content.cloneNode(true)); } }}); </script></element>

Custom Element

Page 24: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

HTML Imports

Page 25: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Using HTML Imports

<!DOCTYPE html><html><head> <link rel="import" href="awesome-button.html" /></head><body> <awesome-button text="Pure Awesome!"></awesome-button></body></html>

HTML

Page 26: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Importing Inside Your Element

<link rel="import" href="awesome-tooltip.html" />

<element name="awesome-button"> <template> <button id="my-button">Pure Awesome!</button> <awesome-tooltip target="my-button"> <span>Press the button to see some awesomeness</span> </awesome-tooltip> </template> ...</element>

awesome-button.html

Page 27: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Current Browser Support

Page 28: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Page 29: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Layers of Polymer

Page 30: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Layers of Polymer

NativeThe current browser landscape

Page 31: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Layers of Polymer

NativeThe current browser landscape

PlatformWeb Components polyfills for allModern browsers

Page 32: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Layers of Polymer

NativeThe current browser landscape

PlatformWeb Components polyfills for allModern browsers

PolymerAn opinionated way to work with WebComponents

Page 33: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Layers of Polymer

NativeThe current browser landscape

PlatformWeb Components polyfills for allModern browsers

PolymerAn opinionated way to work with WebComponents

ElementsReusable custom elements (in progress)

Page 34: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Why use Polymer?

Less boilerplate codeSimple element registrationDeclarative databindingDeclarative event mappingPublished attributesChange watchersAutomatic node finding

Page 35: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Polymer Example

<link rel="import" href="polymer.html" />

<polymer-element name="awesome-button"> <template> ... </template></polymer-element>

awesome-button.html

<script src="platform.min.js"></script><link rel="import" href="awesome-button.html" />

<awesome-button></awesome-button>

HTML

Page 36: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

DEMO

Page 37: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Special thanks to…Eric Bidelman, Rob Dodson, Addy Osmani, Matthew McNulty

….and the rest of the Polymer team

Page 38: Orchard Harvest 2014 - The Future of Widgets?

© 2014 Orchard Harvest Redmond – WA - 2014

Thanks!Twitter: @stevetayloruk

LinkedIn: linkedin.com/in/stevetayloruk

Github: github.com/stevetayloruk