Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Polymer Summit 2017 Rob Dodson @rob_dodson Custom Elements Everywhere

Slide 3

Slide 3 text

I've been at Google for 3+ years

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Custom Elements should work everywhere.

Slide 7

Slide 7 text

Custom Elements should work everywhere. So… why don't they?

Slide 8

Slide 8 text

Did we put the before the unihorse

Slide 9

Slide 9 text

TWO PARTS PART no. 1 What is a "good" Custom Element? PART no. 2 How should frameworks act?

Slide 10

Slide 10 text

What is a "good" Custom Element?

Slide 11

Slide 11 text

Do you know if there are any reference Custom Elements I could look at which live up to this standard? Not that I'm aware of.

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

SCIENCE!

Slide 14

Slide 14 text

HowTo: Components A collection of educational custom elements that demonstrate best practices. Examples are meant to be read, interpreted, and ported to your own elements.

Slide 15

Slide 15 text

super(); this.attachShadow({mode: 'open'}); this.shadowRoot.appendChild(template.conte } connectedCallback() { if (!this.hasAttribute('role')) this.setAttribute('role', 'checkbox'); if (!this.hasAttribute('tabindex')) this.setAttribute('tabindex', 0); this._upgradeProperty('checked'); this._upgradeProperty('disabled'); this.addEventListener('keydown', this._onK The construtor is a good place to create Shadow DOM, though you should avoid touching any attributes or Light DOM children as they may not be available yet. connectedCallback fires when the element is inserted into the DOM. It's a good place to set the initial role, tabindex, internal state, and install event listeners. A user may set a property on an instance of an element, before its prototype has been connected to this class. The `_upgradeProperty` method

Slide 16

Slide 16 text

HowTo: Components are a work in progress.

Slide 17

Slide 17 text

Check it out @ bit.ly/howto-components

Slide 18

Slide 18 text

Shadow DOM Attributes & Properties Events

Slide 19

Slide 19 text

Avoid absolutism

Slide 20

Slide 20 text

Shadow DOM

Slide 21

Slide 21 text

Does your element need Shadow DOM?

Slide 22

Slide 22 text

You gotta use Shadow DOM! You gotta use Shadow DOM!

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

howto-checkbox.js + howto-checkbox.css

Slide 25

Slide 25 text

If someone puts your element inside another shadow root, you'll have to figure out how to get your styles in there.

Slide 26

Slide 26 text

w3c/webcomponents #468

Slide 27

Slide 27 text

Create a shadow root if you want to self-apply styles.

Slide 28

Slide 28 text

We have CSS-in-JS for scoping styles, so why do we need Shadow DOM?

Slide 29

Slide 29 text

const template = document.createElement('template'); template.innerHTML = `
Count:
`; Polymer Summit 2017

Slide 30

Slide 30 text

export class CountWithShadow extends HTMLElement { constructor () { super(); this.attachShadow({ mode: 'open' }); this.shadowRoot.appendChild(template.content.cloneNode(true)); } } customElements.define('x-count-with-shadow', CountWithShadow); Polymer Summit 2017

Slide 31

Slide 31 text

export class CountWithoutShadow extends HTMLElement { constructor () { super(); } connectedCallback() { this.appendChild(template.content.cloneNode(true)); } } customElements.define('x-count-without-shadow', Count); Polymer Summit 2017

Slide 32

Slide 32 text

// Meanwhile, in a React component… render () { const { count } = this.state; // increments every second return (
{count} {count}
); } Polymer Summit 2017

Slide 33

Slide 33 text

Count: 1 1 Count:

Slide 34

Slide 34 text

Count: 1 1 Count: 1

Slide 35

Slide 35 text

Count: 1 1 Count: 1
Count:

Slide 36

Slide 36 text

Count: 1 1 Count: 1
Count:
ummm…

Slide 37

Slide 37 text

2 Count: 2 2

Slide 38

Slide 38 text

Place any children you create into a shadow root.

Slide 39

Slide 39 text

These children are your implementation and should be hidden.

Slide 40

Slide 40 text

Does your element need Shadow DOM?

Slide 41

Slide 41 text

Yeah, it probably does.

Slide 42

Slide 42 text

Without Shadow DOM you lose all guarantees of safety.

Slide 43

Slide 43 text

Create a shadow root if you want to self-apply styles. Place any children you create into a shadow root. Shadow DOM

Slide 44

Slide 44 text

Attributes & Properties

Slide 45

Slide 45 text

Hey! HTML has a spec!

Slide 46

Slide 46 text

But HTML is also kinda gnarly

Slide 47

Slide 47 text

Accept primitive data as either attributes or properties; ideally, both.

Slide 48

Slide 48 text

Reflect primitive data from attribute to property, and vice versa.

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

class CustomVideo extends HTMLElement { get preload() { const value = this.getAttribute('preload'); return value === null ? 'auto' : value; } set preload(value) { this.setAttribute('preload', value); } } Polymer Summit 2017

Slide 51

Slide 51 text

class CustomVideo extends HTMLElement { get preload() { const value = this.getAttribute('preload'); return value === null ? 'auto' : value; } set preload(value) { this.setAttribute('preload', value); } } Polymer Summit 2017 default value

Slide 52

Slide 52 text

class CustomVideo extends HTMLElement { get preload() { const value = this.getAttribute('preload'); return value === null ? 'auto' : value; } set preload(value) { this.setAttribute('preload', value); } } Polymer Summit 2017

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Only accept rich data as properties.

Slide 55

Slide 55 text

Do NOT reflect rich data properties to attributes.

Slide 56

Slide 56 text

It's expensive to reflect.

Slide 57

Slide 57 text

A serialized object will lose its identity.

Slide 58

Slide 58 text

Accept primitive data as either attributes or properties. Reflect primitive data from attribute to property, and vice versa. Only accept rich data as properties. Do NOT reflect rich data from property to attribute. Attributes & Properties

Slide 59

Slide 59 text

Events

Slide 60

Slide 60 text

It's superfluous and may cause infinite loops.

Slide 61

Slide 61 text

Do NOT dispatch events in response to the host setting a property (downward data flow).

Slide 62

Slide 62 text

Do dispatch events in response to internal element activity.

Slide 63

Slide 63 text

The element knows something changed, but the host does not.

Slide 64

Slide 64 text

Do NOT dispatch events in response to the host setting a property (downward data flow). Do dispatch events in response to internal element activity. Events

Slide 65

Slide 65 text

What is a "good" Custom Element?

Slide 66

Slide 66 text

Resize This Window Building Components. A guide to Custom Element best practices.

Slide 67

Slide 67 text

Check it out @ bit.ly/building-components

Slide 68

Slide 68 text

How should frameworks act?

Slide 69

Slide 69 text

Billions and billions…

Slide 70

Slide 70 text

Custom Elements Everywhere Making sure frameworks and custom elements can be BFFs

Slide 71

Slide 71 text

custom-elements-everywhere.com

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Rob waves his arms around and apologizes [ ]

Slide 74

Slide 74 text

known unknowns

Slide 75

Slide 75 text

Custom Element test scores

Slide 76

Slide 76 text

Attributes & Properties Events

Slide 77

Slide 77 text

Attributes & Properties

Slide 78

Slide 78 text

Manual The developer tells the framework how to pass data. Automated The framework determines how to pass data.

Slide 79

Slide 79 text

Angular

Slide 80

Slide 80 text

Angular set the foo property equal to bar

Slide 81

Slide 81 text

Angular i.e. myElement.foo = bar

Slide 82

Slide 82 text

Angular set the baz attribute equal to squid

Slide 83

Slide 83 text

Angular i.e. myElement.setAttribute('baz', squid)

Slide 84

Slide 84 text

Vue set the foo attribute equal to bar

Slide 85

Slide 85 text

Vue set the foo property equal to bar

Slide 86

Slide 86 text

React set the foo attribute equal to bar

Slide 87

Slide 87 text

React

Slide 88

Slide 88 text

React You can work around this by imperatively grabbing a reference to the element and setting the property on it.

Slide 89

Slide 89 text

React Resize This Window facebook/react #10399

Slide 90

Slide 90 text

Preact

Slide 91

Slide 91 text

Preact set the foo property if it's available

Slide 92

Slide 92 text

Preact if ('foo' in myElement) myElement.foo = bar else myElement.setAttribute('foo', bar)

Slide 93

Slide 93 text

Preact if ('foo' in myElement) myElement.foo = bar else myElement.setAttribute('foo', bar)

Slide 94

Slide 94 text

Preact if ('foo' in myElement) myElement.foo = bar else myElement.setAttribute('foo', bar)

Slide 95

Slide 95 text

Preact if ('foo' in myElement) myElement.foo = bar else myElement.setAttribute('foo', bar)

Slide 96

Slide 96 text

Events

Slide 97

Slide 97 text

Angular

Slide 98

Slide 98 text

Angular myElement.addEventListener('foochanged', onFoo)

Slide 99

Slide 99 text

Angular lowercase

Slide 100

Slide 100 text

Angular CAPSCase

Slide 101

Slide 101 text

Angular camelCase

Slide 102

Slide 102 text

Angular kebab-case

Slide 103

Slide 103 text

Angular PascalCase

Slide 104

Slide 104 text

Angular AsShOlEcAsE

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

Vue

Slide 107

Slide 107 text

Vue same behavior as Angular

Slide 108

Slide 108 text

React

Slide 109

Slide 109 text

React unfortunately won't work.

Slide 110

Slide 110 text

React You can work around this by imperatively grabbing a reference to the element and adding an event listener.

Slide 111

Slide 111 text

React Resize This Window facebook/react #7901

Slide 112

Slide 112 text

Preact

Slide 113

Slide 113 text

Preact TOTES WORKS!

Slide 114

Slide 114 text

Preact TOTES WORKS! kinda. sorta. mostly…

Slide 115

Slide 115 text

Preact always calls .toLowerCase() myElement.addEventListener('foochanged', onFoo)

Slide 116

Slide 116 text

So…

Slide 117

Slide 117 text

So… it doesn't support AsShOlEcAsE?!?!

Slide 118

Slide 118 text

Preact Resize This Window developit/preact #788

Slide 119

Slide 119 text

So, what did we learn?

Slide 120

Slide 120 text

There are best practices we can all follow

Slide 121

Slide 121 text

What's broken today seems easy to fix

Slide 122

Slide 122 text

amazing people Reviewers HowTo: Components team Sean Rob Jason Evan Dan Domenic Steve Dominic Surma Monica Ewa

Slide 123

Slide 123 text

Thank You! Polymer Summit 2017 Rob Dodson @rob_dodson Images by Simon Child, Eucalyp, Edward Boatman from the Noun Project.