Styled Components v3.0.0
github.com
github.com
Few quick reasons off the top of my head. - HMR works well - Theming, variables with things like ternary - Code reuse is better (Especially with theming) - Component can contain everything in one file - Extend other components easily - Better developer work flow - Contained class names
Have you run into problems with hot reloading of stylesheets? Seems like the kind of problem that should be solved by now (for reference, LiveReload was doing CSS injection back in 2011, and maybe some other tool even earlier: https://web.archive.org/web/20110428025941/https://github.co...)
2) Since it's in javascript, I can reference JS variables within the file. I can derive CSS values from props passed into the component: styled.div`color: ${props => props.userColor}`
3) I can extend other styled components' styles - i have a small shared library of central components like buttons, inputs. I can extend those: styled(MyThemedButton)`color: ${props => props.specialColor}`
4) I can use awesome micro-libraries like styled-system to add parameterization to the JSX elements: const Foo = styled.div`${space}`. ... <Foo key='1' mx={1} my={2} />... <Foo key='2' mx={2} my={3} />
5) I can change the style of every single styled component with a ThemeProvider. styled.div`color: ${props => props.theme.headerColor}`
6) I can see everything related to the component in one single file.
Your Button.scss would typically look like:
.Button { ... }
.Button--primary { ... }
.Button--small { ... }
.Button--alt { ... }
And then you need to repeat all those in Button.js (but really they're globals and anything could be using them...) return (
<button className={classnames('Button', {
'Button--alt': props.alt,
'Button--primary': !props.secondary,
'Button--small': props.small,
}, props.className)}>
Click Me
</button>
)
(I even skipped over the very first issue you run into, which is building up the className string, which is why people use the classnames helper everywhere.)And that's the simple case with only one child component – usually there are even more dynamic class names to consider for all the possible states, and other children that need their own classes as well. Once you use a solution like Styled Components, building up classes using that damn classnames helper just seems so primitive and tedious. The duplication involved in keeping selectors from the CSS in sync with those actually used in the components also drives me nuts.
- Where are we going to?
- What is the sense behind this syntax?
- Can we go back to HTML/CSS/JS when the hype's over?
What are the current Dojo? Or the current Mootools, prototype?
Backbone is an elegant library but I think its popularity and ecosystem were on a decline even before Walmart -- which was one of the biggest corporate contributors (e.g. Thorax [0]) -- did a complete overhaul in 2016 to React [1]
[0] https://github.com/walmartlabs/thorax
[1] https://medium.com/walmartlabs/publish-react-native-bundles-...
Backbone has Models, Collections, a Router, History, Sync, View and Utilities.
In contrast, React is only the view part.
So, it would be closer to say that React is just the View part of Backbone, you need to bring in the rest yourself.
Likewise, I think there's value in scoped work, as opposed to the mess than can come of globals. Otherwise you find yourself hard-coding !important when you need to make something work.
I prefer "build on lessons learned from" to "going back to" :-)
>- Can we go back to HTML/CSS/JS when the hype's over?
Frameworks like React and Vue make building large, complicated front-ends a lot easier and predictable.
Once you work a bit with React/Angular/Aurelia, you start noticing that having your styles somewhere in a stylesheet is somewhat clumsy for reusable components or overly dynamic components.
You end up having to refactor with your stylesheets very often as your app grows and things can quickly turn into a mess.
You are then tempted to put style="" declaration in your .jshtml (or equivalent) templates but that's a also mess. So your start developing a way to inject styles into dom elements. This then grow up into some strange frankeinstein that no other devs can work with because you documented nothing... so you simply start using https://www.styled-components.com instead.
I am quite happy with VanilaJS™, but I do sometimes use jQuery as well.
The alternatives are shoehorning React into simple content-centric pages (bloat), or stitching together a bespoke set of logic for a large complex app noone will be able to maintain (spaghetti).
This is the root of your consternation. Like everything in our industry - experiment, build, then judge. I was a naysayer initially too. Then I tried React, Redux, styled-components, etc. and now I'm a believer. Combine this with modern CSS like Flexbox and CSS Grid and it truly is a golden age of web development!
On the one hand: It is powerful at automating some the typical tedium you always have. It gives you a better chance to end up with a decent view model, by forcing it upon you.
On the other hand: You lose control. Almost always you can gain it back, but it is an uphill battle. If you know you need low level control (for example over complex animations and interactions in your UI) you've got to think carefully.
It all comes back to perspective. As an interaction engineer I really hate the process of trying to make a specific animation work the way I want inside of a React managed DOM. As an application developer, the purity of the approach is extremely attractive.
Also I must brush up on Grid. It's about time we got rid of god damn tables.
I was a bit confused when I followed the HN link originally.
- Performance performance performance (expect some news on that front soon)
- Streaming server-side rendering support
- A better global CSS API that allows you to use your theme dynamically
- babel-macro mode