That said, it seems the winds have shifted a bit and more people are eschewing inline styles in favor of CSS-in-JS solutions such as styled-components or emotion. I've tinkered with both of these libraries a bit, and I have to say I can't see how these are an improvement over plain inline styles. They introduce additional build steps, the use of string literals instead of plain JavaScript objects, and they mess with the core React.createElement function. The upside is you get to use pseudo-selectors, which is nice, but I've found with a real programming language at my disposal (i.e. JavaScript), I don't really need many pseudo-selectors beyond :hover.
What am I missing?
At the same time people who understand one technology may not want to learn new ideas and techniques. If it aint broke don't fix it.
The point of me writing these two conflicting ideas is inject some skeptisism into the conversation. Maybe you aren't missing anything and the tool is overhyped and you don't fully understand the benefits. Both of these things can be true.
It's hard to beat the high-level declarative nature of writing style rules in a stylesheet that can style any HTML or XML DOM you use them with. I think a lot of current CSS-in-JS tooling ends up being too limited in that it often requires you to know the DOM in advance and couples the styles tightly with the DOM - while also leaving you unable to style anything you run across or need to work with that was created outside your particular workflow or pre-exists outside of your app.
The future looks like CSS, augmented by Layout worklets written in JS, and augmented by Paint worklets written in JS, and Animation worklets written in JS, and Custom Property definitions that can be supported by JS. And we believe custom script-supported at-rules, functions, and selectors are next to come to CSS.
The long and sort of it - you should be writing CSS in CSS stylesheets, and JavaScript in your JavaScript files, and find ways to use Javascript to define the custom features you want to use in your stylesheets. This is how the technology is growing, and when you extend real CSS with real JS, you can style anything, any DOM made by any tools.
More reading:
- Enforce only local styles
- Developer productivity
- Better theming support
- Better support for dynamic styles
- Easier to consume packages
- Typesafe styles
Cons: - Worse runtime performance
- Bigger bundle size
- Fragmentation of ecosystem
- Idiosyncratic additions to css
It depend on your project. For small and medium sized projects I would recommend CSS-in-js. For large and performance-sensitive css-modules would be better.
I do think it's possible to have too much of a good thing, but for the most part I've seen developers ill-define a CSS API (with no easy way to override) which then becomes messy with !important all over the place. I do wish CSS blocks from LinkedIn (https://css-blocks.com/) had better support and adoption because I thought it looked promising. I may even try it on a project soon since my new employer doesn't have an onerous on what I do during my free time
Also, I think you should not use the HTML "style" attribute; it is bad for accessibility. Use the "class" attribute instead.