Slide 1

Slide 1 text

styled-jsx

Slide 2

Slide 2 text

CSS in JS library for React inspired by ShadowDOM

Slide 3

Slide 3 text

zeit/styled-jsx

Slide 4

Slide 4 text

Guillermo Rauch Naoyuki Kanezawa Giuseppe Gurgone

Slide 5

Slide 5 text

Guillermo Rauch Naoyuki Kanezawa Giuseppe Gurgone

Slide 6

Slide 6 text

styled-jsx

Slide 7

Slide 7 text

CSS in HTML

Slide 8

Slide 8 text

.container { background: black } h1 { color: white }

Slide 9

Slide 9 text

CSS in JSX

Slide 10

Slide 10 text

{` .container { background: black } h1 { color: white } `}

Slide 11

Slide 11 text

{` .container { background: black } h1 { color: white } `}

Slide 12

Slide 12 text

CSS in styled-jsx

Slide 13

Slide 13 text

{` .container { background: black } h1 { color: white } `}

Slide 14

Slide 14 text

{` .container { background: black } h1 { color: white } `}

Slide 15

Slide 15 text

Rendered inside a JSX element

Slide 16

Slide 16 text

Zeit rocks

{` .container { background: black } h1 { color: white } `}

Slide 17

Slide 17 text

Zeit rocks

{` .container { background: black } h1 { color: white } `}

Slide 18

Slide 18 text

Styles encapsulation Controlled Cascade Full CSS support

Slide 19

Slide 19 text

Simplicity and DX

Slide 20

Slide 20 text

Zeit rocks

{` .container { background: black } h1 { color: white } `}

Slide 21

Slide 21 text

Zero learning curve

Slide 22

Slide 22 text

How?

Slide 23

Slide 23 text

Rewrite JSX with a Babel plugin

Slide 24

Slide 24 text

npm i --save styled-jsx

Slide 25

Slide 25 text

{ “plugins“: [ “styled-jsx/babel“ ] }

Slide 26

Slide 26 text

Zeit rocks

{` .container { background: black } h1 { color: white } `}

Slide 27

Slide 27 text

becomes

Slide 28

Slide 28 text

import _JSXStyle from ’styled-jsx/style’

Zeit rocks

<_JSXStyle styleId={’123’} css={` .container.jsx-123 { background: black } h1.jsx-123 { color: white } `} />

Slide 29

Slide 29 text

import _JSXStyle from ’styled-jsx/style’

Zeit rocks

<_JSXStyle styleId={’123’} css={` .container.jsx-123 { background: black } h1.jsx-123 { color: white } `} />

Slide 30

Slide 30 text

Compile time transformation, Babel and Stylis Server Side Rendering out of the box High-performace CSS injection at runtime Critical CSS and style tags deduping

Slide 31

Slide 31 text

Built-in CSS vendor prefixing Source maps Prettier

Slide 32

Slide 32 text

just by adding a single jsx attribute

Slide 33

Slide 33 text

{` .container { background: black } h1 { color: white } `}

Slide 34

Slide 34 text

Simplicity

Slide 35

Slide 35 text

Advanced features

Slide 36

Slide 36 text

Global styles

Slide 37

Slide 37 text

{` .container { background: black } h1 { color: white } `}

Slide 38

Slide 38 text

One-off global selectors with :global()

Slide 39

Slide 39 text

.container > :global(h1)

Slide 40

Slide 40 text

.container.jsx-123 > h1

Slide 41

Slide 41 text

Dynamic styles and themes

Slide 42

Slide 42 text

(props) =>

{props.children} {` h1 { color: ${props.color} } `}

Slide 43

Slide 43 text

External CSS.js modules

Slide 44

Slide 44 text

import css from ‘styled-jsx/css’ export default css` h1 { color: white } `

Slide 45

Slide 45 text

import styles from ‘./styles’ {styles}

Slide 46

Slide 46 text

import styles from ‘./styles’ {styles}

Slide 47

Slide 47 text

css works in the same file

Slide 48

Slide 48 text

const styles = css` h1 { color: white } ` {styles}

Slide 49

Slide 49 text

CSS preprocessing via plugins

Slide 50

Slide 50 text

(css: string, settings: Object) => string

Slide 51

Slide 51 text

Trade-offs

Slide 52

Slide 52 text

Strings not as powerful as Object Literals

Slide 53

Slide 53 text

Interpolations not preprocessed or scoped

Slide 54

Slide 54 text

Styling Shadow DOM from the outside world is tricky

Slide 55

Slide 55 text

:global() makes it easier but it is not perfect

Slide 56

Slide 56 text

const scoped = resolveScopedStyles( {` .link { font-weight: bold } `} ) export default () =>

Zeit rocks

Slide 57

Slide 57 text

const scoped = resolveScopedStyles( {` .link { font-weight: bold } `} ) export default () =>

Zeit rocks

Slide 58

Slide 58 text

function resolveScopedStyles(scope) { return { className: scope.props.className, Styles: () => scope.props.children, classes: (className) => scope.props.className + ‘ ‘ + className } } // © Anton Ignatov

Slide 59

Slide 59 text

Function resolveScopedStyles(scope) { return { className: scope.props.className, Styles: () => scope.props.children, classes: (className) => scope.props.className + ‘ ‘ + className } } // © Anton Ignatov

Slide 60

Slide 60 text

Zeit {` .link { font-weight: bold } } `}

Slide 61

Slide 61 text

Styling third parties components

Slide 62

Slide 62 text

styled-jsx/css produces scoped and global CSS for now

Slide 63

Slide 63 text

Recap

Slide 64

Slide 64 text

Add a jsx attribute to a style element

Slide 65

Slide 65 text

Rendered inside a JSX element

Slide 66

Slide 66 text

Profit

Slide 67

Slide 67 text

Simplicity

Slide 68

Slide 68 text

It is just CSS

Slide 69

Slide 69 text

encapsulated

Slide 70

Slide 70 text

“if the poo is trapped in a box then it only smells when you open it.” – @danwrong, on software modularization.

Slide 71

Slide 71 text

@giuseppegurgone

Slide 72

Slide 72 text