CSS-in-JS: still worth it in 2024?

ahmed_khan92

New Member
Messages
63
Reaction score
0
Location
Toronto, ON

I've been using styled-components for years, but I'm starting to think it's not worth the overhead anymore. The bundle size keeps growing, and the runtime cost is real, especially on mobile. Last week I refactored a project to use CSS modules with some PostCSS magic, and the difference is night and day. Page load felt snappier, and the bundle dropped by like 40%.

But I do miss the dynamic styling and the colocation. And the DX with TypeScript isn't as smooth. Anyone else here made the jump? Or are you still all-in on CSS-in-JS? What's your stack looking like in 2024? I'm curious if I'm just late to the party or if there's still a case for it.

jt_loves_tacos

New Member
Messages
75
Reaction score
0
Location
Phoenix, AZ

Yeah I made the jump about a year ago and haven't really looked back. The bundle size thing is real, but what killed it for me was the runtime cost on cheap Android phones. Felt like I was shipping a whole styling engine for no reason.

That said, I do miss the colocation sometimes. My workaround has been CSS modules for layout and static stuff, then inline styles or CSS variables for the dynamic bits. Not as clean but it works. The TS DX thing is overblown imo, you can get solid autocomplete with a decent setup.

What PostCSS plugins did you end up using? I'm always hunting for ways to make the build less painful.

wafflesRgood

New Member
Messages
57
Reaction score
0
Location
Berlin, DE

ok so I'm not a pro dev, I just build stuff for my fish tanks (monitoring dashboards, feeding logs, that kinda thing) and the bundle thing hit me too. My little dashboard was like 300kb of JS for what is basically a table and some toggles. Moved to CSS modules and yeah, way snappier on my old phone.

but I kinda disagree with jt_loves_tacos on the dynamic styling part. Losing it is a bigger deal than people admit. My data changes color based on water params (temp, pH, ammonia spikes) so I ended up with piles of conditional classnames and it got ugly fast. CSS variables set inline works but my TS types for the vars are a whole mess and I don't know how to fix it tbh.

also nobody answered the postcss plugin question and I want to know too lol. I've just been running autoprefixer and calling it a day, feels like I'm missing half the magic everyone talks about.

side note my tank controller page was slow as heck on mobile data and someone here pointed me at website speed discussions which helped a little, but the JS was the real problem all along. so yeah, kinda team CSS modules now.

patrick_jones88

New Member
Messages
60
Reaction score
0
Location
Glasgow, UK

Nobody answered the plugin question twice now so let me take a swing at it. For most projects postcss-preset-env is the one you actually want, it bundles autoprefixer plus nesting, custom media, custom properties fallbacks, and a bunch of other stage-2/3 stuff behind one config. So if you're "just running autoprefixer" you're basically running a lighter version of the same thing. Add cssnano for minification in prod, postcss-import if you want to @import files that actually get inlined (native CSS @import does not do that), and postcss-mixins if you miss Sass-style helpers. That's the whole magic. It's less magic than people make it out to be tbh.

On the TS side, wafflesRgood's complaint about typed CSS variables is legit and it's the thing that almost pushed me back to styled-components. The trick that actually works is augmenting csstype, not fighting it. Something like:

declare module 'csstype' {
  interface Properties {
    [index: `--${string}`]: string | number | undefined
  }
}

Drop that in a d.ts and now style={{ '--temp-color': 'red' }} type checks everywhere without you casting to any. For CSS module classnames themselves, the typescript-plugin-css-modules VS Code plugin gives you autocomplete and go-to-definition, and if you want generated .d.ts files checked into the repo you can run typed-css-modules in a watch task. It's a bit of setup but it's a one-time cost.

I'll push back slightly on the "losing dynamic styling is a big deal" framing though. Honestly for anything data-driven I reach for CSS variables plus a couple of utility classes and it's cleaner than the styled-components version was, because the logic lives in JS and the styling lives in CSS. Where styled-components genuinely wins is when you need to compose variants and theme deep component trees. That's a smaller set of apps than people admit.

kewldad

New Member
Messages
60
Reaction score
0
Location
Calgary, AB

lol the fish tank dashboard is the most honest use case in this whole thread. water params driving color is literally the thing CSS-in-JS was invented for, so I get why dropping it stung.

But I'll push back a little on the "losing dynamic styling is a bigger deal than people admit" take. You can claw back like 90% of it with data attributes plus CSS vars and never touch a type cast. Instead of jamming style={{'--temp-color': temp}} on every element, drop data-status="warn" or data-status="critical" on the container and do the color logic in CSS. TS stays happy, no csstype hacking, and all your thresholds live in one place instead of scattered conditionals. The only time I still reach for an inline var is genuinely continuous stuff, like a gauge width percentage. Everything else can be a variant class.

Patrick's csstype augmentation trick is legit though, used it on a client project last year and it saved me from a mountain of as any. Just don't go wild, the second you've got 20 custom props on one component you've basically rebuilt styled-components but worse.

On mobile, I'll die on this hill: for dashboards and marketing sites CSS modules win every time, no contest. The only place I think styled-components still earns its keep is a big design system monorepo pushing theming and variants to a dozen teams, and honestly even there vanilla-extract is the better 2024 answer. Also waffles if you're still tuning that controller page, half the win is just not shipping a styling runtime. Worth a skim over here if you go down the perf rabbit hole

https://websiteforums.info/forums/cdn-and-performance.4/

rk_designs

New Member
Messages
68
Reaction score
0
Location
Vancouver, BC

kewldad is right about data attributes and I'll go further: if you're tempted to jam 20 custom props onto one component, you don't actually want CSS modules, you want Tailwind. Nobody in this whole thread has mentioned it and that's wild to me. The "losing dynamic styling" complaint mostly evaporates once you've got data-status variants and a handful of arbitrary values. I fought Tailwind for a year, finally caved on a dashboard rebuild, and the colocation people miss from styled-components just kind of shows up in the className string. Not as pretty. Works fine.

Patrick's plugin list is solid. preset-env + cssnano + postcss-import covers like 95% of what normal people need. I'd skip postcss-mixins unless you genuinely miss Sass, it's just extra config surface for stuff custom properties already handle. And yeah the csstype augmentation is the correct answer, but honestly I still just cast when it gets annoying. I've got a VPN that drops mid-deploy more reliably than my types break, so I pick my battles.

Where I'll push back on the thread though: nobody's talking about the SSR angle. Bundle size is the easy number to point at, but styled-components on a server-rendered app means serializing all those styles into the HTML and then rehydrating a runtime that has to reconcile them. On a cheap Android that's worse than the kB count suggests. That's what finally killed it for me, not the size. Size was just the excuse I gave my PM.

wafflesRgood your fish tank dashboard is genuinely the best justification for CSS-in-JS I've seen in here and I'm sorry you lost it lol. But kewldad's variant approach would actually clean up your water params thing a lot. Thresholds in one CSS file beats scattered conditionals every time.

You must log in or register to reply here.