-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
fix self-referential behavior in combo selectors #2071
Conversation
before: & + & -> .b + .b after: & + & -> .b + .sc-a
in a nested scenario, it should refer to the main component but also acquire any selector modifiers tacked on to the amp
13c666c
to
73af3ce
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks solid!
|
Perhaps we can add a snapshot test for a combination of const Wrapper = s.div`
&&&{
h2{
background: red;
}
}
& + & {
background : blue;
}
& + & ~ &{
background : yellow;
}
`; |
@imbhargav5 sure, can you add an example for me to your comment and I'll put it in the PR? |
4bd3981
to
c8b97cd
Compare
@probablyup Just did |
@imbhargav5 done |
Awesome. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it works I guess LGTM, but damn I'm afraid of that regex having unintended edge cases 😕
@kitten any issues or tests I should add to the suite? |
@probablyup looks solid to me; the only thing we could do differently is to actually add it as a stylis middleware, to its selector input transform function. |
… For the delf ref issue read more here: styled-components/styled-components#1275 or take a look at initial PR: styled-components/styled-components#2071
… For the delf ref issue read more here: styled-components/styled-components#1275 or take a look at initial PR: styled-components/styled-components#2071
#201) * Adjust toHaveStyleRule method to use proper base component name (changed in commit: styled-components/styled-components@1c19edc#diff-8a1d0988ee5ae37265b4647fa41def2a), disabled calling lifecycle methods for enzyme shallow rendering - unfortunately this would need to be fixed in tests that use the library (no better solution yet). * Fix self-ref issue and try to remain compatible with all other cases. For the delf ref issue read more here: styled-components/styled-components#1275 or take a look at initial PR: styled-components/styled-components#2071 * Remove tests that got outdated to to changes in styled-components implementation
#201) * Adjust toHaveStyleRule method to use proper base component name (changed in commit: styled-components/styled-components@1c19edc#diff-8a1d0988ee5ae37265b4647fa41def2a), disabled calling lifecycle methods for enzyme shallow rendering - unfortunately this would need to be fixed in tests that use the library (no better solution yet). * Fix self-ref issue and try to remain compatible with all other cases. For the delf ref issue read more here: styled-components/styled-components#1275 or take a look at initial PR: styled-components/styled-components#2071 * Remove tests that got outdated to to changes in styled-components implementation
When a component refers to itself with the ampersand placeholder the behavior today is a bit inconsistent and won't work as intended if your component has dynamic styles.
For example:
The second instance of
Comp
will not get themargin-left
because a different className is generated due to the different color prop, and today ampersand refers to the current styling context className.It effectively looks like this:
Note how the combo is
.a + .a
and.b + .b
, so only two adjacent components with the exact same styling props would trigger a match. This is a surprising behavior and makes compositions more troublesome.The solution is to treat the second
&
as a reference back to the component itself (.sc-a
), not the style of the component.Then the styles look like this:
And any
Comp
next to aComp
will receive themargin-left
. Note that the reason the first ampersand isn't also switched to the static component class is that will break style isolation.before:
& + & -> .b + .b
&[disabled] { & + & } -> .b[disabled] + .b[disabled]
after:
& + & -> .b + .sc-a
&[disabled] { & + & } -> .b[disabled] + .sc-a[disabled]
fixes #784