Live data from Hacker News

Why I Write CSS in JavaScript

mxstbr.com

21–30 of 255 posts

Re: Why I Write CSS in JavaScript

#21

In my opinion this is a workaround for a language flaw in the React concept. Look at this beauty for comparison: https://vuejs.org/v2/guide/single-file-components.html

Yup, that's what you can have with precompiled templates. Riot.js introduced the same concept of scoped css years ago, I wish it was more popular. On the other hand it goes against the "it's just javascript" concept of React.

Re: Why I Write CSS in JavaScript

#22

In my opinion this is a workaround for a language flaw in the React concept. Look at this beauty for comparison: https://vuejs.org/v2/guide/single-file-components.html

Hm, I don't share your opinion on this matter, to be honest. You can easily avoid this in both Angular (which does it out of the box) and React by including CSS Modules (https://github.com/css-modules/css-modules) which, if you use create-react-app to build your projects, is a 5 minute change to your architecture. So there really is no reason to write that down as a con for React.

Re: Why I Write CSS in JavaScript

#24
post #8

Earlier quoted context omitted.

- Were there moments when you cried back SASS or vanilla CSS? - In your react example, I see you created as styled.h1, but that's an awkward way of bootstrapping a component. Even if I used SC, I would still put the style information outside my component code in the same directory. At this point I don't see the benefit over doing something like: import * from './login.scss'; What can SC do that regular CSS or SASS ca…

The big win for me is if you're making an SPA, all the knowledge of the app state is already stored in JS (redux, or local state, or handed down as props if using React). So instead of toggling styles on a class, you just hand the state to the styled component, since it's just a function that returns css.

Toggling styles is a good question. I have sometimes multiple classes that have to work together. For example to open a panel, I use some translate/transform, different positioning. How do you compose different styles for a single component?

How do you deal with mobile, where sometimes I have to override dozens of declarations with media queries?

Re: Why I Write CSS in JavaScript

#25
The down-side with this is very high coupling between UI logic and style.

If style becomes out of fashion, you have to modify files that contain logic. If framework (react/vue) becomes out of fashion, keeping styles will be difficult.

The other down-side is thousands of web developers having to re-learn CSS and the tooling behind your app instead of just changing CSS files.

Re: Why I Write CSS in JavaScript

#26
post #2

Author here, happy to respond to any questions. AMA!

Honestly, I think the CSS-in-JS approach is not for me and we haven't used it except for times where there was no other way to do something.

Instead we have used CSS Modules (https://github.com/css-modules/css-modules), first by including it manually in webpack, now it's already built into create-react-app so I just have to install node-sass and call my Sass files ComponentName.module.scss and I can use them the same way in my .tsx/.jsx files that you use CSS-in-JS. IMO that's a lot less messy and makes your jsx files smaller and better to read. Also it's easier to write Sass and have the pros of that language on top.

Did you consider this approach at all and what did you not like about it?

Re: Why I Write CSS in JavaScript

#27

How do you handle advanced CSS Level 3/4 selectors, CSS grid, and media queries?

The idea with CSS-in-JS is that you don't use selectors. Since you are constructing DOM nodes individually, you don't need to select them. Styled components can accept props, which lets you style dynamically (e.g., using an array index to replace `:nth-child`).

With regard to media queries, it depends on the library. For the library the author is using, the media queries are defined inline: https://www.styled-components.com/docs/advanced#media-templa...

Re: Why I Write CSS in JavaScript

#28
post #7

Earlier quoted context omitted.

How do you plan to accommodate NoScript users?

I know this will be unpopular, but after a certain point, noscript users just get a bad experience and that can't be helped. If a developer wants to provide a rich experience, then there will always be certain platform requirements. Without them, you will have a lacklustre experience. It's like saying "I want to have all the whizzy functionality of your app, but I want to access it using a standard, run of the mill,…

I know how the sausage is made, so I know that making a full web app work without JS isn't at reach for most teams/products.

By knowing how the sausage is made, I'm also aware that the line marking the aforementioned certain point is often drawn too early, at informational (i.e. document) stuff where HTML and CSS are fully capable on their own.

Post reply on HN