Real World Web components

59
Web Components Real World Jarrod Overson jarrodoverson.com

description

Slides for talk given at the jQuery Conference San Diego in 2014

Transcript of Real World Web components

Page 1: Real World Web components

Web ComponentsReal World

Jarrod Overson jarrodoverson.com

Page 2: Real World Web components

Web Componentsa.k.a.

outside of Google

Page 3: Real World Web components

Web Components?What are

Page 4: Real World Web components

Templates Custom Elements Shadow DOM HTML Imports (and more…)

Web Components

Page 5: Real World Web components

HTML Templates

<script type="text/x-template"> <h1>Hello World!</h1> </script>

<div style="display:none"> <p>I am cloned over and over!</p> </div>

( traditionally )

Page 6: Real World Web components

HTML Templates

<template> <p>I don’t exist yet!</p> </template>

now(ish)!

Page 7: Real World Web components

Benefits

1. Inert 2. No .innerHTML 3. It makes sense

Page 8: Real World Web components

Custom Elements

❯ MyEl = document.registerElement('my-el'); function my-el() { [native code] } !❯ var el = new MyEl(); undefined !❯ el.outerHTML; "<my-el></my-el>"

Page 9: Real World Web components

OH NO!Not XML!

Page 10: Real World Web components

Benefits

1. Legit new elements 2. Can inherit from the old 3. Make your own semantics

Page 11: Real World Web components

Shadow DOM❯ outer = document.createElement('div'); <div></div> !❯ inner = document.createElement('p') <p></p> !❯ inner.innerText = 'Hello jqcon!'; "Hello jqcon!" !❯ outer.createShadowRoot().appendChild(inner); <p>Hello jqcon!</p> !❯ document.body.appendChild(outer); <div></div>

Page 12: Real World Web components

Shadow DOM

Page 13: Real World Web components

Shadow DOM

Page 14: Real World Web components

Shadow DOM

Page 15: Real World Web components

Shadow DOM

Page 16: Real World Web components

Benefits

1. Proper encapsulation 2. Style boundaries 3. You can use IDs again!

❯ document.querySelector(‘#sameId’); !❯ el.shadowRoot.querySelector(‘#sameId’);

Page 17: Real World Web components

HTML Imports

<link rel=“import" href="external.html">

Page 18: Real World Web components

Benefits

1. It gets stuff out of your stuff

Page 19: Real World Web components

Support for each

<template>

registerElement

Shadow DOM

HTML Imports

Page 20: Real World Web components

Wat

Page 21: Real World Web components

Why am I even here?

Page 22: Real World Web components

Web Components?Why even bother with

Page 23: Real World Web components

Web Components

are part of a fundamental shift in how

the web evolves

Page 24: Real World Web components

The web learns best from itself

getElementsBySelector CoffeeScript Everything CSS ever From what you do

Page 25: Real World Web components

Spec writers make things possible

The community’s job is to make them easy

Page 26: Real World Web components

oi jQuery, tip o’ the hat

Page 27: Real World Web components

Web Components?Ok, so how do I use

Page 28: Real World Web components
Page 29: Real World Web components

Polyfills for emerging features

and a framework for building upon them

Page 30: Real World Web components

<polymer-element name="hello-world"> <script> Polymer('hello-world', {}); </script> </polymer-element>

Page 31: Real World Web components

What happened to <element>?

proto = Object.create(HTMLElement.prototype); HelloWorld = document.registerElement( ‘hello-world', { prototype : proto });

vs Native

Page 32: Real World Web components

<polymer-element name="hello-world"> <template> <div>Hello World</div> </template> <script> Polymer('hello-world', {}); </script> </polymer-element>

Page 33: Real World Web components

<template id="hw-template"> <div>Hello World</div> </template> !<script> var proto = Object.create(HTMLElement.prototype); proto.createdCallback = function() { var template = document.getElementById('hw-template'); var clone = document.importNode(template.content, true); this.createShadowRoot().appendChild(clone); }; HelloWorld = document.registerElement( 'hello-world', { prototype : proto } ); </script>

vs Native

Page 34: Real World Web components

Yay!

Page 35: Real World Web components

- polymer-project.org

Page 36: Real World Web components

Boo!

Page 37: Real World Web components

So what’s the reality?

Page 38: Real World Web components

Parent with crappy, leaky

styles

Sexy scoped web component

chrome 34 with flags turned on

Page 39: Real World Web components

IE 10 with defaults

Page 40: Real World Web components
Page 41: Real World Web components

firefox 26.0 with defaults

Page 42: Real World Web components

chrome 32 with defaults

Page 43: Real World Web components
Page 44: Real World Web components

The reality?

Page 45: Real World Web components

It’s largely thereAfter all,

the component did render.

and you do get useful lifecycle methods

and Polymer does give you sexy data binding

Page 46: Real World Web components

Shadow DOM is not very polyfillable.

Page 47: Real World Web components

Why would I use Web Components

when I have

directives?

Page 48: Real World Web components

How do

work together?

and

Demo!

Page 49: Real World Web components

Web Components are useful

and important.

Page 50: Real World Web components

The bleeding edge is accessible

but you need excuses to use it

Page 51: Real World Web components

1. Prototypes

Page 52: Real World Web components

Make your prototypes with the bleeding edge

Attach a cost to backwards compatibility.

Page 53: Real World Web components

2. Internal Tools

Page 54: Real World Web components

Internally, browser compatibility

doesn’t need to matter.

IE 8 doesn’t exist when you will it away.

Page 55: Real World Web components

3. Native Apps

Page 56: Real World Web components

Do you really need a web server?

Page 57: Real World Web components

Or do you just love web tech?

Page 58: Real World Web components

1. node-webkit 2. brackets-shell 3. CEF 4. Packaged Apps

Demo!

Page 59: Real World Web components

Jarrod Overson

jsoverson.com/githubjsoverson.com/linkedin

jsoverson.comjsoverson.com/google+

[email protected]

jsoverson.com/twitter

@jsoverson