Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSA: CSS in JS also a bad idea #4

Open
acobster opened this issue Jun 17, 2019 · 2 comments
Open

PSA: CSS in JS also a bad idea #4

acobster opened this issue Jun 17, 2019 · 2 comments

Comments

@acobster
Copy link

acobster commented Jun 17, 2019

This project made me laugh with mirth, and cry in pain. And then I thought about it. This is actually useful in that it proves the point that CSS and JS should remain as decoupled as possible.

So to anyone having a laugh at this repo and then shrugging it off to go back to your CSS-in-JS architecture, please read this: https://keithjgrant.com/posts/2015/05/against-css-in-js/

This has been a Public Service Announcement from your friendly neighborhood opinion-haver.

@sdunham
Copy link

sdunham commented Jun 17, 2019

Here we go

@jabsatz
Copy link

jabsatz commented Jun 17, 2019

There are libraries like styled-components that address every single issue of that article.
It does not create any lock-in problems since you actually write CSS in a template string, and it gets parsed exactly like you wrote it.
It supports use from outside without it's use, since it works exclusively by creating classes and passing them through the className prop. You can pass a regular class and write some CSS, or you can style it through their own tools.
It has every CSS feature since it actually writes CSS. It also has nesting built-in, which is not in CSS yet, but many other tried and tested style sheet languages like SASS have it.

And there ARE some downsides in CSS. If you work solo and you know what you're doing it may be easy to keep stylesheets short, but in a team it frequently happens that some rules apply not only to the things you're working on, but some other component buried deep in the app, since classNames match globally. When you encounter something like this, it's quite hard to erase unused rules, leading to a huge creep in stylesheets that I've seen to some extent in every single project I've worked on.

Styled-components for example, does not rely on this, since it creates a unique className for every component it creates. So when you see some CSS in one place, you can be absolutely sure it only affects that specific component.

All of this is without getting into all the advantages of having CSS-in-JS, like the capacity for easy theming and implementing different states for a component in a much more declarative way.

I understand the gut feeling when hearing CSS-in-JS, I had that too for a time, but it has evolved way past style objects in the past few years. It's easier both for "front-end devs" who can implement things like theming and "front-end designers" who can still work in the same CSS syntax they know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants