4 ms·
I agree that single file components are awesome. I wish you had been introduced to styled components in react: import styled from 'styled-components' con
by giantsloth 9y ago
I agree that single file components are awesome. I wish you had been introduced to styled components in react:
import styled from 'styled-components'
const Container = styled.div`
padding: 10px;
background: ${
({ isHovered }) => isHovered ?
'green' :
'red'
};
`
const H1 = styled.h1`
font-size: 15px;
`
const P = styled.p`
font-size: 10px;
`
const MyComponent = ({ isHovered }) => {
return (
<Container
isHovered={isHovered}
>
<H1> Hello <H1>
<P> This is a thing </P>
</Container>
)
}
- untog 9y agoThat really doesn't work well with: > Our designer could churn out neat html/css, but was a beginner in Javascript. I write JS every day, and frankly that code looks like an unholy mess to me, so I can't imagine what it looks like to a beginner. Is "styled.div``" a function call? It's not self-evident. How do you do inheritance? Is that a template string with functions inside it? Would that CSS autocomplete? That code looks very much like it sacrifices ease of writing CSS for ease of writing JSX. That isn't the correct tradeoff for everyone.
- giantsloth 9y agoInteresting critique. I would highly suggest checking out template literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... Yeah styled.div is a function as you can check out in the mdn docs I linked, it's really cool stuff. Inheritance is done like: const List = styled.ul` li { padding: 10px; } ` const HeavyPaddedList = styled(List)` li { padding: 20px; } ` If you wanted to just change the li you'd have to: const ListItem = styled.li` padding: 10px; ` const HeavilyPaddedListItem = styled(ListItem)` padding: 20px; ` You're writing scss within the text blocks. You're correct that you need some understanding of JS. I've never worked with designers that wrote css, I'd also probably not trust them to do so (my own failings). I don't agree that it looks like an unholy mess (hyperbole may be lost on me), but I can see how it could be jarring at first look. I had a similar reaction when I looked at Relay (https://facebook.github.io/relay/ https://facebook.github.io/relay/), when they used literals for data fetching.
- untog 9y ago> You're writing scss within the text blocks. So, yeah, you lose all the syntax highlighting etc. that comes with just writing normal CSS. I don't think it's unreasonable for a designer to object to that - how would you feel if you were presented with a build system that required you to write all your JavaScript inside one big string statement? Maybe it's just me, but I don't find it intuitive at all. `styled` has properties for, I assume, every HTML tag? But it can also be called as a function for inheritance purposes? And that inherited call appears to use the same tag as its parent... but what if I want to reuse styles across different tags? How could I define some shared CSS properties I'd use across different elements? Don't get me wrong, it is cool stuff. But it also feels far too much like hand-wavey magic stuff. I've literally never used Vue before, but looking at this page: https://vuejs.org/v2/guide/single-file-components.html https://vuejs.org/v2/guide/single-file-components.html I have zero confusion about how to write styles for it. And I really don't understand why styled-components would be worth the extra effort by comparison. What does it bring to the table? It's different without a compelling reason for being different.
- shados 9y ago> So, yeah, you lose all the syntax highlighting etc. that comes with just writing normal CSS Except you don't. Editor support is quite good, and syntax highlighting, syntax validation, auto-completion, etc all work. I mean, same way with Vue really. If the editor didn't know wtf a Vue file is, you'd lose all the integration too.
- untog 9y ago> I mean, same way with Vue really. If the editor didn't know wtf a Vue file is, you'd lose all the integration too. In a way, that's fair. In another way, the example single component Vue file I linked to is an HTML file. It has a <script> tag and a <style> tag. Just associate .vue with HTML and you're done. That gets to my broader point as well - styled-components reinvents the way you declare styles and the way you apply styles, and requires the entire thing to be written as a JS file. And for what? I still don't understand what the advantage is over a syntax everyone already knows (and has validators for already!)
- sametmax 9y agoGod this is horrible. It's ugly. Hard to read. Things are scattered all over the code. And where is my sass ?
- giantsloth 9y agoIt doesn't support sass, but it does support scss.
- allover 9y agoTo all intents and purposes, when people say Sass, they mean Scss.
- mottomotto 9y agoI much prefer putting the styles in a separate {componentName}.scss file, import'ing that into the component and using the Webpack Extract Text Plugin: https://github.com/webpack-contrib/extract-text-webpack-plugin https://github.com/webpack-contrib/extract-text-webpack-plug... I guess now you have two files per component but to me, it's well worth it.
- onenite 9y agoYou can work with two files per component if you must truly put the styling in another file. One of the boons of react is that you can create a set of reusable base components that can be built upon throughout your app. If you can code-split the app, lazy-loading components as needed, and if you can cache all of the static app anyway, why does it matter if the presentation is tied to the logic? The same can be said for vue.js. :) The two languages have many merits and they both make their own sacrifices. But they are ultimately two peas of a pod. Like Thor and Loki. But who is Thor and who is Loki? ;)
- jrobn 9y agoCode like parent comment is why I use Intercooler.js [1]. [1] http://intercoolerjs.org http://intercoolerjs.org
- NumberCruncher 9y agoIt is a good example for solving problems without overengineering them.
- agumonkey 9y agoCute ideas in there. Much appreciated
- kimjongman 9y agougly piece of code
- snarkyturtle 9y agoThere is the Vue equivalent though: https://github.com/styled-components/vue-styled-components https://github.com/styled-components/vue-styled-components
- nikcub 9y agoWhy do packages like this need to be ported or re-implemented for Vue rather than referenced? That package hasn't been updated since August while the root styled-components library has ~700 closed issues and 11,300 watchers I've seen it with other stacks as well with almost complete re-implementations, a lot of duplicated effort and it feels like it goes against the packaging philosophy for javascript