CSS-in-JS - Andrii Los (20.10.2017)

Post on 21-Jan-2018

382 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

Transcript

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

top related