Repository navigation
Conversation
|
This is beautiful. Solves all of the concerns with one concise, lovely API! Let's see what everybody else has to say.
Please tell me that we can reuse that to make const Box = styled.div`${{
background: props => props.bg,
}}` |
|
Oh shit just realised this solves #134 as well! const Box = styled.div.attrs({
style: props => ({
background: `hsl(
${props.hue},
${50 + props.luminosity / 2}%,
${props => props.luminosity}%
)`
})
})`
margin: 2rem auto;
width: 360px;
height: 360px;
` |
|
Holy cow that's huge! |
|
Interesting, I like it. If it's not already, I suggest being able to pass string literals in the attributes like this: const ScrollView = styled.ScrollView.attrs({
contentContainerStyle: `
background-color: red;
`,
style: `
background-color: green;
`
})^ Also, I passed the |
|
This is great. The one thought I have is that: const activeClassName = btoa(Math.random())
const StyledLink = styled(Link).attrs({
activeClassName
})`
color: palevioletred;
display: block;
margin: 0.5em 0;
font-family: Helvetica, Arial, sans-serif;
&:hover {
text-decoration: underline;
}
&.${activeClassName} {
color: red;
}
`;still requires me to think a lot about how the internals work in order to cover this use case. What I'd ideally like to get working is: const StyledLink = styled(Link).attrs({
activeClassName: css`
color: red;
`
})`
color: palevioletred;
display: block;
margin: 0.5em 0;
font-family: Helvetica, Arial, sans-serif;
&:hover {
text-decoration: underline;
}
`;This gives me no need to think about how an active class name gets generated. The only problem I see at the moment is that the return type of On an unrelated note, i wonder about calling it |
|
@ForbesLindesay For this specific case, I don't think we need that complexity. const StyledLink = styled(Link).attrs({
activeClassName: 'active'
})`
color: palevioletred;
display: block;
margin: 0.5em 0;
font-family: Helvetica, Arial, sans-serif;
&:hover {
text-decoration: underline;
}
&.active {
color: red;
}
`;It would work perfectly (or am I missing something?) |
Yeah, it seems too much like React's defaultProps. I didn't read everything yet, but the only difference I see is we are passing in actual props here (so you can set a prop based on actual props). I'm afraid it stimulates people to use this instead of const Image = styled.img`
&::after {
content: '${props => props.alt}';
}
`
Image.defaultProps = {
alt: 'My Beautiful Face'
} |
The advantage of my proposal is that it doesn't require me to know the internal structure of the If I can somehow tell styled-components to generate another, totally separate style, it doesn't matter what the internal structure is. I'm not currently sure if this is worth trying to change/extend the API for though. I may need to keep thinking about it/try out the existing API.
I'm not sure if this is actually a problem. Is there anything wrong with doing: const Image = styled.img.withDefaultProps({
alt: 'My Beautiful Face',
})`
&::after {
content: '${props => props.alt}';
}
` |
|
Nice!!! Just had a play, this feels much, much cleaner! |
If this is a problem, it's
What's the benefit of using a different API from another library ( Actually, besides Just my 2c. |
|
Thanks for the discussion and the feedback. As for the However, I could consider treating /* React Native example */
const ScrollView = styled.ScrollView.attrs({
contentContainerStyle: css`
background-color: red;
`
})`
background-color: green;
`
/* inline style example */
const Box = styled.div.attrs({
style: css`
background: hsl(
${props => props.hue},
${props => 50 + props.luminosity / 2}%,
${props => props.luminosity}%
);
`
})`
margin: 2rem auto;
width: 360px;
height: 360px;
`This would basically convert CSS snippets to objects, so no nesting or anything would be permitted, but it might be useful for these cases. Feels like this would be useful for #149 too. |
|
Ok so that previous comment isn't going to work just yet. It ends up needing to do something really similar to css-to-react-native which at the moment depends on PostCSS, and we're just about to replace PostCSS with Stylis (#286). I've opened #371 to track it. But otherwise I think we should merge this. |
|
will this feature be in v2? |
|
@kidwm yes. |
|
This should be available in |
|
That's because @geelen forgot to export it 😉 PR? |
|
Oh, yes. 😀 |
|
Wait sorry that's not right, this is working as intended. What's the code you're running? |
|
@mxstbr that's weird, tried it in |
|
@mxstbr sorry to bug you, but it happened again, replicated it here http://www.webpackbin.com/V1_gYxlKf |
|
@k15a that's weird, Firefox and Edge show the same thing. http://imgur.com/a/SIRFa |
|
Yeah I've noticed that I looked wrong that's why I've removed my comment :p |
|
Not sure what the problem is here but |
|
@geelen Can it be done like this? const Button = styled(({className, submit}) =>
<input className={className} type={submit ? 'submit' : 'button'} />
)`
/* ... */
` |
|
@streamich this certainly works, but please don't comment on stale PRs and issues. Especially since this is a PR and you have a rather unrelated question. I don't mean to be rude, so my suggestion is to go to Spectrum, or Twitter, or at the last create an issue for your questions 😉 |


Ok, so this is my proposal for #291, #134 and #184 (and probably others). I think it does what we need in a really nice way:
So, @ticky's original code becomes:
And @ForbesLindesay's becomes:
I've added a bit of smarts to make it more similar to how interpolations work:
It gets passed the same
{ ...this.props, theme }object that interpolations do, and recalculates whenever the CSS snippet gets reevaluated. I.e. it works exactly the same, as much possible.If you have a
propand anattrwith the same key, thepropwins (in that sense they're likedefaultProps). That is except forclassNamewhich just adds everything together. I decided against trying to generalise it—adding extra classes is kind of a special case so I'm happy to keep it that way. If you want any other kind of weird manipulation of properties just make a stateless functional component.I've also called it
attrsbecause, well, I thinkpropsis too overloaded. And, at the end of the day you're rendering HTML elements most of the time. They have attributes, not props. And I see this sort of API most useful when working with these elements—if you have more complex objects you should define a real component. I'm not trying to reinvent React here, but being able to define attributes in the same way you generate and attach CSS turns out to work quite nicely.The big exception to the previous paragraph is, of course, react-router's
Link. It masquerades as a HTML element, but it's a component, so having a single syntax that preserved that facade was important to this. Still, I didn't think it was worth changing the name.Oh also anything in
attrsgets turned into aproplater. That's not very useful, except for some cases like this:Happy to hear feedback, in particular if you can see this breaking down in certain circumstances (that don't just warrant adding an extra component). And everyone's reaction to the name, as well. Don't submit alternate syntaxes here though. That discussion is over at #291, and I'm pretty sure we've considered every possible syntax already. If not, continue the conversation there.
Closes #291, closes #134 and closes #184.