Components on the Web

A presentation at Fronteers in October 2019 in Amsterdam, Netherlands by Jack Franklin

Slide 1

Slide 1

Slide 2

Slide 2

time in the world amount of components

Slide 3

Slide 3

time components mistakes made building

Slide 4

Slide 4

time components learning about

Slide 5

Slide 5

WHAT ARE COMPONENTS ?

Slide 6

Slide 6

Cristiano Rastelli @areaweb

Slide 7

Slide 7

WHERE HAVE WE COME FROM ?

Slide 8

Slide 8

Slide 9

Slide 9

Slide 10

Slide 10

Slide 11

Slide 11

Model

Slide 12

Slide 12

Model View

Slide 13

Slide 13

Model View Controller

Slide 14

Slide 14

Model View Controller

Slide 15

Slide 15

Model View Controller

Slide 16

Slide 16

Model View Controller

Slide 17

Slide 17

Model View Controller

Slide 18

Slide 18

Slide 19

Slide 19

Slide 20

Slide 20

Slide 21

Slide 21

Slide 22

Slide 22

Slide 23

Slide 23

Slide 24

Slide 24

<a href=”/notifications”> Notifications </a>

Slide 25

Slide 25

n h2 div n main an div na v em div an em img sp an em em sp a em img na v h2 a na img v div a sp a sp img str ong sp 4 h h2 a h2

Slide 26

Slide 26

Slide 27

Slide 27

Components

Slide 28

Slide 28

Slide 29

Slide 29

React

Slide 30

Slide 30

React Svelte

Slide 31

Slide 31

React Svelte Polymer

Slide 32

Slide 32

React Svelte Polymer Web components

Slide 33

Slide 33

React Svelte Polymer Angular Web components

Slide 34

Slide 34

React Svelte Vue Polymer Angular Web components

Slide 35

Slide 35

React Svelte Vue Polymer Angular lit-html Web components

Slide 36

Slide 36

React Svelte Vue Polymer Preact Angular lit-html Web components

Slide 37

Slide 37

React Ember Svelte Vue Polymer Preact Angular lit-html Web components

Slide 38

Slide 38

twitter feed search form icon tweet input tweet tweet

Slide 39

Slide 39

Slide 40

Slide 40

Slide 41

Slide 41

Slide 42

Slide 42

The examples in this deck use React

Slide 43

Slide 43

The examples in this deck use React But this is not a React talk

Slide 44

Slide 44

That’s the theory…

Slide 45

Slide 45

Let’s get into the reality…

Slide 46

Slide 46

Slide 47

Slide 47

our eight year old app is built on Backbone! we’v e got Ang a bu ular n c can 1 c omp h of o ‘t pr ld one iorit nts ise we ugp radi ng

Slide 48

Slide 48

we h ave a bu jQu n ch o ery f6y tha ear t fu per ncti o ld fect ons ly w ell

Slide 49

Slide 49

Slide 50

Slide 50

Slide 51

Slide 51

Slide 52

Slide 52

Slide 53

Slide 53

Slide 54

Slide 54

Components are designed to be black boxes of functionality

Slide 55

Slide 55

We’d like a nice carousel on our home page <fancy-carousel speed=”1000” infinite-playback ></fancy-carousel>

Slide 56

Slide 56

strive for black box components

Slide 57

Slide 57

black box components let you work in isolation

Slide 58

Slide 58

thread.com/shop

Slide 59

Slide 59

We’ve had bug reports on the site where users are unable to like or dislike individual items. Please could you investigate? Thanks!

Slide 60

Slide 60

thread.com/shop

Slide 61

Slide 61

thread.com/shop

Slide 62

Slide 62

thread.com/shop

Slide 63

Slide 63

thread.com/shop

Slide 64

Slide 64

ItemCard

Slide 65

Slide 65

<ExclusiveBadge /> <ItemImage /> <ItemShipping /> <ItemDetails /> <ItemSentiment /> <ItemSize /> ItemCard

Slide 66

Slide 66

<ItemImage /> <ItemShipping /> <ItemDetails /> <ItemSentiment /> <ItemSize /> ItemCard

Slide 67

Slide 67

<ItemShipping /> <ItemDetails /> <ItemSentiment /> <ItemSize /> ItemCard

Slide 68

Slide 68

<ItemDetails /> <ItemSentiment /> <ItemSize /> ItemCard

Slide 69

Slide 69

<ItemSentiment /> <ItemSize /> ItemCard

Slide 70

Slide 70

<ItemSentiment /> ItemCard

Slide 71

Slide 71

/item­ sentiment sentiment.j s sentiment.module.c ss sentiment.test.js sentiment­ positive.svg sentiment­ negative.svg

Slide 72

Slide 72

keep as little in your head as possible

Slide 73

Slide 73

e h t i l i b a s u re w a s e e s y t

Slide 74

Slide 74

twitter feed these are a ll the same comp onen t tweet tweet tweet

Slide 75

Slide 75

please build a component to view an item of clothing on Thread.com

Slide 76

Slide 76

Done! <thread-item product-id=”1234” ></thread-item>

Slide 77

Slide 77

sometimes it needs to show the brand as well as the title

Slide 78

Slide 78

Done! <thread-item product-id=”1234” include-brandname ></thread-item>

Slide 79

Slide 79

and we need to specify if to show the old price alongside the new sale price

Slide 80

Slide 80

Done! <thread-item product-id=”1234” include-brandname show-old-pricesale ></thread-item>

Slide 81

Slide 81

and sometimes we might not want sentiment

Slide 82

Slide 82

Done! <thread-item product-id=”1234” include-brandname show-old-pricesale no-sentiment ></thread-item>

Slide 83

Slide 83

more options lead to more confusion

Slide 84

Slide 84

Slide 85

Slide 85

Slide 86

Slide 86

Slide 87

Slide 87

Slide 88

Slide 88

Past Jack

Slide 89

Slide 89

Past Jack Present Jack

Slide 90

Slide 90

Past Jack Present Jack Future Jack

Slide 91

Slide 91

Past Jack Present Jack Future Jack

Slide 92

Slide 92

he’s an idiot Past Jack Present Jack Future Jack

Slide 93

Slide 93

actually, he’s always trying his best Past Jack Present Jack Future Jack

Slide 94

Slide 94

Past Jack

Slide 95

Slide 95

remember, there is no such thing as a bad decision, only a decision made with less data then you have now. Past Jack

Slide 96

Slide 96

remember, there is no such thing as a bad decision, only a decision made with less data then you have now. Past Jack so great job, past Jack!

Slide 97

Slide 97

Past Jack Present Jack Future Jack

Slide 98

Slide 98

“ Past Jack what can I learn from Past Jack? Present Jack Future Jack

Slide 99

Slide 99

“ what can I learn from Past Jack? Present Jack Past Jack “ that will make Future Jack’s life easier? Future Jack

Slide 100

Slide 100

this does not work well! <thread-item product-id=”1234” include-brandname show-old-pricesale no-sentiment ></thread-item>

Slide 101

Slide 101

e h t u s p e e r c w a s e e s u o y n o p

Slide 102

Slide 102

You will constantly trade off maintainability and configurability This is one of the hardest things to get right consistently

Slide 103

Slide 103

“Feed” “Full” m o C ” ” t c a p

Slide 104

Slide 104

“Feed” “Full” m o C ” ” t c a p

Slide 105

Slide 105

“Full” “ e m “Feed” a s e h t y l r a e n m o C ” ” t c a p

Slide 106

Slide 106

allowing components to differ slightly when required

Slide 107

Slide 107

variants

Slide 108

Slide 108

Encode an allowed set of variants rather than individual options that can be toggled on or off. export const variants = { FULL, COMPACT, FEED }

Slide 109

Slide 109

Users can explicitly pass in the variant they want. <ItemCard variant={variants.COMPACT} />

Slide 110

Slide 110

always prefer explicit configuration over implicit options

Slide 111

Slide 111

breaking components down

Slide 112

Slide 112

components are cheap, but not free

Slide 113

Slide 113

Slide 114

Slide 114

£ maintaining the component

Slide 115

Slide 115

£ maintaining the component £ complexity in the system

Slide 116

Slide 116

£ maintaining the component £ complexity in the system £ communication between components

Slide 117

Slide 117

£ maintaining the component £ complexity in the system £ communication between components £ documentation

Slide 118

Slide 118

components should be small and you should have lots of them

Slide 119

Slide 119

and complexity component size When is a component ready to be split up? time

Slide 120

Slide 120

and complexity component size When is a component ready to be split up? time

Slide 121

Slide 121

and complexity component size When is a component ready to be split up? time

Slide 122

Slide 122

and complexity component size When is a component ready to be split up? time

Slide 123

Slide 123

?

Slide 124

Slide 124

? How many lines of code is it?

Slide 125

Slide 125

? How many lines of code is it? How many different HTML elements does this component render?

Slide 126

Slide 126

? How many lines of code is it? How many different HTML elements does this component render? How “hard” is this component to understand?

Slide 127

Slide 127

? How many lines of code is it? How many different HTML elements does this component render? How “hard” is this component to understand? Is the component hard to name? Do I naturally want to put “and” in the name?

Slide 128

Slide 128

? How many lines of code is it? How many different HTML elements does this component render? How “hard” is this component to understand? c i r Is the component hard to name? Do IdP n A ename? naturally want to put “and” in the m a N m e t I ♂ e

Slide 129

Slide 129

there are no hard rules here find a set of guidelines that you’re comfortable with

Slide 130

Slide 130

breaking the black box abstraction

Slide 131

Slide 131

could you pick up a component and drop it into a new project with no additional work or setup?

Slide 132

Slide 132

not all of your components can or should be black boxes

Slide 133

Slide 133

Slide 134

Slide 134

“ an yo hel ne lo th …is er e?

Slide 135

Slide 135

Your app’s data Your app’s components

Slide 136

Slide 136

Your app’s data Your app’s components

Slide 137

Slide 137

Your app’s data Your app’s components

Slide 138

Slide 138

Your app’s data Your app’s components

Slide 139

Slide 139

how do we give a component data?

Slide 140

Slide 140

1 fetching from an API 2 via attributes passed in HTML 3 via data in script tags

Slide 141

Slide 141

1 fetching from an API 1. Component renders loading spinner 2. Make request to /api to fetch JSON 3. Parse response 4. Render data onto page

Slide 142

Slide 142

1 fetching from an API ✅1.Component the most up to date data Componentalways rendershas loading spinner 2.Easy Maketorequest tothe /apidata to fetch ✅ re-fetch later.JSON 3. Parse response ❌Have to handle error state 4. Render data onto page ❌JS dependency on showing the user anything

Slide 143

Slide 143

2 via attributes passed in HTML

Slide 144

Slide 144

2 via attributes passed in HTML ✅ no API required to fetch data ✅ no error state ❌long strings in HTML ❌data might be out of date by the time it’s rendered

Slide 145

Slide 145

3 via data in script tags

Slide 146

Slide 146

3 via data in script tags ✅ useful for data that’s global across your system ❌can get outdated ❌user could change this data - so be careful what you use it for!

Slide 147

Slide 147

1 fetching from an API 2 via attributes passed in HTML 3 via data in script tags

Slide 148

Slide 148

the path to components in your application

Slide 149

Slide 149

2 years ago jQuery, jQuery and more jQuery Server side templates with Django No true component system in place Today ~100 components across the site Reusable components make it easier for others to contribute Starting to form a Thread design system to improve consistency We’ve never stopped shipping features.

Slide 150

Slide 150

Slide 151

Slide 151

software migrations

Slide 152

Slide 152

value added ground up rewrite time to reach feature parity time ew n g n i d uil b nd a d e p p shi es r u t fea

Slide 153

Slide 153

value added ground up incremental rewrite rebuild with new features time

Slide 154

Slide 154

value added ground up incremental rewrite rebuild with new features time

Slide 155

Slide 155

value added ground up incremental rewrite rebuild with new features slower at first as you build tools to bridge the legacy gap, but quickly you’re shipping and adding value time

Slide 156

Slide 156

incremental rebuilds let you test assumptions

Slide 157

Slide 157

Slide 158

Slide 158

Slide 159

Slide 159

?

Slide 160

Slide 160

how we adopted CSS Modules at Thread

Slide 161

Slide 161

hey, I think we should try CSS Modules at Thread because…

Slide 162

Slide 162

sounds great to me, let’s give it a go on the next component we build!

Slide 163

Slide 163

Slide 164

Slide 164

Regular CSS, global styles, trying to name things uniquely

Slide 165

Slide 165

Regular CSS, global styles, trying to name things uniquely

Slide 166

Slide 166

Regular CSS, global styles, trying to name things uniquely CSS Modules

Slide 167

Slide 167

WE DISLIKE IT Replace the CSS Modules component with our usual solution. We learned a bunch and reversed out easily.

Slide 168

Slide 168

WE DISLIKE IT WE LIKE IT! Replace the CSS Modules component with our usual solution. We learned a bunch and reversed out easily. Decide on next step: enforce that every component uses CSS Modules from now on decide to experiment or figure out how to answer any more concerns that we have.

Slide 169

Slide 169

Yes, all your components should be built using the same technology. But don’t let ideals get in the way of experimenting and trying something new. A failed experiment is a great chance to learn.

Slide 170

Slide 170

It’s easy to get bogged down comparing and debating different frameworks, libraries, and approaches It’s quicker to just try it.

Slide 171

Slide 171

Thank You javascriptplayground.com fishandscripts.com @fishandscripts @Jack_Franklin

Slide 172

Slide 172

Slide 173

Slide 173

<div>Icons made by <a href=”https://www.flaticon.com/authors/freepik ” title=”Freepik”>Freepik</a> from <a href=”https://www.flaticon.com/” title=”Flaticon”>www.flaticon.com</a></div>