r/Frontend • u/ms-maria-ma • Mar 07 '19
Why I Write CSS in JavaScript
https://mxstbr.com/thoughts/css-in-js/11
u/FlorinPop17 Mar 07 '19
I’ve been using Styled Components for a while now and I am happy with how they work. I also use css files on smaller projects, so I’m comfortable with both! ☺️
1
Mar 20 '19
I was always against mixing js and css. ITCSS + BEM solves the problem (if you know how to use them properly)
1
1
u/kowdermesiter Mar 07 '19
What's the killer feature that SASS doesn't have?
2
u/TheAesir 12 YOE Mar 07 '19
Among others
all the benefits of css modules
being able to write conditionals in JS
significantly easier to support multiple themes
1
u/kowdermesiter Mar 08 '19
All of these are supported by SASS.
1
u/TheAesir 12 YOE Mar 08 '19
Kind of, but not with the simplicity that you get with using JS to handle it. When the discussions at work came up initially about CSS in JS, they centered around theming. We support multiple brands, across our platform, and while it was possible to handle this using SASS modules, it wasn't clean a solution. There was also the issue of having to maintain JS constants files that matched the themes, in case we needed the values for whatever reason. Maintenance has been infinitely better since transitioning
2
u/colnarco Mar 08 '19
The ability to evaluate statements at runtime. Sass needs to be compiled ahead of time and you therefore can’t alter your sass based on the state of your JavaScript.
1
u/kowdermesiter Mar 08 '19
And this is useful how?
2
u/colnarco Mar 08 '19
You can react to state-changes directly in your css, instead of having to toggle classes and you can have theming that you don’t know about at compile-time (this is getting there with css-variables too, but that’s not something sass has to offer). But other than that there isn’t any killer features - you get sass + a little extra for a insignificant performance penalty.
For me personally I really like the Vue-approach of having your css written in the same file as your component. It completely eliminates the fear of removing css, scoping issues and specificity problems. Its not for everyone though.
1
u/kowdermesiter Mar 08 '19
Thanks for the elaboration. For something animation heavy I see the benefits where there are lots of animated and updated CSS rules. For standard admin style UI stuff, not so much.
1
u/fennekinyx Mar 07 '19
I’ve only just started using styled components myself to see what the fuss is all about, but automatic multi browser prefixes is pretty nice.
1
17
u/Rhym Mar 07 '19
I see the benefits of using css in js, but after using BEM for years I don't have the issues that people usually run into and don't see myself changing for a while.