CSS Is an Interface
ericwbailey.design
ericwbailey.design
But, it makes sense when you realize why the solution is a fuck-it solution. People are not great at CSS, got tired of it, and said fuck-it. And voila, now we have a fuck-it solution.
It’s one thing to utilize the convenience of css-in-js as needed, but we have, as usual, gone to the extreme and just wholesale ditched css lol.
CSS-in-JS is similar to React abstracting away HTML, as well as solving global issues etc.
React Native Web abstracts both the view and style layer, you work with a RN stylesheet based interface that generates atomic css on web and RN stylesheets on native. Similar to how React generates HTML on web and native controls on other platforms.
Personally CSS is too bloated, I love working with a subset that includes flexbox and the core styling properties.
Either, it depends on the library. It's becoming more popular to extract into regular css files as opposed to style tags. This is usually referred to as 'zero runtime css-in-js' as it doesn't depend on any JS for styles to load but you still get the composability and benefits of writing it in js.
> you scope down to such a granular level that the style literally applies to that one element.
It applies to that component (and any instance of that component), css-in-js also supports all standard css composition so it's very easy to do something like target all spans tags inside FancyButtons, you don't only have to target components.
At the end of the day it's a superset of css, all normal css is valid so no css has been ditched. This has come about because css requires you to manually keep styles and components in sync and css-in-js couples them together in a way that removes that management while also making it impossible to accidentally cause conflicts e.g. multiple components defining their own Container will never touch each others css unless you explitly import that definition and use it.
I have nothing against regular css and I'm a big fan of SCSS and CSS modules but css-in-js has a lot of advantages.
Sure, there are certainly people who exclusively used CSS in JS, but I would hardly say that they are even a majority of frontend devs.
CSS-in-JS is also able to compile to non-inline styles in some situations if I’m not mistaken, but I do agree this is something of an anti pattern
For my last project I went with CSS in JS. It has some shortcomings and I don't think it would work very well if I use a lot of components from NPM, but it was really nice at encapsulating the component to a single file.
I don't think either approach is more or less proper. Inline styles had obvious large shortcomings in 2010, but not so much in the current context of front-end development.
Most, not all but most, CSS-in-JS is written as plain CSS or a Sass-like superset, in a tagged template literal. Where it’s not that, it’s typically written as the equivalent but with camelCase in a POJO. Effectively the same thing with minor syntax differences. In a few other cases it’s written in literal style tags.
In nearly all cases, the styles are not inlined at all. They produce one or more classes which can be applied to an element.
None of that is a fuck-it solution. You might not agree with the benefits, but to people who prefer it those are:
- Colocation of styles and usage, which aids understanding how or whether styles are applied (and could help correct cases like the example early in this article)
- Composition with clear semantics
- Language and tooling integration of various levels beyond referencing arbitrary CSS
- Build-time optimizations that allow extraction or inlining or code splitting, as appropriate for the project
I have no ill will toward Tailwind or any other “atomic CSS” solution, but I suspect more of your points apply to that particular approach than anything to do with CSS-in-JS. (Except Fela, which I quite like for some use cases, but by default has output similar to the complaints raised by the article.)
Usually just blogs and stuff, though, trying to mess with a React or Angular app is usually futile.
E.g. this snippet mostly fixes my system's abysmal default typography on sites like https://danluu.com :
p {
font-family: Charter;
font-size: 16pt;
line-height: 1.4;
max-width: 36em;
margin: 2em auto;
}It's the 夕 kanji, which reads as either "せき" or "ゆう", it's not the タ katakana.