4 ms·
One major problem I've run into is that CSS in JS usually lacks support for pseudo-classes and browser breakpoints. The recommendation from communities in fram
by danShumway 8y ago
One major problem I've run into is that CSS in JS usually lacks support for pseudo-classes and browser breakpoints.
The recommendation from communities in frameworks like React is that you should use JS and extra elements to polyfill that behavior. This is both less performant and less semantic. It really doesn't work particularly well if you're used to leveraging CSS in powerful ways.
Stuff like :first-of-type, sibling selectors, and so on are extremely useful. You give them up when you start manually attaching inline styles, and both JS performance and JS complexity to replicate those features will pretty much always be worse.
Doing animations in JS is usually a step too far even for the React community, so the recommendation I see there is often that you should define normal CSS classes and dynamically apply them. But now you're starting to mix CSS and CSS-in-JS, which is do-able, but can come with own set of subtle consequences. It also significantly lessens the benefit of CSS-in-JS, which is that you get to stop worrying about conflicts. It's not a dealbreaker, but it means you're somewhat back to managing the same name conflicts and typos that you were before.
I started out as much more open to CSS in JS than I am now. I was cautious about it and usually avoided it for personal projects. After doing a little bit more work on projects that use this pattern, I now feel like it's probably a bad idea, and advise other people to be careful about using it, even if I'm not willing to take a hard stance quite yet.
It's not that component-based CSS is a bad idea, I'm much more friendly now towards BEM than I used to be. And it's not even that CSS outside of JS doesn't have problems (the authors point about loading order is a particularly good example, albeit one that really shouldn't be coming up very often in a component based world. Also yeah, testing). I'm just less convinced that CSS in JS is a sufficiently scalable solution to those problems, or that it doesn't have significant problems of its own.
- vcarl 8y agoWe use styled-components, which is one of the most commonly used css-in-js libraries, and it supports both pseudo-classes and browser breakpoints. I'm not sure how applicable this argument is, my experience runs counter to the premise. I've also done animations in JS using react-motion and react-flip-move and had a pleasant experience with both.
- Cu3PO42 8y agoI think you're missing an important point. Not all CSS in JS solutions attach styles as inline styles. Some of them, such as Styled Components [1] and Emotion [2] (and many more) will actually generate proper CSS Stylesheets and attach them to the document, thereby enabling usage of all CSS features—including, for example, pseudo-classes. This may incur some additional runtime cost when the page loads (which is definitely nothing to dismiss), but should be (roughly) as performant as "native" styles after. They still come with other disadvantages (and advantages), so I'm not saying you should definitely use them, but maybe you should take a look at some implementations you might not have seen yet. Personally I arrived at the conclusion that they're not for me and that I'll just stick to SCSS. [1] https://www.styled-components.com https://www.styled-components.com [2] https://emotion.sh https://emotion.sh
- danShumway 8y agoYou're right, I didn't know that there were solutions that took this approach! Yeah, I would agree that dynamically attaching stylesheets is better than using the default React model for inline styles. I'd still be worried about the performance implications, but this might address a lot of my concerns.
- tracker1 8y agoJSS (react-jss) has a couple plugins to handle this very well, and allows for nesting similar to scss/less. const styles = theme => ({ main: { padding: theme.spacing.unit * 2, fontFamily: theme.fonts.primary, ... '&:hover': ... '@media (max-width: 600px)': ... }, inactive: {}, }); You get the benefit of the nested structure per class... but the added options of easily establishing the classNames, which are computed to be unique, so no conflicts in practice. const FooComponent = ({ classes, inactive }) => ( <div className={classnames( classes.main, { [classes.inactive]: inactive } )} > ... </div> ) In this way, you can bring in shared values from your theme. I can now inject different values into the theme from a central point in the application, via release, runtime, from an api/backend. I still have the option for psuedo selectors and media queries. There are more advantages with FAR less bugs than using CSS, even with SCSS/Less or other tooling. Stylesheets are then generated with dynamic selectors, and the selectors are applied to the component, and the stylesheet is injected dynamically.
- danShumway 8y agoThis strikes me as a much more sane approach, which makes me really surprised that somehow I haven't heard about it until now. To me, the biggest problem in CSS that makes me not completely dismissive of JS-in-CSS is scoping (although BEM does kind of tend to solve that problem pretty well for me already). Dynamically generating the class names seems at first glance at least to be a good solution to that problem. Doing it as part of a compile step would be better than doing it clientside or serverside, but obviously that's not really feasible for most dynamic applications. Possibly there could be a Babel step that swaps out references in source code or something. But yeah, I'm interested into looking into this more. Thanks for letting me know!
- tracker1 8y agoThere are different CSS-in-JS libraries, I'm using react-jss[1] myself, which is the example... I'm also using it configured via material-ui[2] component library which has a couple addons for nested structures, psuedo-selectors, media queries etc. I believe there are options for many of them to run a pass through your components and generate CSS at build time, but I find that dynamically loading (react-loader) my components works out better overall, and don't notice the CSS generated at runtime in practice. I do inject a default style baseline that is enough for the app loader spinner, and the global error handler's output screen. [1] https://github.com/cssinjs/react-jss https://github.com/cssinjs/react-jss [2] https://material-ui.com/ https://material-ui.com/