Slide 1

Slide 1 text

CSS in JS The Good & Bad Parts ReactiveConf 2017

Slide 2

Slide 2 text

Robin Frischmann @rofrischmann @rofrischmann

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

rofrischmann/fela

Slide 5

Slide 5 text

Who is already familiar with CSS in JS ?

Slide 6

Slide 6 text

CSS in JS
 101

Slide 7

Slide 7 text

CSS in JS !!!== inline style

Slide 8

Slide 8 text

const style = { fontSize: 12, color: 'red', ':hover': { color: 'blue' }, '@media (min-width:320px)' { fontSize: 12 } } const style = ` font-size: 12px; color: red; &:hover {
 color: blue; } @media (min-width:320px) { font-size: 14px; } } ` Objects Template strings

Slide 9

Slide 9 text

The Problems

Slide 10

Slide 10 text

Choosing the “right” library 1

Slide 11

Slide 11 text

• aphrodite • babel-plugin-css- in-js • babel-plugin-pre- style • bloody-react-styled • classy • csjs • css-constructor • css-light • css-loader • css-ns • cssobj • cssx-loader • emotion • es-css-modules • glamor • glamorous • hyperstyles • i-css • j2c • jsxstyle • linaria • pre-style • radium • react-css-builder • react-css- components • react-css-modules • react-cssom • react-cxs • react-fela • react-free-style • react-inline-css • react-inline-style • react-inline • react-jss • react-look • react-native-web • react-statics-styles • react-styl • react-style • react-styleable • react-stylematic • react-theme • react-vstyle • reactcss • restyles • scope-styles • smart-css • stile-react-media- queries • stilr • style-it • styled-components • styled-jsx • styletron-react • styling • superstyle • typestyle • uranium

Slide 12

Slide 12 text

Use what works for you!

Slide 13

Slide 13 text

1. Objects or Template Strings? 2. Working with Designers? 3. Simplicity or Flexibility? 4. React-centric or Framework-agnostic? 5. Server-side Rendering?

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Not everyone is used to JavaScript 2

Slide 16

Slide 16 text

1. Choosing a library is hard 2. Using CSS in JS does not always turn out to be a good choice at all

Slide 17

Slide 17 text

The Benefits

Slide 18

Slide 18 text

It’s JavaScript! 1

Slide 19

Slide 19 text

Who has used or is using a preprocessor like Sass, Less or Stylus? ?

Slide 20

Slide 20 text

JavaScript everywhere!

Slide 21

Slide 21 text

$primary-color = red .button { color: $primary-color; padding: 15px 20px; font-size: 20px; } const primaryColor = 'red' const buttonStyle = { color: primaryColor, padding: '15px 20px', fontSize: 20 } (S)CSS JS

Slide 22

Slide 22 text

$vendors: "-webkit-", "-ms-", ""; @mixin prefix($property, $value) { @each $vendor in $vendors { !#{$vendor}!#{$property}: !#{$value}; } } .button { @include prefix( 'border-radius', '5px' ) } const vendors = ['-webkit-', '-ms-', ''] function prefix(property, value) { return vendors.reduce( (style, vendor) !=> { style[vendor + property] = value return style }, {} ) } const buttonStyle = { !!...prefix( 'border-radius', '5px' ) } SCSS JS

Slide 23

Slide 23 text

Ecosystem

Slide 24

Slide 24 text

styled-components/polished

Slide 25

Slide 25 text

Computers do the hard work 2

Slide 26

Slide 26 text

– Phil Karlton “There are only two hard things in Computer Science: cache invalidation and naming things.”

Slide 27

Slide 27 text

Naming CSS classes is hard

Slide 28

Slide 28 text

BEM smacss.com oocss.org getbem.com OOCSS SMACSS

Slide 29

Slide 29 text

Conventions !!!== enforced

Slide 30

Slide 30 text

Computers for the rescue!

Slide 31

Slide 31 text

Class Names?
 Non of my business!

Slide 32

Slide 32 text

– Alan B Smith “[…] the appeal of CSS-in-JS is not simplicity, rather predictability and consistency.” Source: https://hackernoon.com/why-we-use-styled-components-at-decisiv-a8ac6e1507ac

Slide 33

Slide 33 text

Predictable Styling 3

Slide 34

Slide 34 text

Who has ever accidentally destroyed the layout or styles of some part of the application, while actually working on something completely different? ?

Slide 35

Slide 35 text

const view = () !=> (
Some Text !
) .text_green { color: green } JS(X) CSS

Slide 36

Slide 36 text

const view = state !=> (
Some Text !
) .text_red { color: red } .text_green { color: green } JS(X) CSS

Slide 37

Slide 37 text

const view = state !=> (
Some Text !
) .text_red { font-size: 20px; color: yellow; } .text_green { color: green } JS(X) CSS

Slide 38

Slide 38 text

UI = f(state)

Slide 39

Slide 39 text

#1 What is displayed #2 How is it displayed

Slide 40

Slide 40 text

How What Markup Styling HTML CSS

Slide 41

Slide 41 text

UI = f(state)

Slide 42

Slide 42 text

Style = f(state)

Slide 43

Slide 43 text

import { css } from 'any-css-in-js-library' const style = state !=> ({ color: state.error ? 'red' : 'green' }) const view = state !=> (
Simple Text !
) JS(X)

Slide 44

Slide 44 text

- Choosing a library is hard - Using CSS in JS is not necessarily the best choice ✓ Power of JavaScript ✓ Unique generated classes ✓ Predictable UI Benefits Problems

Slide 45

Slide 45 text

@rofrischmann @rofrischmann

Slide 46

Slide 46 text

Thank you! @rofrischmann @rofrischmann