Web Components Revolution

31
Saeid Zebardast Web Components Revolution

Transcript of Web Components Revolution

Page 1: Web Components Revolution

Saeid ZebardastWeb Components Revolution

Page 2: Web Components Revolution

Hi! I’m Saeid,and I love We Components.

zebardast.irsaeidsaeidzebardast

Page 3: Web Components Revolution

I’ll talk about...

• Web Components?• The Specs• Use Them, Today!

Page 4: Web Components Revolution

How Do You Use Libraries?

• Bootstrap• Foundation• jQuery UI

1. Go to docs

2. Copy & Paste• All of them!

3. See result

Page 5: Web Components Revolution

Whyyy!

Page 6: Web Components Revolution

We can do it better

1.Find a tag (element)• bower install --save PolymerElements/paper-menu

2.Import • <link rel="import" href="../bower_components/paper-menu/paper-

menu.html">

3.Use it• <paper-menu selected="0">

<paper-item>Inbox</paper-item> <paper-item disabled>Starred</paper-item> <paper-item>Sent mail</paper-item></paper-menu>

Page 7: Web Components Revolution

Like a Boss

Page 8: Web Components Revolution

<google-map>

<google-map latitude="35.699694" longitude="51.338560"></google-map>

Page 9: Web Components Revolution

<nvd3-pie>

<nvd3-pie data="[[data]]" auto-resize show-legend></nvd3-pie>

Page 10: Web Components Revolution

<moment-js>

<moment-js format="YYYY-MM-DD HH:mm:ss"></moment-js>

2016-07-18 00:52:59

Page 11: Web Components Revolution

Web Components 😄

1.Create your own HTML elements2.Scope your styles3.React with lifecycle callbacks

Page 12: Web Components Revolution

The Specs

• Templates• Shadow DOM• Custom Elements• Imports

Page 13: Web Components Revolution

The Specs

• Templates• Shadow DOM• Custom Elements• Imports

Page 14: Web Components Revolution

Templates

Template are inert chunks of DOM the can be reused.

<template id="my-element"><h1>Awesome Trunk!</h1>

<img src=""></template><script>

var tmpl = document.querySelector(‘#my-element’); tmpl.content.querySelector("img").src = "trunk.gif";

document.body.appendChild(document.importNode(tmpl.content, true));</script>

Content

Page 15: Web Components Revolution

Templates Limitations

• No built-in data interpolation• {{}} don’t do anything...yet!

• Nested templates not activated automatically• Append each template separately

Page 16: Web Components Revolution

The Specs

• Templates• Shadow DOM

• Encapsulation• Custom Elements• Imports

Page 17: Web Components Revolution

• Provides style and markup encapsulation.• The same technology used by browser makers to implement tags:

• <video>• <video src="./be-happy.webm" controls>

Shadow DOM

Page 18: Web Components Revolution

Shadow HostThe node that contains all of our shadow DOM <video>

Shadow RootThe first node in the shadow DOM #document-fragment

Shadow BoundaryThe barrier that protects our shadow DOM

<div> <div>

Page 19: Web Components Revolution

<template><style>

h3 { color: white; background: tomato}</style>

<h3>A Shadow Header</h3></template><script>

var tmpl = document.querySelector('template'); var host = document.querySelector(.my-header');

var root = host.createShadowRoot();root.appendChild(document.importNode(tmpl.content, true));

</script></body>

Style Encapsulation

A Shadow Header

<body><h3>A Default Header</h3> A Default Header

Page 20: Web Components Revolution

The Specs

• Templates• Shadow DOM• Custom Elements

• Extensions!• Imports

Page 21: Web Components Revolution

Templates + Shadow DOM

Custom Elements

Page 22: Web Components Revolution

document.registerElement('tag-name', {prototype: proto

});

Custom Elements

• Tag name must have a dash• Proto must inherit from HTMLElement

Page 23: Web Components Revolution

var tmpl = document.querySelector('#some-template');

var MyElementProto = Object.create(HTMLElement.prototype);

MyElementProto.createdCallback = function() {var root = this.createShadowRoot();root.appendChild(document.importNode(tmpl.content, true);

};

var MyElement = document.registerElement('my-element', {prototype: MyElementProto

});

Create a Custom Element

Page 24: Web Components Revolution

• <my-element></my-element>

// OR

• document.createElement('my-element’);

// OR

• new MyElement();

Using Your Element

Page 25: Web Components Revolution

Life Cycle Callbacks

• createdCallback()When a new instance is created. Use like a constructor

• attachedCallback()When an element is added to the page

• detachedCallback()When an element is removed from the page

• attributeChangedCallback(attrName, oldVal, newVal)When one of an element's attributes changes

Page 26: Web Components Revolution

The Specs

• Templates• Shadow DOM• Custom Elements• Imports

• Packaging• Everything on one page

Page 27: Web Components Revolution

• Imports load external documents into your page.• Use a link tag, just like css.• Use rel type import

• <link rel="import" href="my-import.html">

Imports

Page 28: Web Components Revolution

Browser Support

Native Polyfill (webcomponents.js)

Page 29: Web Components Revolution

When Use It? Whenever You Want!When Not Use It? Whenever You Don’t Want!

Page 30: Web Components Revolution

• webcomponents.org• customelements.io• polymer-project.org• See you at the Polymer workshop!

Use Them, Today!

Page 31: Web Components Revolution

AnyQuestion,Comments?

Thank You 👏

zebardast.irsaeidsaeidzebardast