Top Banner
WEB COMPONENTS json Jason Park
28
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Brownbag on basics of web components

WEB COMPONENTSjson Jason Park

Page 2: Brownbag on basics of web components

WHAT ARE WEB COMPONENTS?

Page 3: Brownbag on basics of web components

WEB COMPONENTS

• Custom Elements

• Templates

• Shadow DOM

• HTML Imports

Page 4: Brownbag on basics of web components

WEB SEVERELY LACKS EXPRESSIONSEver seen how Gmail builds a ‘tab’?

Page 5: Brownbag on basics of web components

SEXY MARKUP!Custom element’s here to rescue!

Page 6: Brownbag on basics of web components

CUSTOM ELEMENTREGISTERING NEW ELEMENTS

var XFoo = document.registerElement('x-foo');

document.body.appendChild(new XFoo());

Page 7: Brownbag on basics of web components

CUSTOM ELEMENTEXTENDING ELEMENTS

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

var XFooExtended = document.registerElement('x-foo-extended', {

prototype: XFooProto,

extends: 'x-foo'

});

Page 8: Brownbag on basics of web components

CUSTOM ELEMENTINSTANTIATE ELEMENTS

Declare them:

<x-foo></x-foo>

Create DOM in JS:

var xFoo = document.createElement(‘x-foo’); (= var xFoo = new XFoo())

xFoo.addEventListener('click', function(e) {

alert('Thanks!');

});

document.body.appendChild(xFoo);

Page 9: Brownbag on basics of web components

TEMPLATES BEFORE HTML5#1: Offscreen DOM

✓ Using DOM

✓ Nothing gets rendered

‣ Network request is still made for the image

‣ Difficult to scope CSS rules for the template

Page 10: Brownbag on basics of web components

TEMPLATES BEFORE HTML5#2: Overloading script

✓ Nothing is rendered

✓ Browser doesn’t parse the content as it doesn’t recognise the mime type

‣ Security issues. Runtime string parsing of user supplied data can easily lead to XSS vulnerabilities.

Page 11: Brownbag on basics of web components

TEMPLATES

✓ Content is effectively inert until activated

✓ Throw in any content (audio, script, style, etc) inside template. There won’t be a side effect until the template is used

✓ Content is considered not to be in the document

Page 13: Brownbag on basics of web components

SHADOW DOM• HTML5’s answer to DOM tree encapsulation

• Form internal structure of web component

Page 14: Brownbag on basics of web components

SHADOW DOM

• Page will display “こんにちは、影の世界!” instead of Hello, world!.

Page 15: Brownbag on basics of web components

SHADOW DOM• Separate content from presentation inc. CSS

Page 16: Brownbag on basics of web components

Delivering web component

Page 17: Brownbag on basics of web components

HTML IMPORT

• Distribute HTML/CSS/JS in a single bundle

• Manage dependencies - resources are automatically de-duped by leveraging browser caching

• Choose what you need on a page.

Page 18: Brownbag on basics of web components

HTML IMPORT

can be replaced by

bootstrap.html

Page 19: Brownbag on basics of web components

PUBLISH WEB COMPONENTS

• Web components are not website specific.

• HTML, CSS and script are all packaged in a single file - loaded via HTML import

• http://customelements.io

Page 20: Brownbag on basics of web components

HTML5.. Isn’t that just a fairytale?

Page 21: Brownbag on basics of web components

YES…

Page 22: Brownbag on basics of web components

& NO - WITH POLYFILLonly on “evergreen” browsers though

Page 23: Brownbag on basics of web components

POLYMERWeb component polyfill from Google

Page 24: Brownbag on basics of web components

X-TAGWeb component polyfill from Mozilla

<X>

Page 25: Brownbag on basics of web components

Is this applicable in real world?

Page 26: Brownbag on basics of web components

WEB

• Only on evergreen browsers.

• Most of them are experimental.

• http://builtwithpolymer.org/

Page 27: Brownbag on basics of web components

MOBILE/DESKTOP

• Crosswalk project - run HTML5 apps on iOS & Android

• node-webkit - app runtime based on Chronium & node.js. Distribute to Mac, Linux and Windows.

Page 28: Brownbag on basics of web components

THANKS!:)