-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sibling selectors with computed props #743
Comments
This should work if you are using the babel-plugin
|
It should work if you do this. You need a function returning the component since the component hasn't been declared yet when you reference it. const Container = styled('div') `
background: ${props => props.active ? 'red' : 'transparent'};
& + ${() => Container} {
margin-top: 20px;
}
` |
Interesting, what you said makes sense but I tested it on a local app with the Babel plugin on and it worked 🤷 |
Hey @tkh44 and @mitchellhamilton thanks for the workarounds! As far as I can tell, it would actually then require using the function in both places? Like so... const Container = styled('div') `
background: ${props => props.active ? 'red' : 'transparent'};
${() => Container} + ${() => Container} {
margin-top: 20px;
}
` This starts to feel really hacky though. More generally... this feels like something where the default behavior isn't right. It's really hard for me to think of a case meriting the current behavior, where any change to any of the dynamic props will change the It seems like Do you agree? Is this something that can be fixed? |
How would I use this with the object syntax? Is that possible? |
I think using a function value and object styles it would look like this. I haven't tested it though. const Container = styled('div')((props) => ({
background: props.active ? 'red' : 'transparent',
[`${Container} + ${Container}`]: {
marginTop: 20
}
})) |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. |
This still feels like an API flaw to me. All of the workarounds are unintuitive (and hard to determine what even the correct one is), whereas the current behavior is almost never going to be what people want in the first place. |
I think you are right, not sure how to reliably generate unique stable class names for scoping (targeting |
I disagree, I think the behavior here is correct. let Comp = styled.div`
&:hover {
color: ${props => props.hoverColor};
}
`; |
What if a new syntax was introduced like && ? It could mean any variation of this |
@mitchellhamilton u are right (as always 😅 ), maybe we could somehow detect problematic patterns (with eslint plugin?) and warn about them? The problematic pattern here would be i.e. |
@mitchellhamilton that's a great point. What do you think of |
|
@mitchellhamilton When I use
Is there something I'm missing? |
The issue got stale and I think it's appropriate to close this now. Thank you for your understanding. |
What ever happened to the Still seems like a footgun in the API to me. |
There are existing suggestions to use |
emotion
version:9.2.4
react
version:16.0.0
Relevant code:
Here I'm trying to style the sibling selector for a given component. Without the computed
background
it works fine, since the CSS classname will always be the same. But as soon as the computed property is added it seems like that means that the classname will change, depending onprops.active
.But if the component with
props.active
is in the middle of two others, this will also break the& + &
since it's no longer referring to all of the component instances.I think the way to solve this would be to have a "computed class" for each component, and also a "base class" that never changes. And the
&
self selector would always use the base class, so that it's unaffected by computed classes.This sounds similar to something
styled-components
added.Or, I might be doing something wrong, let me know! Thanks.
The text was updated successfully, but these errors were encountered: