Slide 1

Slide 1 text

Adam Onishi Financial Times @onishiweb Planes, trains, and CSS components

Slide 2

Slide 2 text

FINANCIAL TIMES @onishiweb

Slide 3

Slide 3 text

@onishiweb

Slide 4

Slide 4 text

1. Unify front-end styles across the FT 2. Reduce time spent repeating work @onishiweb

Slide 5

Slide 5 text

Components! @onishiweb

Slide 6

Slide 6 text

http://registry.origami.ft.com/components o-header o-table o-card

Slide 7

Slide 7 text

http://origami.ft.com/

Slide 8

Slide 8 text

Building components @onishiweb

Slide 9

Slide 9 text

@onishiweb

Slide 10

Slide 10 text

@onishiweb

Slide 11

Slide 11 text

1. Automobiles 2. Trains 3. Planes

Slide 12

Slide 12 text

1. CSS 2. CSS Modules 3. Web Components

Slide 13

Slide 13 text

The CSS way…

Slide 14

Slide 14 text

The current way… @onishiweb

Slide 15

Slide 15 text

The Sass/LESS way @onishiweb

Slide 16

Slide 16 text

The BEM way @onishiweb

Slide 17

Slide 17 text

The SMACSS way @onishiweb

Slide 18

Slide 18 text

The OOCSS way @onishiweb

Slide 19

Slide 19 text

The Atomic CSS way @onishiweb

Slide 20

Slide 20 text

@onishiweb • Naming convention • Namespacing • Reduce specificity • Easy to understand • Require discipline

Slide 21

Slide 21 text

The Origami way… @onishiweb

Slide 22

Slide 22 text

Specificity must be minimised. Use BEM… Origami spec http://origami.ft.com/docs/syntax/scss/

Slide 23

Slide 23 text

An example: @onishiweb

Slide 24

Slide 24 text

@onishiweb

Slide 25

Slide 25 text

Slide 26

Slide 26 text

Slide 27

Slide 27 text

o-gallery__figure o-gallery__image o-gallery__caption

Slide 28

Slide 28 text

.o-gallery__figure {} .o-gallery__figure__image {} .o-gallery__figure__caption {}

Slide 29

Slide 29 text

.o-gallery__figure {} .o-gallery__figure__image {} .o-gallery__figure__caption {} // Better .o-gallery__figure {} .o-gallery__image {} .o-gallery__caption {}

Slide 30

Slide 30 text

.o-gallery__control { position: absolute; top: 50%; width: 30px; height: 70px; } .o-gallery__control--left { left: 0; background-image: url(''), none; } .o-gallery__control--right { right: 0; background-image: url(''), none; }

Slide 31

Slide 31 text

this.controlLeft = document.createElement('button'); this.controlLeft.className = 'o-gallery__control o-gallery__control-- left';

Slide 32

Slide 32 text

Using Origami components @onishiweb

Slide 33

Slide 33 text

@import "o-assets/main"; @import "o-colors/main"; @import "o-icons/main"; @import "o-hoverable/main"; @import "o-gallery/main";

Slide 34

Slide 34 text

require('o-date'); require('o-header'); require('o-gallery');

Slide 35

Slide 35 text

Alice Bartlett Origami Lead, Financial Times @alicebartlett Can’t you make it more like Bootstrap? Considerations for building front end systems

Slide 36

Slide 36 text

What about HTML? @onishiweb

Slide 37

Slide 37 text

Customisation @onishiweb

Slide 38

Slide 38 text

/// General styling for the gallery @mixin oGallery { [...] } /// Styling for the gallery title @mixin oGalleryTitle { [...] }

Slide 39

Slide 39 text

The Cascade @onishiweb

Slide 40

Slide 40 text

The current Origami way… @onishiweb

Slide 41

Slide 41 text

1. CSS 2. CSS Modules 3. Web Components

Slide 42

Slide 42 text

Remix culture @onishiweb

Slide 43

Slide 43 text

@onishiweb

Slide 44

Slide 44 text

The CSS Modules way…

Slide 45

Slide 45 text

What is CSS Modules? @onishiweb

Slide 46

Slide 46 text

[CSS where] all class names and animation names are scoped locally by default CSS Modules Spec https://github.com/css-modules/css-modules

Slide 47

Slide 47 text

.gallery { } .wrapper { } .content { } .slide { }

Slide 48

Slide 48 text

._gallery_1rsuk_1 { } ._wrapper_1rsuk_7 { } ._content_1rsuk_13 { } ._slide_1rsuk_25 { }

Slide 49

Slide 49 text

Values and composes @onishiweb

Slide 50

Slide 50 text

@value primary: red; .btn-primary { background: white; border: 1px solid primary; color: primary; }

Slide 51

Slide 51 text

.btn-primary { background: white; border: 1px solid red; color: red; } .btn-secondary { composes: btn-primary; border-color: blue; color: blue; }

Slide 52

Slide 52 text

Slide 53

Slide 53 text

Truly component only CSS @onishiweb

Slide 54

Slide 54 text

Requires a build step @onishiweb

Slide 55

Slide 55 text

@onishiweb • Webpack • Browserify • Babel • PostCSS • JSPM

Slide 56

Slide 56 text

An example: @onishiweb

Slide 57

Slide 57 text

@onishiweb

Slide 58

Slide 58 text

No more .html @onishiweb

Slide 59

Slide 59 text

  • # This is an image caption

Slide 60

Slide 60 text

" className('styles', 'image') %>" 'caption') %>" This is an image caption
<%= className('styles', 'gallery') %>

Slide 61

Slide 61 text

What about the JavaScript? @onishiweb

Slide 62

Slide 62 text

this.controlLeft = document.createElement('button'); this.controlLeft.className = className('styles', 'control-left');

Slide 63

Slide 63 text

Great in a JavaScript world @onishiweb

Slide 64

Slide 64 text

Atomic CSS Modules @onishiweb

Slide 65

Slide 65 text

We decided to solve the main Atomic CSS-related problems using the power of CSS Modules, getting all the benefits of both solutions. Michele Bertoli https://medium.com/yplan-eng/atomic-css-modules-cb44d5993b27#.p7jtpneoh

Slide 66

Slide 66 text

Is Origami going to use CSS Modules? @onishiweb

Slide 67

Slide 67 text

No.

Slide 68

Slide 68 text

First Class Styles - Mark Dalgleish Front Trends 2016 https://www.youtube.com/watch?v=KmtgJ1d4zuY

Slide 69

Slide 69 text

The alternative way… @onishiweb

Slide 70

Slide 70 text

Delayed 1. Automobiles 2. Trains 3.

Slide 71

Slide 71 text

1. CSS 2. CSS Modules 3. Web Components

Slide 72

Slide 72 text

The Web Components way…

Slide 73

Slide 73 text

Create your own elements and encapsulate their CSS

Slide 74

Slide 74 text

@onishiweb But what does that mean‽

Slide 75

Slide 75 text

Slide 76

Slide 76 text

text text text

Slide 77

Slide 77 text

Warning! New specifications and lots of code coming up @onishiweb

Slide 78

Slide 78 text

Custom Elements @onishiweb

Slide 79

Slide 79 text

Slide 80

Slide 80 text

Slide 81

Slide 81 text

customElements.define('o-gallery', …);

Slide 82

Slide 82 text

class OGallery extends HTMLElement { constructor() { super(); [...] } [...] } customElements.define('o-gallery', OGallery);

Slide 83

Slide 83 text

class OGallery extends HTMLElement { connectedCallback() { [...] } }

Slide 84

Slide 84 text

const oGallery = document.createElement('o-gallery'); // Show the next slide oGallery.nextSlide(); // Show the previous slide oGallery.previousSlide();

Slide 85

Slide 85 text

text text text

Slide 86

Slide 86 text

Shadow DOM @onishiweb

Slide 87

Slide 87 text

This is where things get spooky and weird! Soledad Penades https://www.youtube.com/watch?v=2vWgJ7w3hu0

Slide 88

Slide 88 text

@onishiweb

Slide 89

Slide 89 text

Creating a Shadow DOM @onishiweb

Slide 90

Slide 90 text

constructor() { super(); const shadow = this.attachShadow({ mode: 'open' }); }

Slide 91

Slide 91 text

Populating Shadow DOM @onishiweb

Slide 92

Slide 92 text

text text text

Slide 93

Slide 93 text

Templates @onishiweb

Slide 94

Slide 94 text

Slide 95

Slide 95 text

// Attaches a shadow Root to your element const shadowRoot = this.attachShadow({mode: 'open'}); // Get the custom template, clone it const template = document.querySelector('#o-gallery'); const clone = this.importNode(template, true); // Insert it into our shadow DOM shadowRoot.appendChild(clone);

Slide 96

Slide 96 text

Slots @onishiweb

Slide 97

Slide 97 text

Slide 98

Slide 98 text

Delayed plane

Slide 99

Slide 99 text

#shadow-root
Delayed plane

Slide 100

Slide 100 text

Styling web components @onishiweb

Slide 101

Slide 101 text

:host { background-color: #333; } #slide { margin: 0; } img { display: block; } .caption { color: #fff; }

Slide 102

Slide 102 text

Fully encapsulated styles @onishiweb

Slide 103

Slide 103 text

Shadow Boundary @onishiweb

Slide 104

Slide 104 text

::host @onishiweb

Slide 105

Slide 105 text

::host { background-color: #333; } ::host(.theme—light) { background-color: #fff1e0; } ::host(:hover) { /* hover styles */ }

Slide 106

Slide 106 text

::host-context @onishiweb

Slide 107

Slide 107 text

::host-context(.theme-reverse) { background-color: #fff1e0; } ::host-context(.streams) { /* styles */ }

Slide 108

Slide 108 text

::slotted @onishiweb

Slide 109

Slide 109 text

::slotted(img) { display: block; margin: 0; } ::slotted(figcaption) { font-style: italic; margin: 10px 0; }

Slide 110

Slide 110 text

@onishiweb Shadow Host Shadow Root Shadow Boundary Slots Light DOM Shadow DOM Composed DOM Shadow Tree

Slide 111

Slide 111 text

Customising Web Components @onishiweb

Slide 112

Slide 112 text

CSS Custom properties @onishiweb

Slide 113

Slide 113 text

/* product CSS */ --caption-color: #fff1e0; /* Component styles */ .caption { color: var(--caption-color, white); }

Slide 114

Slide 114 text

CSS Mixins @onishiweb

Slide 115

Slide 115 text

.caption { --caption-styles: { color: red; font-size: 14px; }; } .caption { @apply(--caption-styles); }

Slide 116

Slide 116 text

Do I need to use every one of the specifications? @onishiweb

Slide 117

Slide 117 text

No @onishiweb

Slide 118

Slide 118 text

What about Progressive Enhancement? @onishiweb

Slide 119

Slide 119 text

Everything is JavaScript @onishiweb

Slide 120

Slide 120 text

text text text

Slide 121

Slide 121 text

::defined @onishiweb

Slide 122

Slide 122 text

o-gallery:not(:defined) { display: block; } o-gallery:not(:defined) img { display: none; } o-gallery:not(:defined) img:first-child { display: block; width: 100%; height: auto; }

Slide 123

Slide 123 text

Extending native elements @onishiweb

Slide 124

Slide 124 text

Press me!

Slide 125

Slide 125 text

Apple objects to extending subclasses of HTMLElement using is= Ryosuke Niwa https://github.com/w3c/webcomponents/issues/509#issuecomment-222860736

Slide 126

Slide 126 text

Let a button be a button! @onishiweb

Slide 127

Slide 127 text

https://github.com/webcomponents/gold-standard/wiki The Web Components Gold Standard

Slide 128

Slide 128 text

This will make sure accessibility is better too! @onishiweb

Slide 129

Slide 129 text

What this means for Origami @onishiweb

Slide 130

Slide 130 text

A lot more design opinionated @onishiweb

Slide 131

Slide 131 text

We can offer markup too… @onishiweb

Slide 132

Slide 132 text

https://github.com/Polymer/vulcanize Bundling HTML imports

Slide 133

Slide 133 text

HTTP/2? @onishiweb

Slide 134

Slide 134 text

A lot more work to do @onishiweb

Slide 135

Slide 135 text

Using Web Components today @onishiweb

Slide 136

Slide 136 text

The road to v1… @onishiweb

Slide 137

Slide 137 text

Polyfill @onishiweb

Slide 138

Slide 138 text

Libraries @onishiweb

Slide 139

Slide 139 text

@onishiweb • Polymer • x-tag • Bosonic

Slide 140

Slide 140 text

Beware! Here be dragons…

Slide 141

Slide 141 text

http://webcomponents.org/

Slide 142

Slide 142 text

https://developers.google.com/web/fundamentals/primers/customelements/

Slide 143

Slide 143 text

https://developers.google.com/web/fundamentals/primers/customelements/

Slide 144

Slide 144 text

Web Components are here!

Slide 145

Slide 145 text

Almost…

Slide 146

Slide 146 text

Honestly, like really close…

Slide 147

Slide 147 text

1. Automobiles 2. Trains 3. Planes

Slide 148

Slide 148 text

1. CSS 2. CSS Modules 3. Web Components

Slide 149

Slide 149 text

Adam Onishi Financial Times @onishiweb Thank you!