Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Thanks to our sponsors!

Slide 3

Slide 3 text

Andrey Okonetchnikov @okonetchnikov

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

ColorSnapper http://colorsnapper.com

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Past Current time Future

Slide 8

Slide 8 text

A brief history of CSS

Slide 9

Slide 9 text

CSS 1.0 Present Future CSS 2.1 Proposed 1996 2004

Slide 10

Slide 10 text

Present Future 1996 2004

Slide 11

Slide 11 text

CSS was designed for documents

Slide 12

Slide 12 text

How many of you have ever re-designed a web-site with CSS changes only? ✋

Slide 13

Slide 13 text

Present Future 1996 2004

Slide 14

Slide 14 text

Present Future 1996 2004 2005

Slide 15

Slide 15 text

2005 Present Future 1996 2004

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Web-applications are built with UI Components

Slide 20

Slide 20 text

UI Components Button

Slide 21

Slide 21 text

Button UI Components

Slide 22

Slide 22 text

20+ years since the release of CSS 1.0

Slide 23

Slide 23 text

CSS was designed for documents, not for web-applications

Slide 24

Slide 24 text

Best practices

Slide 25

Slide 25 text

Separation of concerns

Slide 26

Slide 26 text

Separation of concerns JS CSS HTML

Slide 27

Slide 27 text

Separation of concerns JS CSS HTML Checkbox Button Dropdown List Modal

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Natural mapping

Slide 30

Slide 30 text

Natural mapping

Slide 31

Slide 31 text

Past Current time Future

Slide 32

Slide 32 text

HTML-in-JS a.k.a. JSX

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

https://trends.google.com/trends/explore?q=%2Fm%2F012l1vxv, %2Fm%2F0268gyp

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Everything is a component!

Slide 40

Slide 40 text

f(state) => UI

Slide 41

Slide 41 text

What about styles?

Slide 42

Slide 42 text

• Logic • HTML • Styles Button • Logic • HTML • Styles • Logic • HTML • Styles

Slide 43

Slide 43 text

• Logic • HTML • Styles Button • Logic • HTML • Styles • Logic • HTML • Styles Global styles

Slide 44

Slide 44 text

• Logic • HTML • Styles Button • Logic • HTML • Styles • Logic • HTML • Styles Global styles

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Global CSS 1 .btn { 2 /* styles for button */ 3 } 4 5 .active { 6 /* styles for active button */ 7 background-color: blue; 8 } 9 10 .label { 11 /* styles for button label */ 12 } 1 .star { 2 /* styles for star */ 3 } 4 5 .active { 6 /* styles for active star 7 background-color: orange; 8 } 9

Slide 47

Slide 47 text

.btn.active

Slide 48

Slide 48 text

.content .albums .btn.active

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

BEM https://en.bem.info/

Slide 52

Slide 52 text

B E M

Slide 53

Slide 53 text

.Block .Block --element .Block --element __modifier Block Element Modifier

Slide 54

Slide 54 text

BEM Click me!

Slide 55

Slide 55 text

BEM Click me!

Slide 56

Slide 56 text

Manual work

Slide 57

Slide 57 text

Slide 58

Slide 58 text

.nav __item { padding: 4px 10px; color: black; } .nav __item_active { font-weight: bold; background: #ffc7c7; } .navigation __item { padding: 4px 10px; color: black; } .navigation __item_active { font-weight: bold; background: #ffc7c7; }

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Manual work

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

+

Slide 64

Slide 64 text

https://github.com/css-modules/css-modules

Slide 65

Slide 65 text

Before: BEM-style const Button = ({ children }) => ( { children } )

Slide 66

Slide 66 text

After: CSS-modules import styles from './Button.css' const Button = ({ children }) => ( { children } )

Slide 67

Slide 67 text

After: CSS-modules import styles from './Button.css' const Button = ({ children }) => ( { children } )

Slide 68

Slide 68 text

How CSS-modules work?

Slide 69

Slide 69 text

Before: BEM-style /* Button.css */ .Button { /* general rules */ } .Button --disabled { /* disabled rules */ } .Button --active { /* active rules */ } .Button __label { /* label rules */ }

Slide 70

Slide 70 text

After: CSS-modules /* Button.css */ .root { /* general rules */ } .disabled { /* disabled rules */ } .active { /* active rules */ } .label { /* label rules */ }

Slide 71

Slide 71 text

The result 1 styles: { 2 root: "Button __root __abc5436", 3 disabled: "Button __disabled __def65 4 active: "Button __active __1638bcd", 5 label: "Button __label __5dfg462" 5 } 1 /* Button.css */ 2 3 .root { } 4 .disabled { } 5 .active { } 6 .label { }

Slide 72

Slide 72 text

CSS-modules import styles from './Button.css' const Button = ({ children }) => ( { children } )

Slide 73

Slide 73 text

BEM for free! ❤

Slide 74

Slide 74 text

CSS-modules ✅ Explicit imports ✅ Scoped & fast selectors ✅ True rules isolation ✅ Code reuse, expressiveness ✅ Framework agnostic Non standard syntax (compose, vals, etc.) Build step is required No dead code elimination No automatic vendor prefixing

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

+

Slide 77

Slide 77 text

https://cssinjs.org

Slide 78

Slide 78 text

export const styles = { button: { padding: '10px', '&:hover': { background: 'blue' } }, '@media (min-width: 1024px)': { button: { padding: '20px' } } }

Slide 79

Slide 79 text

Before: CSS-modules import styles from './Button.css' const Button = ({ children }) => ( { children } )

Slide 80

Slide 80 text

After: JSS import injectSheet from 'react-jss' import styles from './styles' const Button = ({ classes, children }) => ( {children} ) export default injectSheet(styles)(Button)

Slide 81

Slide 81 text

CSS-in-JS (JSS) ✅ Explicit imports ✅ Scoped & fast selectors ✅ True rules isolation ✅ Code reuse, expressiveness ✅ Framework agnostic ✅ Uses w3c standard ✅ No build step is required ✅ Dead code elimination ✅ Automatic vendor prefixing

Slide 82

Slide 82 text

—If we generate class names, why do we still use class attribute?

Slide 83

Slide 83 text

https://www.styled-components.com/

Slide 84

Slide 84 text

// Create a Title component that'll render an

tag with some styles const Title = styled.h1` font-size: 1.5em; text-align: center; color: palevioletred; `; render( Hello React Alicante! );

Slide 85

Slide 85 text

const Button = styled.button` /* Adapt the colors based on primary prop */ background: ${props => props.primary ? 'palevioletred' : 'white'}; color: ${props => props.primary ? 'white' : 'palevioletred'}; font-size: 1em; margin: 1em; padding: 0.25em 1em; border: 2px solid palevioletred; border-radius: 3px; `; render(
Normal Primary
);

Slide 86

Slide 86 text

styled-components ✅ No messing with classNames (implementation detail) ✅ Same mental model and structure for the whole application

Slide 87

Slide 87 text

“styled-components […] removes the mapping between components and styles.” https://www.styled-components.com/

Slide 88

Slide 88 text

Styled Components

Slide 89

Slide 89 text

Styled Primitives

Slide 90

Slide 90 text

styled-components for react-native import styled from 'styled-components/native'; const StyledView = styled.View` background-color: papayawhip; `; const StyledText = styled.Text` color: palevioletred; `; class MyReactNativeComponent extends React.Component { render() { return ( Hello World! ) } }

Slide 91

Slide 91 text

styled-components for Sketch

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Past Current time Future

Slide 94

Slide 94 text

Universal rendering

Slide 95

Slide 95 text

import React from 'react'; import {AppRegistry, Pano, Text, View} from 'react-vr'; class WelcomeToVR extends React.Component { render() { // Displays "hello" text on top of a loaded 360 panorama image. // Text is 0.8 meters in size and is centered three meters in front of you. return ( hello ); } }; AppRegistry.registerComponent('WelcomeToVR', () => WelcomeToVR);

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

Design systems shared between designers and developers!

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

Wait! But… Myths & misconceptions about CSS-in-JS

Slide 103

Slide 103 text

“CSS-in-JS is slow”

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

1. CSS-in-JS ≠ inline styles!

Slide 106

Slide 106 text

import { css } from 'glamor' const title = css({ fontSize: '1.8em', fontFamily: 'Comic Sans MS', color: 'blue' }) console.log(title) // → 'css-1pyvz'

Slide 107

Slide 107 text

2. CSS-in-JS is fast enough

Slide 108

Slide 108 text

http://cssinjs.org/function-values/

Slide 109

Slide 109 text

3. Maintanability > Speed

Slide 110

Slide 110 text

https://twitter.com/dan_abramov/status/842329893044146176

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

https://codepen.io/malyw/pen/XRRgeB

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

“Crafted CSS is better for performance”

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

https://meiert.com/en/blog/20170531/70-percent-css-repetition/ “In CSS, we repeat ourselves too much. While it’s absolutely, practically possible to limit declaration repetition to 10– 20%, reality averages 72% (median 66%).”

Slide 117

Slide 117 text

Functional CSS?

Slide 118

Slide 118 text

http://tachyons.io

Slide 119

Slide 119 text

Tachyons

Title

Lorem ipsum dolor sit amet.

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

Manual work

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

Enables CSS optimisations

Slide 125

Slide 125 text

…but uses very simple API import {styled} from 'styletron-react'; const Panel = styled('div', (props) => ({ backgroundColor: props.alert ? 'orange' : 'lightblue', fontSize: '12px' })); Danger!

Slide 126

Slide 126 text

airbnb.com CSS output size

Slide 127

Slide 127 text

Use functional CSS

Slide 128

Slide 128 text

Generate functional CSS

Slide 129

Slide 129 text

No content

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

“It’s not real CSS!”

Slide 132

Slide 132 text

No content

Slide 133

Slide 133 text

SASS has variables, mixins, etc.

Slide 134

Slide 134 text

JavaScript has variables, functions

Slide 135

Slide 135 text

https://una.im/sass-es2015/#

Slide 136

Slide 136 text

Variables let color = "red"; $color: "red";

Slide 137

Slide 137 text

Lists const colorArray = ["red", "yellow", "green"]; for (let color of colorArray) { console.log(color); } $colorList: "red", "yellow", "green"; @each $color in $colorList { @debug $color; }

Slide 138

Slide 138 text

Functions function PrintMe(firstArg, ...theRest) { console.log(firstArg, theRest); } @mixin funCircle($size, $gradient ...) { width: $size; height: $size; background: radial-gradient($gradient); }

Slide 139

Slide 139 text

https://polished.js.org/

Slide 140

Slide 140 text

// Styles as object usage const styles = { background: lighten(0.2, '#CCCD64'), background: lighten(0.2, 'rgba(204,205,100,0.7)'), } // styled-components usage const div = styled.div` background: ${lighten(0.2, '#FFCD64')}; background: ${lighten(0.2, 'rgba(204,205,100,0.7)')}; ` // Output element { background: "#e5e6b1"; background: "rgba(229,230,177,0.7)"; }

Slide 141

Slide 141 text

“You should use the Platform”* * Or, just Google Chrome?

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

styled-jsx export default () => (

only this paragraph will be red

{` p { color: red; } `}
)

Slide 148

Slide 148 text

styled-jsx import _JSXStyle from 'styled-jsx/style' export default () => (

only this paragraph will get the style :)

<_JSXStyle styleId='cn2o3j' css={`p[data-jsx=cn2o3j] {color: red;}`} />
)

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

“CSS is simple”

Slide 151

Slide 151 text

CSS is simple

Slide 152

Slide 152 text

CSS is simple CSS is easy ≠

Slide 153

Slide 153 text

https://adactio.com/journal/12571 “It is simple in the sense of “not complex”, but that doesn’t mean it’s easy. Mistaking “simple” for “easy” will only lead to heartache.”

Slide 154

Slide 154 text

How many of you know how to extract critical CSS or doing that? ✋

Slide 155

Slide 155 text

Critical CSS with <> import { renderToString } from 'react-dom/server' import { ServerStyleSheet } from 'styled-components' const sheet = new ServerStyleSheet() const html = renderToString(sheet.collectStyles()) const css = sheet.getStyleTags()

Slide 156

Slide 156 text

https://medium.com/seek-blog/a-unified-styling-language- d0c208de2660 “If you build your app with progressive enhancement in mind, despite being written entirely in JavaScript, it might not require JavaScript on the client at all.”

Slide 157

Slide 157 text

Takeaways

Slide 158

Slide 158 text

No content

Slide 159

Slide 159 text

“CSS-in-JS enforces the best practices trough technology and shared knowledge.”

Slide 160

Slide 160 text

It’s still CSS and you still have to learn it!

Slide 161

Slide 161 text

It’s not about how fast you can write code, it’s about how fast you can read it.

Slide 162

Slide 162 text

Stay open-minded & keep experimenting!

Slide 163

Slide 163 text

No content

Slide 164

Slide 164 text

Thank you!

Slide 165

Slide 165 text

Andrey Okonetchnikov @okonetchnikov http://okonet.ru https://github.com/okonet UI Engineer @ Feedly