News for hackers. Stuff that matters.
Re:Real-world CSS vs. CSS-in-JS performance comparison by MatekCopatek on Tuesday June 08, 2021 @03:46PM (#27439878)
Can someone explain the appeal of CSS-in-JS? I've used scoped styling in .vue components extensively, at a quick glance it seems to offer similar benefits (lives right in your component and is scoped to it), but most importantly it can be extracted to a plain CSS file when building frontend assets.

Is there something extra these React libraries do that prevents them from doing this?

Re:Real-world CSS vs. CSS-in-JS performance comparison by mekkkkkk on Tuesday June 08, 2021 @06:02PM (#27441560)
I've worked on and off with React since its inception, and the main reason I would use it is simple ergonomics. If you are working with an MVP or a small widget project, you gain a lot of velocity. You don't need to worry about scoping, naming collisions, class toggles, etc. and as long as you use decently small components, it all fits snuggly in the same file as the component definition.

The price you pay comes in the way of performance and maintainability. Since the line becomes very blurry between styling and markup, it's up to the dev(s) to bring order to potential chaos.

For any bigger project, my go to solution is SCSS modules. It's by far the most versatile solution I've tried if you want to roll your own CSS and not rely on a CSS framework. You don't need to worry about scoping, the bundle process is excellent, you have access to all of your normal SCSS shenanigans and most important it's a solution that makes it dead easy to collect or break down parts into logical units.

Re:Real-world CSS vs. CSS-in-JS performance comparison by Kiro on Wednesday June 09, 2021 @02:29AM (#27444879)
I've found it easier to maintain CSS-in-JS since I know exactly what will be affected when I change it. I'm always scared to touch the .css files on large projects.

I haven't used SCSS modules though.

Re:Real-world CSS vs. CSS-in-JS performance comparison by mekkkkkk on Wednesday June 09, 2021 @10:32AM (#27448326)
With my preferred SCSS module setup you get a hybrid solution with the best of both worlds. You can keep a clean stylesheet next to your component and then just:

    import { mainGraphic } from './component.scss';

    [...]

    return (
        <figure className={mainGraphic} />
    );
Where `mainGraphic` is a regular class block in the SCSS. So it's pretty straight forward and the mapping is still fairly tight. At bundle time, the class name is minimized, deduped and a hash is added to it that corresponds to the rule content. The generated standalone CSS file also gets hashed for cache busting purposes. It's pretty neat.

And since it's SCSS you can keep constants, mixins, functions, etc. imported and organized within and between SCSS files, separated from the components.

Re:Real-world CSS vs. CSS-in-JS performance comparison by jfengel on Tuesday June 08, 2021 @04:22PM (#27440257)
I'm afraid I don't get it either. To me, the big disadvantage of css-in-js is that the browser doesn't display it neatly.

If I write plain CSS, it's easy to make changes right in the browser and have it react instantly. Or I can make them in the .css file, and webpack (or whatever it is) will automatically redisplay them without having to restart the app.

Perhaps if I had much larger projects I'd see more headaches that drives people to css-in-js. But I manage fairly large things and don't have too much grief with plain css.

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Tuesday June 08, 2021 @06:00PM (#27441523)
Imagine you need to implement an alternative stylesheet for people with increased visual needs (e.g. high contrast).

How would you go about doing this?

--

Edit: to the person who downvoted me for asking a question. Classy.

Re:Real-world CSS vs. CSS-in-JS performance comparison by masswerk on Tuesday June 08, 2021 @08:31PM (#27442843)
This is, what `<link rel="alternate stylesheet" href="contrast.css" title="High contrast">`is for. (Using JS, you can switch between default and alternative stylesheets by setting their "disabled" attribute accordingly.) This mechanism also allows a user to (pre)select a stylesheet in the browser.

Sadly, Chrome doesn't support this standard (it has been around for a while and is in HTML4, HTML Living Standard, CSSOM). But you can work around this by JS, see above.

(Edit: It's for things like this that you still want a menu bar for your browser.)

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Wednesday June 09, 2021 @04:24AM (#27445536)
True, but I'm wondering how the CSS in JS users would implement this.
Re:Real-world CSS vs. CSS-in-JS performance comparison by betageek on Wednesday June 09, 2021 @11:02AM (#27448705)
CSS-in-JS alone doesn't give you many tools for that kind of stuff, but if you use something that integrates more design system-style theming it's pretty easy - have a look at https://chakra-ui.com/ and click on the moon icon in the top corner for instant dark mode. Implementing various themes for high-contrast etc. would just be a case of changing the theme.
Re:Real-world CSS vs. CSS-in-JS performance comparison by notoverthere on Tuesday June 08, 2021 @06:09PM (#27441657)
Switch the `<link href="">` value to point to a high contrast stylesheet.

Or alternatively, apply a class to the body tag – e.g. `<body class="high-contrast">` – and declare CSS rules accordingly. Specificity should take care of overriding the 'normal' style rules where needed.

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Wednesday June 09, 2021 @04:25AM (#27445539)
Having to override styles that have random namespace tokens in the class names (via CSS in JS) is going to be difficult.
Re:Real-world CSS vs. CSS-in-JS performance comparison by easrng on Tuesday June 08, 2021 @06:12PM (#27441698)
I'd use CSS variables for colors and write another stylesheet, using the cascade so I only override the styles I need to.
Re:Real-world CSS vs. CSS-in-JS performance comparison by acdha on Tuesday June 08, 2021 @08:38PM (#27442893)
You can specify alternative style sheets and even get some standard browser UI:

https://developer.mozilla.org/en-US/docs/Web/CSS/Alternative...

I’ve implemented that two ways: having JavaScript pop the alternative attribute off for all but the desired style sheet or simply using a top-level class to modify the relatively few things we were customizing. Back in the day, I used SCSS but CSS Variables have matured to the point where you can do an awful lot that way, too.

Re:Real-world CSS vs. CSS-in-JS performance comparison by Supermancho on Tuesday June 08, 2021 @08:18PM (#27442762)
HN randomly mods posts. Don't assume malice.
Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Wednesday June 09, 2021 @04:23AM (#27445529)
Could you please explain what you mean?
Re:Real-world CSS vs. CSS-in-JS performance comparison by jfengel on Wednesday June 09, 2021 @01:11PM (#27450454)
I think you should assume malice... but reconcile yourself to the fact that there's not much you can do about it. People engage in petty acts of malice on social media because they can get away with it.

The best you can hope for is to forget about it. It's unsatisfying, but less unsatisfying than trying to get back at them or apply "sour grapes" or whatever. None of that works.

The sooner you put your brain on something else, the sooner you'll forget that somebody was a dick to you and got away with it. It sucks, but it's the least worst alternative. Treat it as noise -- which I think is what the OP was trying to say.

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Thursday June 10, 2021 @07:14AM (#27459341)
I agree re. the assumption; and also the resolution.

I also think it's worth calling it out too though.

Re:Real-world CSS vs. CSS-in-JS performance comparison by Supermancho on Thursday June 10, 2021 @09:05AM (#27460219)
New posts get down modded randomly for whatever reason (iirc it was to disrupt some bot karma farming). Wait a day and the post will almost always be back to to the default (1) + any user mods.
Re:Real-world CSS vs. CSS-in-JS performance comparison by codegrappler on Tuesday June 08, 2021 @08:57PM (#27443002)
For us the big win is branded sites. We have a white labeled product that is customizable for each client. No code deploys needed to add a new client. Values live in a db table and get pulled in dynamically based on the url subdomain.
Re:Real-world CSS vs. CSS-in-JS performance comparison by enumjorge on Tuesday June 08, 2021 @09:34PM (#27443246)
Honestly curious, couldn’t you achieve something similar with style sheets, where you determine what .css file to serve based on the subdomain?
Re:Real-world CSS vs. CSS-in-JS performance comparison by girvo on Tuesday June 08, 2021 @10:45PM (#27443668)
Yes, and using CSS Custom Properties/the var() function you can even keep that branding file as small as said table. Could even build it dynamically, if one wanted.
Re:Real-world CSS vs. CSS-in-JS performance comparison by rajangdavis on Tuesday June 08, 2021 @04:59PM (#27440716)
If you want to have a range of applicable values for some CSS property or state-based styling, CSS-in-JS can be a good tool for expressing that as opposed to writing JS that updates some styling declaratively.

For example, I've used it on a personal app to allow adjusting the placement and opacity of controls. I prefer the CSS-in-JS approach because the code is simple to understand, I can reuse the code in multiple areas, and I can pass state to some function to generate the styling. I also imagine that I could also write some spec to support it, but it's not something that I plan on quite yet.

It's not a technique that I would use for ALL styling, but I think for the use-case I mentioned above, it works great.

Re:Real-world CSS vs. CSS-in-JS performance comparison by runarberg on Tuesday June 08, 2021 @05:55PM (#27441466)
There are number of techniques that can fit that. State based styling can be achieved with simple class name toggles or CSS animation + the `animationend` event. If you don’t like toggling classes with JS there are number of selectors (including the attribute selector) which you can use instead.

For making code easier to understand you can set a CSS custom property instead of specific style values, and the CSS will be really straight forward.

Re:Real-world CSS vs. CSS-in-JS performance comparison by rajangdavis on Tuesday June 08, 2021 @06:12PM (#27441690)
To be clear - when I need to toggle a basic state with a class or set of classes, that is the strategy I use.

Where that technique fails is when you have hundreds of possible values that can occupy a given state.

If I want to set the rotation or opacity for a control and I want to use a range of values, there is nothing native in CSS to allow me to do that.

You can use SCSS to dynamically generate the CSS class declarations for every possible value, but I would only ever use that solution if I needed the performance boost and that was the only way to get it.

Re:Real-world CSS vs. CSS-in-JS performance comparison by runarberg on Tuesday June 08, 2021 @06:20PM (#27441800)
I see what you mean. For that I would use a CSS custom property:

    .rotatable {
      transform: rotate(var(--rotation-angle));
    }
...and set it directly with JavaScript

    document
      .querySelector('.rotatable')
      .style.setProperty(
        '--rotation-angle',
        `${angle}deg`,
      );
Re:Real-world CSS vs. CSS-in-JS performance comparison by Aeolun on Tuesday June 08, 2021 @07:21PM (#27442352)
That’s not a very elegant solution in any front-end JS framework. Calling directly to css/element styles is generally strongly discouraged.
Re:Real-world CSS vs. CSS-in-JS performance comparison by runarberg on Tuesday June 08, 2021 @10:22PM (#27443548)
Any insight into why that is discouraged?

Also I explain on a sibling thread that there is a fundamental difference between setting a custom property (like I do here) and setting a specific style value. While I do see how the latter can be problematic as there are no hints to the changed style in the CSS file, the former does not have that issue, particularly if you name the property with a hint to the dynamic nature if it.

Re:Real-world CSS vs. CSS-in-JS performance comparison by rajangdavis on Tuesday June 08, 2021 @06:47PM (#27442093)
And that's why I wrote:

If you want to have a range of applicable values for some CSS property or state-based styling, CSS-in-JS can be a good tool for expressing that as opposed to writing JS that updates some styling declaratively

Re:Real-world CSS vs. CSS-in-JS performance comparison by runarberg on Tuesday June 08, 2021 @07:17PM (#27442325)
I see. There might be a difference in opinion.

I think there is a fundamental difference in setting style properties in JS and setting CSS custom properties (even though CSS custom property is a style property). The main difference is that it is clear from the CSS that this property is dynamic and can be expected to change. If you overwrite a style attribute (e.g. `element.style.transform = "rotate(50deg)"`) this is less clear.

So in honestly I don’t see how setting a CSS custom property in JavaScript as lacking in expressing the intent.

Re:Real-world CSS vs. CSS-in-JS performance comparison by rajangdavis on Wednesday June 09, 2021 @12:18AM (#27444181)
The main use case highlighted in the article is React and that is the use case that I am speaking to.

With React, you don't modify the DOM directly, so the use case you have laid out is not applicable.

With that in mind, you are still free to use the CSS custom property with React, but if you want to have multiple transformations across multiple parts of an application, it's a lot simpler to use CSS-in-JS to generate styling based on state.

The use case you have brought up should work well with vanilla js/jquery; however, the context of the discussion is rooted in front-end libraries and the popularity of CSS-in-JS, so I am not sure how custom properties are relevant.

Re:Real-world CSS vs. CSS-in-JS performance comparison by The_rationalist on Tuesday June 08, 2021 @05:51PM (#27441398)
Css variables can be exported and modified in js through some libraries and without having the constraints of css in js.
Re:Real-world CSS vs. CSS-in-JS performance comparison by runarberg on Tuesday June 08, 2021 @06:15PM (#27441743)
You don’t even need a library. Modifying a CSS variable is as easy as:

    myElement.style.setProperty('--my-custom-property', 'some-value');
Re:Real-world CSS vs. CSS-in-JS performance comparison by eckmLJE on Tuesday June 08, 2021 @04:41PM (#27440480)
If you have a lot of dynamic styles that respond to application state, then even with vanilla CSS you are often writing JS to manage class declarations to get the right CSS combinations. With styled-components, instead of adding/removing/combining class names, you can write javascript inside your declarations that use the state variables.

For me, writing javascript that directly sets css properties based on state variables is a more powerful and clearer approach to managing dynamic styles than manually managing class names.

Further, when combined with typescript, you can type a theme object in the styled-components theme provider and get auto-complete in your editor on your style tokens like colors, spacing values, timing in ms, etc.

So it has many benefits beyond scoping styles to a component.

Re:Real-world CSS vs. CSS-in-JS performance comparison by runarberg on Tuesday June 08, 2021 @06:05PM (#27441601)
I’ve had great luck achieving that with CSS custom properties. I assign a custom property to a style rule and then change that property with JavaScript when needed.

I also find much state has a nice selector (e.g. :hover, :checked, :invalid, :empty, :focus-inside, etc.) or media queries `@media (prefers-color-scheme: dark)` which I can change the custom property values inside. So it is really an exception when I manually need to change the value of custom properties with JavaScript.

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Tuesday June 08, 2021 @05:59PM (#27441509)
> For me, writing javascript that directly sets css properties based on state variables is a more powerful and clearer approach to managing dynamic styles than manually managing class names.

I'd argue there's a lot more flexibility available if you choose to use classes.

Sure, it's more complicated and involves more work; but to say it's less powerful just isn't true.

Re:Real-world CSS vs. CSS-in-JS performance comparison by iaml on Tuesday June 08, 2021 @06:21PM (#27441807)
> there's a lot more flexibility available if you choose to use classes

That is simply not true. Apart from what was described in the post above, in JSS frameworks such as styled-components, you usually have ways to hook directly into css parser, allowing you to implement custom expansions, replacements or basically whatever logic you want. This is not possible unless you write your own language and transform it to css.

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Tuesday June 08, 2021 @06:46PM (#27442083)
We'll have to agree to disagree.
Re:Real-world CSS vs. CSS-in-JS performance comparison by Aeolun on Tuesday June 08, 2021 @07:28PM (#27442407)
CSS is not a programming language.

I find it hard to believe you think you can do more with plain CSS than with the full power of the Javascript programming language.

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Wednesday June 09, 2021 @04:16AM (#27445479)
Sure CSS isn't a programming language.

However, when used correctly you can create a system that will allow you maintain and apply your presentation layer in an efficient, extensible, consistent and reusable way.

Lack of knowledge isn't an excuse for believing otherwise.

--

Edit: I'm actually shocked by how much bad feeling there is for standard front-end technology.

A domain specific solution is a benefit.

Re:Real-world CSS vs. CSS-in-JS performance comparison by iaml on Wednesday June 09, 2021 @04:20PM (#27452665)
> you can create a system that will allow you maintain and apply your presentation layer in an efficient, extensible, consistent and reusable way.

If that is your intention, typescript is a better tool for the job: you get type safety, ide autocomplete, tests (have you ever written a test for css?). You are right, being a domain specific solution is a benefit, but that has nothing to do with how good of a system you can make with it. I'm sure it's possible to write a system in typescript that would be as good as what you describe, but it will be more flexible at expense of being slower.

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Monday June 14, 2021 @05:50AM (#27500868)
I don't know how to say this any other way.

I think you are entirely incorrect.

Typescript is absolutely not the solution.

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Tuesday June 08, 2021 @03:53PM (#27439945)
I've never really been comfortable with the idea, as I was raised on the idea that separation of concerns is good thing.

I think one of the main appealing aspects is the idea of never having a styling conflict with other components ever.

Less macro-level management and organisation required.

--

Edit: sorry, just realised you're talking about a specific implementation.

Re:Real-world CSS vs. CSS-in-JS performance comparison by stevebmark on Tuesday June 08, 2021 @04:08PM (#27440095)
CSS and Javascript aren't concerns, they're technologies, and separation of technologies isn't a software engineering principle. "Concern" is pretty undefined in general. If anything, they're all part of the "view" concern. It's the same reason why JSX doesn't violate separation of "concerns."
Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Tuesday June 08, 2021 @05:47PM (#27441342)
I disagree.

You're being obtuse. JavaScript is used for more than presentation.

Re:Real-world CSS vs. CSS-in-JS performance comparison by rimliu on Tuesday June 08, 2021 @04:18PM (#27440219)
Moving goalposts is not a concern either. CSS and Javascript are concerns, becaus HTML is of content/structure, CSS is for styling and JS is for behaviour. Of course somewhere along the road devs thought that HTML and CSS are to primitive to spend any time learning them properly, because of that they later found them to bee too complex and we ended up in the mess we are right now, when we try to hide the lack of understanding under multiple wraps of abstraction.
Re:Real-world CSS vs. CSS-in-JS performance comparison by stevebmark on Tuesday June 08, 2021 @04:27PM (#27440322)
I don't think it's that clear cut, you can define all sorts of behavior with both HTML and CSS, and there's no semantic meaning in `<div>`, which itself changes page style due to its layout properties. I don't know what goalposts are being moved, React/css-in-js have never violated separation of concerns. After all, css-in-js produces CSS, and React produces HTML. Separation of technologies isn't a principle, but DSLs are a well established pattern.
Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Tuesday June 08, 2021 @06:55PM (#27442150)
Is complexity a drawback?
Re:Real-world CSS vs. CSS-in-JS performance comparison by stevebmark on Tuesday June 08, 2021 @07:52PM (#27442589)
What?
Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Wednesday June 09, 2021 @04:19AM (#27445502)
I believe any extra layer between intention and result will create extra complexity.

CSS in JS just isn't required a lot of the time. We have more mature methodologies available.

Re:Real-world CSS vs. CSS-in-JS performance comparison by vgel on Tuesday June 08, 2021 @04:43PM (#27440510)
> CSS is for styling

The web has never lived up to that ideal. Sometimes you need to add a wrapper div with no semantics to make your layout work. Sometimes you need some Javascript to do a special layout CSS doesn't support or work around some other limitation. I've never seen a project that actually had meaningful "separation of concerns" based on technology, whereas almost every componentized project has at least decent separation of concerns along the component axis -- where you can drop a component into a design without thinking about it's internals at all.

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Tuesday June 08, 2021 @05:51PM (#27441394)
We are not living in a world of absolutes. To put it simply, being able to replace the presentation layer _easily_ is a benefit to many projects

> I've never seen a project that actually had meaningful "separation of concerns" based on technology.

With respect, you just need to look a little harder.

I've used react libraries which are fundamentally tied to their styling, due to the fact they use CSS in JS.

Applying custom styling has been more difficult than it should have been.

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Tuesday June 08, 2021 @06:57PM (#27442161)
I agree, I don't know why you're being downvoted.
Re:Real-world CSS vs. CSS-in-JS performance comparison by tobobo on Tuesday June 08, 2021 @04:45PM (#27440550)
I don't see CSS-in-JS as a rejection of separation of concerns. In fact, one of the big advantages of CSS-in-JS is that it lets you decide how best to separate your concerns.

Using hand-tuned CSS will undoubtedly be the most performant option, but it leaves you with very difficult problems at the boundary of your styling and content/structure concerns. If you have even a sliver of dynamic content in your website, it will quickly become near-impossible to verify that your content and styles work together as expected, or even to verify that your class names match between your CSS and HTML.

On the other hand, if you use CSS-in-JS, what you lose in performance you gain in compatibility guarantees between your concerns, regardless of how you prefer to separate. Are you putting your styles in the same files as your layout components to fully separate one feature from another? Great, you can unit test those components and be reassured that the elements are styled as expected. Are you isolating your styles to only a certain subset of components that deal directly with styling concerns? Also great—if you're using TypeScript, you can guarantee correct use of those styles at build time.

For a large enough team, those guarantees really pay off. If you have lots of customers using 2G/3G networks and want to hand-roll your CSS, I commend you! For most products, I think there's a better way to make that tradeoff and your users won't mind a slightly slower experience that has fewer bugs.

Re:Real-world CSS vs. CSS-in-JS performance comparison by lwhi on Tuesday June 08, 2021 @05:54PM (#27441449)
If you want to replace all the styling in your project, because you've had a rebrand; I would argue that CSS in JS makes your life more difficult.

If you want to share styling between your app and your website; I would also state that this process is made more difficult.

If you want to create, implement and maintain an atomic design system to build in consistency and reuse .. again, more complicated that it might have been.

To add to that, this article suggests that there's a performance deficit.

Re:Real-world CSS vs. CSS-in-JS performance comparison by The_rationalist on Tuesday June 08, 2021 @05:57PM (#27441483)
Css modules totally solve this without any css in js
Re:Real-world CSS vs. CSS-in-JS performance comparison by TimTheTinker on Tuesday June 08, 2021 @03:55PM (#27439970)
We're migrating to styled-components at my current company. It's nice for abstracting components -- either into shared libraries or separate micro-clients that are composed together at runtime.

In those scenarios, there are a few benefits: (a) no worries about accidentally trampling on someone else's styles, or styles from elsewhere overriding your styles; (b) build configuration is simpler; and (c) no worries about how CSS/SCSS is referenced/loaded - all references are handled in plain JS with no special loaders or anything.

I wouldn't use it everywhere (I've used other approaches that have worked just fine), but for some scenarios it makes sense.

Re:Real-world CSS vs. CSS-in-JS performance comparison by alexchamberlain on Tuesday June 08, 2021 @04:07PM (#27440088)
Doesn't SCSS support CSS modules to prevent trampling on each others styles?
Re:Real-world CSS vs. CSS-in-JS performance comparison by 11235813213455 on Tuesday June 08, 2021 @04:06PM (#27440066)
like you say, the benefit is to easily attach styles to components (in the same programming language, compared to scss/css modules)

note: @emotion/styled is equivalent to styled-components, lighter and easier to set up for SSR (it works out of the box https://emotion.sh/docs/ssr#default-approach)

When using server-side rendered pages, there's no performance loss on the client-side, maybe a little more processing server-side, not sure if significant

Re:Real-world CSS vs. CSS-in-JS performance comparison by WorldMaker on Tuesday June 08, 2021 @04:07PM (#27440081)
Some of the React libraries do support CSS extraction to plain CSS files.

styled-components does not, and I'm not sure exactly why. It may be related to that they support runtime variables inline as React props and/or that they didn't want to complicate the build pipeline more than they already complicate it. (Many of the libraries like Vue's have necessarily tighter coupling with webpack or whatever other packer they use to get the extraction automated.)

Re:Real-world CSS vs. CSS-in-JS performance comparison by Kiro on Tuesday June 08, 2021 @04:31PM (#27440364)
I love not having to come up with class names. That's it.
Re:Real-world CSS vs. CSS-in-JS performance comparison by The_rationalist on Tuesday June 08, 2021 @05:55PM (#27441457)
Yay let's loose semantic meaning aka understandability and reuse
Re:Real-world CSS vs. CSS-in-JS performance comparison by 1_player on Wednesday June 09, 2021 @09:23AM (#27447566)
Nothing stops you from adding a semantic class name, if you need one. Let me remind you that a tag can have more than one class.
Re:Real-world CSS vs. CSS-in-JS performance comparison by jahewson on Tuesday June 08, 2021 @03:57PM (#27439984)
For highly dynamic styles it allows me to avoid all the logic around coming up with class names and applying them. Think of it less like traditional CSS and more like a super version of inline styles.
Re:Real-world CSS vs. CSS-in-JS performance comparison by thoughtpalette on Tuesday June 08, 2021 @03:54PM (#27439954)
Main thing that I think would prevent it is the ability to use React Props/Arguments in your CSS.

e.g. const Container = styled.div` width: $props => props.width `

Re:Real-world CSS vs. CSS-in-JS performance comparison by the__alchemist on Tuesday June 08, 2021 @06:29PM (#27441909)
I think there's a conflation that confuses this: We have CSS in SPA components vs CSS, and inline CSS in HTML. In the latter, you may prefer inline to prevent flipping between files, and creating reusable data structures that are used once. In the former, you may have the same, or additional reasons like programmatically changing styles.
Re:Real-world CSS vs. CSS-in-JS performance comparison by troll_v_bridge on Tuesday June 08, 2021 @03:52PM (#27439937)
Css-in-js helps minimize context switching for logic related styling, believe this is one of the main advantages from a developers perspective. It also allows javascript developers to colocate their CSS where they want, separate file, inline, same file, etc. This is more flexible than a .vue file in that sense.
Re:Real-world CSS vs. CSS-in-JS performance comparison by mmgutz on Tuesday June 08, 2021 @07:22PM (#27442359)
An underlying appeal of React is it's just Javascript (yes even JSX templates). CSS-in-JS gives you the same flexibility and control over CSS from Javascript. No need to learn SASS, Less etc. It replaces the functionality those utilities with just Javascript. No setup required.
Re:Real-world CSS vs. CSS-in-JS performance comparison by hmsimha on Tuesday June 08, 2021 @07:50PM (#27442568)
For anyone capable of using React, there's no barrier to 'learning' sass (you can be productive with it in about 30 seconds). Admittedly there might be some additional work around configuring compilation to work with the rest of your build and dev processes, etc. But sass in it's most basic usage (nested selectors) is really what CSS should have been.
Re:Real-world CSS vs. CSS-in-JS performance comparison by dheera on Tuesday June 08, 2021 @04:58PM (#27440704)
Considering CSS is Turing complete I suppose we could also do JS-in-CSS.
Re:Real-world CSS vs. CSS-in-JS performance comparison by pentagone on Wednesday June 09, 2021 @05:05AM (#27445804)
We actually do this at my company. Our user base is mostly security focused and browses with javascript disabled so we had to find another way to make reading a simple text with some pictures thrown in terribly slow for them. We like it so much that we are currently developing a server runtime for it called node.css. We are also working on a meta package manager based on this runtime so we can manage our dependencies on different package managers more easily.
Re:Real-world CSS vs. CSS-in-JS performance comparison by iaml on Tuesday June 08, 2021 @06:27PM (#27441891)
Nobody mentioned this, but for me big benefit is that you can remove everything related to css from webpack, and no dependency on ruby/dart/whatever else. Oh, also autoprefixer out of the box.