Download - CSS-in-JS - Andrii Los (20.10.2017)
I'm Andrii Los,
Frontend Engineer at ITV Project
π
GitHub: RIP21 Twitter: @RIP212
πCSS-in-JS π Styling for component era web
π© Problems of CSS
π€ How they were addressed
π CSS-in-JS features
π€βοΈ CSS-in-JS F.A.Q and its problems π©
π Future of CSS-in-JS
Problems of CSS π©
π© Designed for documents not apps
π© Globally scoped
π© Hardly reusable
π© Leaking
π© Non-programmatic
π© Non-modular
π© CSS develops too slow
How they were addressed π€
π€ LESS
π€ PostCSS
π€ SASS
π€ Stylus
π€ CSS Modules
π€ All above with Webpack loaders and plugins
What problems were
preserved? π©
π© Non-modular out of the box
π© Still not componentized enough
π© Require a lot of class names manual work
π© Still global and can leak
π© Reusability still not perfect
So what if we would like to
fix them all in one ultimate
solution? π€
π
Glorious
CSS-in-JS!
π
CSS-in-JS
π styled-components
π©π€ emotion
π glamorous
CSS rules definition
π Template string literals
π Object React inline-styles notation
π©π€ Supports both
So what the possibilities?
π Media queries
π Keyframes
π Pseudo classes
π Nested selectors
π It's JS, so you have all its power
π Babel and Webpack optimisations for production
π Total isolation if old global CSS approach is not used
π Easy theming support
So what the possibilities?
π Full interoperability with existent CSS
π Inject global styles if you know what you are doing
π Full support for styling 3rd parties components
π Extend API for easily reuse styles
π react-primitives - React Native, React Sketch, etc.
π Endless of other powerful things that you can
come up with
Main features demo π€
What's the problems? π©
π© Weak editors support?! It's just strings and objects!
π Nope! Webstorm, VS Code, Sublime Text, Atom support is there!
π© Formatting! I sure it's just highlights the text, but no formatting!
π Nope! Webstorm formats it perfectly. (Not sure about others though)
π© Meh! Then I won't use!
π Editors doesn't matter. Prettier is formatting CSS in template literals ;)
π© But we don't use it!
π€βοΈ What's wrong with you?! It's amazing!
π© I'm not impressed yet..... I need something. It's stupid to have CSS in JS!
π€·βοΈ Okay, but what did you were saying about JSX a few years ago π€
π© PERFORMANCE!
Well, you are a little correct π©
π© styled-components performance in unrealistic
benchmarks is very weak
π Good news. Emotion and glamorous have bench
performance only 5-10% slower than CSS Modules
π Real world apps performance is good for all
solutions
π There is a hacks to improve it even further if so
needed
So, what you would
recommend?styled-components π
Why?
π 11000 stars on GitHub, means lots of contributions
π Supports React Native and other renderers
π Amazing Jest and other test runners support
π A lot of tooling is already done for it
π Stylelint support (works for emotion as well)
π The best documentation
π Better performance - matter of time
Future of CSS-in-JS
π€ ISTF - Interoperable Style Transfer
Format (Even further performance
improvement, CSS-in-JS styles interop)
π€· Extreme styles optimisations to reduce
amount of CSS code up to 30-60%
(depends on codebase size)
That's all folks! πGitHub: RIP21 Twitter: @RIP212
List of links and resources π
β’ Sandbox for this speech
β’ A unified styled language @markdalgleish - MUST READ (pick on
CSS-in-JS by co-author of CSS Modules)
β’ styled-components documentation
β’ emotion documentation and full of perf demos of emotion in its
authors twitter
β’ glamorous documentation
β’ Amazing article about styled-components future and CSS-in-JS in
overall by one of the authors of s-c
β’ Template string literals or Object notations, what to choose? Read
that