Slide 1

Slide 1 text

with @DavidWells davidwells.io/talks/react-css Bulletproof CSS in React

Slide 2

Slide 2 text

UI/UX Engineer at React and Redux Training for companies Next Class Saturday, July 16th in SF register.reactclass.com Tweet at me @DavidWells DavidWells.io

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

ux.mulesoft.com

Slide 5

Slide 5 text

Challenge Make components stand alone Make them work in legacy applications Avoid external collisions and dont collide with others Have styles & assets consumable by others

Slide 6

Slide 6 text

Obligatory Peter Griffin CSS gif

Slide 7

Slide 7 text

GLOBAL PROBLEMS

Slide 8

Slide 8 text

Global CSS Namespace collisions are the greatest threat we face today - Donald Trump

Slide 9

Slide 9 text

HOW DO WE SOLVE THIS?

Slide 10

Slide 10 text

Localized CSS

Slide 11

Slide 11 text

Raw CSS (OOCSS, SMACSS, BEM, SUIT) Inline styles in JSX CSS-in-JS PostCSS + CSS Modules Current styling landscape

Slide 12

Slide 12 text

https://github.com/MicheleBertoli/css-in-js

Slide 13

Slide 13 text

1. Inline Styles

Slide 14

Slide 14 text

Easy to implement Recompute styles at anytime Dynamic state based styles etc import React from 'react' class ShamWOW extends React.Component { render(){ const styles = { color: (this.props.danger) ? 'red' : 'black' fontSize: '15px' } const otherStyles = { color: 'purple' fontSize: '12px' } return (
WOW!
) } }

Slide 15

Slide 15 text

No Pseudo Classes/Elements No Media Queries No vender autoprefixing No Animations/keyframes !important for overrides No Style Fallbacks Composition? Performance (large HTML, larger JS) Debugging is hard INLINE STYLES GOTCHAS

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

2. CSS in JS

Slide 18

Slide 18 text

import React, {Component} from 'react' import {StyleSheet, css} from 'aphrodite' const styles = StyleSheet.create({ container: { textAlign: 'center' }, button: { backgroundColor: '#ff0000', width: '320px', borderRadius: '5px', ':hover': { color: '#fff', }, '@media (max‐width: 480px)': { width: '160px' } } }) export default class Button extends Component { render() { return (
Click me!
); } }

Slide 19

Slide 19 text

Flash of Incorrectly Styled Content* Pseudo Classes/Elements* Media Queries* No Style Fallbacks Animations/keyframes* Debugging is hard* (no sourcemaps) Performance (larger bundle, DOM calcs) library/flavor lock-in Globals? Composable? CSS in JS Considerations

Slide 20

Slide 20 text

3. PostCSS + CSS Modules

Slide 21

Slide 21 text

My Javascript fatique senses are tingling What are these?

Slide 22

Slide 22 text

Local (and global) scope AST is very very powerful Huge plugin ecosystem Write normal CSS and more... Why PostCSS?

Slide 23

Slide 23 text

WHAT DOES THIS LOOK LIKE?

Slide 24

Slide 24 text

Component architecture /src/components/ └── Button/ # Component folder ├── Button.js # src code ├── Button.css # styles ├── Button.spec.js # tests ├── index.js # Exports your component └── examples/ # Usage Examples └── button_usage.js # Examples of component usage

Slide 25

Slide 25 text

Localized Styles via PostCSS + CSS modules /* Button.js React Component */ import React from 'react' import styles from './Button.css' class Button extends React.Component { render(){ const text = this.props.text return (
{text}
) } }

Slide 26

Slide 26 text

CSS as per usual /* Button.css */ .wrapper { font‐size: 20px; } .button { border: 20px solid #bada55; }

Slide 27

Slide 27 text

How CSS modules work /* Button.js */ import React from 'react' /* styles is imported as an object with classnames as keys */ import styles from './Button.css' /* styles = { button: "Button__button___I_bKh" <‐‐‐ Filename__classname__HASH wrapper: "Button__wrapper___3fslE" } */ class Button extends React.Component { render(){ const text = this.props.text return (
{text}
) } }

Slide 28

Slide 28 text

The end result
My button Text
VS
My button Text

Slide 29

Slide 29 text

Other benefits Use CSS4 Polyfill Flexbox back to IE8 a11y rules linting?!?!! intial: all; Composability Critical CSS paths Separate out all mediaQueries (bit.ly/responsiblejs) Perf gains dev linting via styleLint Designers can write* *gasp* ability to use Global

Slide 30

Slide 30 text

IS GLOBAL CSS ALL BAD? NOPE! modal, drawer, etc. classes on body Sharing keyframe animations External selector hooks (double edged sword) Sometimes you want the cascade

Slide 31

Slide 31 text

.local‐class { color: red; } :global(.prefix‐modal‐open) .local‐class { color: green; } /* or */ :global { .this‐is‐global { color: yellow; } .potential‐collision‐city { color: crap; } } You decide Local by default, global when you want

Slide 32

Slide 32 text

DO I NEED THIS? Are you working on a team? Are you including third party CSS? Markup running inside of a third party environment? Do you want raw AST power? Want your styles to look correct no matter what?

Slide 33

Slide 33 text

Whats the real power here? AST

Slide 34

Slide 34 text

FIX CSS FOREVER WOOOOOOOOOOOOO!!!!!

Slide 35

Slide 35 text

CAVEATS No react native* Requires build step (but what doesn't these days) No "dynamic" computed styles based on state* Be careful of inventing your own syntax Don't go nuts with plugins Testing quirks if asserting on localized classnames You have to write CSS*

Slide 36

Slide 36 text

WHICH APPROACH WINS? ALL OF THEM!

Slide 37

Slide 37 text

Our choice PostCSS + inlineStyles PostCSS handles majority of styling When computed values are needed inlineStyles on the `style`

Slide 38

Slide 38 text

powered by JS! VARIABLES AND MIXINS

Slide 39

Slide 39 text

Javascript powered variables 16. /* require global variables */ 17. require('postcss-simple-vars')({ 18. variables: function () { 19. return require('./variables') 20. }, 21. unknown: function (node, name, result) { 22. node.warn(result, 'Unknown variable ' + name) 23. } 10. /* enable mixins like Sass/Less */ 11. require('postcss-mixins')({ 12. mixins: require('./mixins') 13. }), 14. /* enable nested css selectors like Sass/Less */ 15. require('postcss-nested'), 24. }), 25. /* PostCSS plugin for making calculations with math.js */ 26. require('postcss-math'), 27. /* transform W3C CSS color function to more compatible CSS. */ 28. require('postcss-color-function') 29. ]

Slide 40

Slide 40 text

Mixin & variable usage in CSS .hero { @mixin DinProLight; /* defined in mixins.js */ color: $primaryBlue; /* defined in variables.js */ }

Slide 41

Slide 41 text

Variable usage in JS import React from 'react' import variables from 'path/to/global‐variables' class Button extends React.Component { render(){ const styles = { color: variables.primary zIndex: variables.aboveModal } return (
WOW!
) } }

Slide 42

Slide 42 text

Best of both worlds PostCSS (AST JS infused CSS) and CSS in JS when needed

Slide 43

Slide 43 text

Tips & Tricks

Slide 44

Slide 44 text

Build for production localIdentName: [hash:base64:4] module: { loaders: [ { test: /.css/, loaders: ['style‐loader', 'css‐loader?modules&localIdentName=[hash:base64:4]!postcss‐loader'], } ] }, Output:
My button Text

Slide 45

Slide 45 text

BEM FO FREE localIdentName: [name]__[local] module: { loaders: [ { test: /.css/, loaders: ['style‐loader', 'css‐loader?modules&localIdentName=[name]__[local]!postcss‐loader'], } ] }, Output:

Slide 46

Slide 46 text

npm i classnames via @jedWatson import styles from './Button.css' import classnames from 'classnames' ... const { className, isGhost, disabled, isLoading } = this.props const propBasedClasses = { [styles.ghost]: isGhost, /* if isGhost === true */ [styles.disabled]: disabled || isLoading } const classes = classNames( styles.button, /* localized styles */ className, /* user specified classNames */ propBasedClasses /* prop based classnames */ )

Slide 47

Slide 47 text

http://postcss.parts via @mxstbr

Slide 48

Slide 48 text

catch undefined via @Chrisui .undefined { display: flex !important; position: fixed !important; top: 0 !important; right: 0 !important; bottom: 0 !important; left: 0 !important; background: red !important; color: white !important; justify‐content: center !important; align‐items: center !important; font‐size: 30px !important; font‐weight: bold !important; } .undefined::after { display: block !important; padding: 15px 30px !important; content: 'ERROR! You are missing a class definition in your css module! Inspect me to find out where.' !important }

Slide 49

Slide 49 text

http://cssnext.io via @MoOx

Slide 50

Slide 50 text

Questions? React and Redux Training Next Class Saturday, July 16th in SF React Basics through Redux and Serverside Rendering register.reactclass.com Tweet at me @DavidWells DavidWells.io

Slide 51

Slide 51 text

Phenomic presentation bit.ly/phenomic-sites

Slide 52

Slide 52 text

Questions? React and Redux Training Next Class Saturday, July 16th in SF React Basics through Redux and Serverside Rendering register.reactclass.com Tweet at me @DavidWells DavidWells.io

Slide 53

Slide 53 text

Seriously no Questions?

Slide 54

Slide 54 text

No content