Slide 1

Slide 1 text

@cferdinandi GoMakeThings.com Welcome to the light side WEB COMPONENTS HTML

Slide 2

Slide 2 text

The Uncanny Valley of Web Components

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

The Grain of the Web

Slide 6

Slide 6 text

Chris Ferdinandi GoMakeThings.com

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

The 1st Web Component

Slide 9

Slide 9 text

Web Components https://wallpaperaccess.com

Slide 10

Slide 10 text

https://www.wallpaperflare.com Every Website

Slide 11

Slide 11 text

Slide 12

Slide 12 text

this.innerHTML = ` Clicked 0 Times `;

Slide 13

Slide 13 text

// Create the Shadow DOM this.root = this.attachShadow({ mode: 'closed' }); // Inject HTML into it this.root.innerHTML = ` Clicked 0 Times `;

Slide 14

Slide 14 text

// Add styles to the Shadow DOM let stylesheet = new CSSStyleSheet(); stylesheet.replaceSync( `button { background-color: #08c; border: 1px solid #08c; color: #fff; }` ); this.root.adoptedStyleSheets = [stylesheet];

Slide 15

Slide 15 text

but web native?

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Reactivity

Slide 18

Slide 18 text

Reactivity DOM Diffing

Slide 19

Slide 19 text

Reactivity DOM Diffing Single File Components

Slide 20

Slide 20 text

but web native?

Slide 21

Slide 21 text

Slide 22

Slide 22 text

The Grain of the Web

Slide 23

Slide 23 text

Start End

Slide 24

Slide 24 text

Start End 1 A new approach…

Slide 25

Slide 25 text

Start End 1 A new approach… Tips & Tricks 2

Slide 26

Slide 26 text

Start End 1 A new approach… Tips & Tricks 2 3 Examples

Slide 27

Slide 27 text

A new approach… (that’s kind of old-school) 1

Slide 28

Slide 28 text

Try not to bring React’s mindset with you… Do you really need to invent an entirely new component from scratch? Or can you use HTML up until it reaches its limit and then enhance the markup? — Jeremy Keith https://adactio.com/journal/20618

Slide 29

Slide 29 text

Merlin

Beard & books

Ursula

Really wants to sing

Radagast

Prefers dogs over people

Slide 30

Slide 30 text

Merlin

Beard & books

Ursula

Really wants to sing

Radagast

Prefers dogs over people

Slide 31

Slide 31 text

Merlin

Beard & books

Slide 32

Slide 32 text

BIG benefits

Slide 33

Slide 33 text

Easier to Write

Slide 34

Slide 34 text

this.innerHTML = ` Clicked 0 Times `; YUCK!

Slide 35

Slide 35 text

new Accordion('#wizards');

Slide 36

Slide 36 text

Slide 37

Slide 37 text

Slide 38

Slide 38 text

Better Performance

Slide 39

Slide 39 text

=*(

Slide 40

Slide 40 text

Slide 41

Slide 41 text

// Wizard info new Accordion('#wizards'); // FAQs new Accordion('#faq', { heading: 'h3', autoclose: true }); 👋 Bye!

Slide 42

Slide 42 text

Tips & Tricks 2

Slide 43

Slide 43 text

Clicked 0 Times

Slide 44

Slide 44 text

customElements.define( 'count-up', class extends HTMLElement { // Instantiate component constructor () { // ... } } );

Slide 45

Slide 45 text

constructor () { // Always run first super(); // Your code... }

Slide 46

Slide 46 text

Encapsulation

Slide 47

Slide 47 text

constructor () { // Always run first super(); // Define properties this.count = 0; this.btn = this.querySelector('button'); }

Slide 48

Slide 48 text

count-up button { background-color: #08c; border: 1px solid #08c; color: #fff; }

Slide 49

Slide 49 text

/* JS is loaded! */ count-up:defined { /* ... */ } /* No JS yet */ count-up:not(:defined) { /* ... */ }

Slide 50

Slide 50 text

🎉 Event Handling

Slide 51

Slide 51 text

constructor () { // ... 
 // Define properties this.count = 0; this.btn = this.querySelector('button'); // Listen for click events this.btn.addEventListener('click', this); }

Slide 52

Slide 52 text

// Handle click events handleEvent (event) { this.count++; this.btn.textContent = `Clicked ${this.count} Times`; }

Slide 53

Slide 53 text

// Handle events handleEvent (event) { this[`on${event.type}`](event); } // Handle click events onclick (event) { console.log('clicked!'); }

Slide 54

Slide 54 text

⚙ Declarative Settings

Slide 55

Slide 55 text

Clicked 42 Times

Slide 56

Slide 56 text

constructor () { // Always run first super(); // Define properties let start = this.getAttribute('start'); this.count = parseFloat(start) || 0; this.btn = this.querySelector('button'); }

Slide 57

Slide 57 text

Clicked 0 Times Clicked 42 Times

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Attribute Reactivity

Slide 60

Slide 60 text

Attribute Reactivity

Slide 61

Slide 61 text

Clicked 0 Times

Slide 62

Slide 62 text

// Define the attributes to observe static observedAttributes = ['pause'];

Slide 63

Slide 63 text

// Runs when the value of // an attribute is changed on the component attributeChangedCallback (name, oldVal, newVal) { this.btn.removeEventListener('click', this); } // Define the attributes to observe static observedAttributes = ['pause'];

Slide 64

Slide 64 text

attributeChangedCallback (name, oldVal, newVal) { if (newVal === null) { this.btn.addEventListener('click', this); } else { this.btn.removeEventListener('click', this); } }

Slide 65

Slide 65 text

Examples 3

Slide 66

Slide 66 text

Show More

Now you see me!

Slide 67

Slide 67 text

Show More

Now you see me!

Slide 68

Slide 68 text

customElements.define( 'show-hide', class extends HTMLElement { // … } );

Slide 69

Slide 69 text

// Instantiate the component constructor () { // Run first super(); // Define properties this.btn = this.querySelector('button'); this.content = this.btn.nextElementSibling; }

Slide 70

Slide 70 text

constructor () { // ... // Define properties this.btn = this.querySelector('button'); this.content = this.btn.nextElementSibling; // Set initial UI this.btn.setAttribute('aria-expanded', false); this.content.setAttribute('hidden', ''); }

Slide 71

Slide 71 text

Show More

Now you see me!

Slide 72

Slide 72 text

constructor () { // ... // Set initial UI this.btn.setAttribute('aria-expanded', false); this.content.setAttribute('hidden', ''); // Listen for click events this.btn.addEventListener('click', this); }

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

handleEvent (event) { // If the content is expanded, hide it // Otherwise, show it if (this.btn.getAttribute('aria-expanded') === 'true') { // ... } else { // ... } }

Slide 75

Slide 75 text

handleEvent (event) { // If the content is expanded, hide it // Otherwise, show it if (this.btn.getAttribute('aria-expanded') === 'true') { this.btn.setAttribute('aria-expanded', false); this.content.setAttribute('hidden', ''); } else { this.btn.setAttribute('aria-expanded', true); this.content.removeAttribute('hidden'); } }

Slide 76

Slide 76 text

show-hide button { background-color: #08c; border: 1px solid #08c; color: #fff; } show-hide button[aria-expanded="true"] { background-color: #343d4d; border: 1px solid #343d4d; }

Slide 77

Slide 77 text

show-hide:not(:defined) button { display: none; }

Slide 78

Slide 78 text

Merlin

Beard & books

Ursula

Really wants to sing

Radagast

Prefers dogs over people

Slide 79

Slide 79 text

Merlin

Beard & books

Ursula

Really wants to sing

Radagast

Prefers dogs over people

Slide 80

Slide 80 text

...
...

Slide 81

Slide 81 text

...
...

Slide 82

Slide 82 text

Slide 83

Slide 83 text

Slide 84

Slide 84 text

Wrapping Up

Slide 85

Slide 85 text

🐴 Start with HTML

Slide 86

Slide 86 text

🐴 Start with HTML 🦄 Enhance with Web Components

Slide 87

Slide 87 text

GoMakeThings.com