Author here, happy to respond to any questions. AMA!
Honestly, it sounds like you now have a write-only site, as you will have a hell of a time 5 years down the road when the CI changes. Personally, I find the Vue.js concept much nicer, but even given that, I define my styling in LESS (SASS...) to benefit from inheritance/mixins on the level of styling. Semantic approaches like BEM or Bulma make a lot more sense to me. It seems to me React has made a feature out of a f…
Why I Write CSS in JavaScript
161–170 of 255 posts
Re: Why I Write CSS in JavaScript
#162styled-components is not popular on HN it seems. Allow me to share my own positive experience: I have built websites in a variety of different ways, with a variety of different CSS schemes. I've worked with SASS, LESS, BEM, every major css framework, in both SPA and SSR environments. I have also built react SPAs with the above variety of CSS schemes, as well as CSS-in-JS libraries like Aphrodite and Styled Components…
Re: Why I Write CSS in JavaScript
#163Earlier quoted context omitted.
You're right, but community support around a certain pattern often makes it the de-facto practice, so defaults matter even for a non-opinionated library (where "defaults" are essentially what tutorials and thought leaders point to)
I always thought css-modules is the de-facto practice
Re: Why I Write CSS in JavaScript
#164They've got it figured out. Stick your styles at the bottom of your component. Vertical flexbox by default. And... that's about it. It just works beyond that point.
We liked it so much we ported our webapp from react to react-native-web. It's an amazing library. You can freely mix in regular react, but the styling story is so good with react-native-web that you'd never want to. It powers mobile.twitter.com and the author (same person who made normalize.css) recently joined Facebook, which hopefully means more development to come.
You'd have to pay me to go back to styling the web with css. It really is a hair-pulling experience.
Re: Why I Write CSS in JavaScript
#165In 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
#166Earlier quoted context omitted.
CSS is not a programming language, and it shouldn't be viewed or compared as one. It has different functionalities.
That limitation is why people like the article writer often replace it with a programming language.
Re: Why I Write CSS in JavaScript
#167Earlier quoted context omitted.
This is one of the reasons why I don’t like web development. Here’s how MS solved this in XAML, a markup language used in Windows world for GUI:
I find this extremely hard to read. I mean I can see the advantage, but there must be a better way?
I’ve wrote the above example because it shows a possible solution to the stated problem, i.e. how to introduce namespaces for styles without also introducing a full-blown programming language with scopes and variables. The exact syntax can be replaced making it more compact and easier to read.
Compared to this mostly-immutable scoped dictionaries, way more things can potentially go wrong with code, regardless on the programming language.
Re: Why I Write CSS in JavaScript
#168Imagine a programming language that only had global variables. You have to carefully name your variables using complicated conventions in order to avoid any conflicts or unexpected behavior. Even your functions don't have local variables; there is no lexical scoping. Any function can overwrite or mutate what any other function is doing inside. There is no real encapsulation. After you've done this perfectly, your app…
Very typically, for a given app and page the styles of individual elements and components are not independent. They also depend the app theme, app section, layout area, component group, etc. The element fits the component. The component fits into the section, the section fits into the page. The specific relationships in a particular case are different of course. Styles are visual and the idea that a component on a page has nothing visually to do with the rest of the page is wrong.
The typical scoping rules of programming languages doesn't fit that well here.
To the extent some styling solution encourages you to treat styles that are dependent as independent, that's not a good or useful thing.
Re: Why I Write CSS in JavaScript
#169Imagine a programming language that only had global variables. You have to carefully name your variables using complicated conventions in order to avoid any conflicts or unexpected behavior. Even your functions don't have local variables; there is no lexical scoping. Any function can overwrite or mutate what any other function is doing inside. There is no real encapsulation. After you've done this perfectly, your app…
Re: Why I Write CSS in JavaScript
#170It reminds me of when I was in college learning C where all of your code is just dumped into one namespace, and then later learning Python (or basically any other modern language) with actual modules and namespaces. After seeing it done that way it just makes no sense that it would ever be done the old way again.