4 ms·
The few past submissions on writing CSS in JS didn't seem to have picked up discussions. I'm glad this one at least does. I'm gonna go ahead and avoid repeatin
by chenglou 12y ago
The few past submissions on writing CSS in JS didn't seem to have picked up discussions. I'm glad this one at least does.
I'm gonna go ahead and avoid repeating what's already mentioned in vjeux's presentation (https://speakerdeck.com/vjeux/react-css-in-js https://speakerdeck.com/vjeux/react-css-in-js). Instead I'll focus on defending against the common "shortfalls" people tend to immediately point out with this approach.
Designers will be uncomfortable with learning this: React's JSX has shown that (surprise surprise) designers can bear modifying markup with a little bit of code mingled in it. In this case, JS objects in their simplest form map very well to css declarations. I don't believe designers will be scared away by this at all.
"Atwood's law", "we already have CSS", "stop reinventing CSS": no one's reinventing the idea of CSS; we're reinventing the implementation of it. Declaratively specify your layout is really nice, but you do want the backing of a real language to do so. Merging two styles is just a `merge()`, Overriding a style is just `bla.color = newColor` (assuming you don't mind the mutation; otherwise clone it or use persistent collections), and (!) reusing, importing, exporting styles is already solved with commonJS since you're working with normal js objects.
We already have preprocessors: except preprocessors are there to solve the exact problem "CSS in JS" is solving. The difference is that the latter, again, uses a real, familiar language instead of inventing an ad-hoc new syntax like we've done countless times with HTML templates. And since preprocessors need to generate the final CSS beforehand, this limits lots of potentials such as looping more than, like, ten times.
On this last point, I hope that none of the critics here used React's virtual DOM before; If you did, and are still complaining about writing CSS in JS, then you should go back and re-learn why the virtual DOM came to be. CSS in JS is the "virtual styling" equivalent for CSS. The potentials are huge (runtime feature detection for example). And this new layer allows us to map the neat, declarative style to something else than CSS: say, webGL. This is platform-independent; if anything, it's a nice conceptual model.
(Disclaimer: I made https://github.com/chenglou/RCSS https://github.com/chenglou/RCSS)
- oleg009 12y agoThanks Cheng, you nailed it. Jss was actually inspired by your RCSS idea, I just wanted a different implementation. So thank you again. Some people here assume that I am stupid and didn't learn the css basics. This is just funny.
- chenglou 12y agoGlad you did! I'm not entirely satisfied with my API but the idea's there, so very cool to see someone make something better out of it. These kind of things will receive lots of oppositions at first, but once you know the basics are right (e.g. trivial mapping from js obj to css) and build on top of it, this is bound to take off one day like JSX did =).
- oleg009 12y agoDefinitely!
- malandrew 12y agoI don't get JSX, especially now that 0.12 requires you to use createFactory (a big improvement over the old API) to encapsulate all the static JSX parts. With JSX, you're losing all the things that make having a proper programming language useful. Have you given react-hyperscript a chance? https://github.com/mlmorg/react-hyperscript https://github.com/mlmorg/react-hyperscript Having first class functions for creating DOM makes it much easier to keep your DOM state creation dry. It's more tedious if you use it just like JSX, but if you recognize where you are repeating yourself over and over again, it's trivially easy to write functions that encapsulate all the repetitive structures in your HTML. Furthermore, you don't completely break all the tools out there like eslint and whatnot. The h-script blends really nicely with solutions like RCSS and JSS.
- danabramov 12y agoLinting has not been an issue for a looong time since you can just lint the output of the transform. And designers love JSX, and having designers feel confident working directly on components saves huge amount of time.