-
Notifications
You must be signed in to change notification settings - Fork 255
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
Incorrect style order #671
Comments
Thanks for that! |
In the code example, We can resolve this by having const Bar = styled(Base, StyledBase, {...}) Fixed example: |
Hi @jonathantneal, Thanks so much for the clarification. At the time of opening the issue I didn't even know However, in few use cases where I've stumbled upon this StyledBase might not be available (not exported), e.g. // Button.js
const StyledButton = styled('button', { ...buttonStyles });
export const Button = React.forwardRef((props, ref) => {
// additional button logic here
return <StyledButton {...props} ref={ref} />
}) Consuming: // App.js
import Button from `@ui/Button`
// No access to StyledButton here, since it's an unexported implementation detail of the Button
const MySpecialAppButton = styled(Button, { ...specialAppButtonStyles }) Just curious whether this is a #wontfix, or your solution is a temporary workaround. |
I wouldn’t say it’s #wontfix. Yes, it would be difficult for Stitches to reach into However, we might benefit from a new rendering order algorithm that ensures the order you expect. It seems very reasonable. I would say, we need more time to think about this issue. |
Same issue :( We've. used |
We have similar use-cases and had to come up with weird workarounds in so many places |
Same. Had to roll back the upgrade as the specificity of styles flipped in a lot of components causing a lot of visual regressions. |
This is being worked on. |
Hey, it seems like there was some kind of misunderstanding. I let y’all know a ways back that this issue is not with Stitches. I’m here to say now that the issue we are resolving will not actually resolve this. Here’s a distilled version of the original issue: const StyledBase = styled('button', {
display: 'inline-flex',
color: 'inherit'
})
const Base = (props) => {
return React.createElement(StyledBase, props)
}
const Test = styled(Base, {
color: 'red'
}) How would |
Hey @jonathantneal, Thank you for the clarification. Without knowing much about Stitches internals, I think the assumption that this would work was because a lot of us are coming from styled-components/emotion where it consistently worked that way (and in Stitches < 0.2). To expand on the use case above, one very common scenario is where we extend UI library components with more app-specific styles: // App.js
import Button from `@ui/Button`
// Button is a wrapper around StyledButton with extra logic, however StyledButton isn't exported.
const MyAppButton = styled(Button, { ...appButtonStyles }) Where With all that being said, is there at all a world where this can be supported, even if it’s in future versions? |
That is fully supported. You should be able to do this. I was commenting on reaching inside a functional component, which I would not like to do since that would run components out of order (running the component for side effects, then attaching the class names, versus what we do which is pass the class name into the component). We have been working out another ordering issue, which may happen to do what this issue expects. My first response was connecting these issues, when I should have distinguished these. Let me phrase this another way with some code from the reproduction: /* StyledBase renders "button". */
const StyledBase = styled("button", {
display: "inline-flex",
color: "inherit"
});
/* Base does not extend StyledBase. Base renders StyledBase. */
const Base = (props) => {
return <StyledBase {...props} />;
};
/* Button1 extends StyledBase. Button1 styles will overwrite StyledBase. */
const Button1 = styled(StyledBase, {
color: "red"
});
/* Button2 renders Base. It does not extend Base or StyledBase. Its styles are not necessary before/after StyledBase. */
const Button2 = styled(Base, {
color: "red"
});
Oh yea! I think we can order style injection based on creation. Would this ultimately render what you expect? /* StyledBase is created first. StyledBase styles will be ordered first. StyledBase renders "button". */
const StyledBase = styled("button", {
display: "inline-flex",
color: "inherit"
});
/* Base renders StyledBase. Base does not extend StyledBase. */
const Base = (props) => {
return <StyledBase {...props} />;
};
/* Button1 is created second. Button1 extends StyledBase. Button1 styles will overwrite StyledBase. */
const Button1 = styled(StyledBase, {
color: "red"
});
/* Button2 is created third. Button2 renders Base. Button2 styles will overwrite Button1 and StyledBase. */
const Button2 = styled(Base, {
color: "red"
}); I originally thought this issue might be related to our "order by creation" issue, but then I saw, based on the reproduction, that it was more a side-effect. Perhaps we should reopen this. I bet the team would be in favor! I feel I was a little stuffy in my last response, anyway (sorry)! I’d like your thoughts, but the new presumption would be that we want the CSS to be ordered the way it was originally created. |
Yes, I think ordering styles based on creation should solve this. I'd be happy to test it out if it's necessary. |
I noticed what I think is the same issue with core: https://codesandbox.io/s/priceless-chatterjee-cpqij I expected that if the Fela have a dedicated section on this issue explaining why you have to pass your "rule"s through a util to ensure specificity: https://fela.js.org/docs/latest/advanced/combined-rules#problem-order-matters Seems to be the same issue? I raised this previously with @peduarte and he had an API concern regarding the proposed solution for ppl who want to compose multiple class names in the component but I’d personally be okay with always doing the following in that case: // compose multiple upfront
const text = css(fontSize, color, {});
function Text(props) {
return (
<span
{...props}
className={text({ className: props.className )}
/>
);
} It ensures as much as possible is statically composed at least, since the proposed solution means styles wouldn't be known until runtime I assume? I’m stumped for other ways this could be solved tho 😬 @jonathantneal If we ordered the CSS the way it was originally created, wouldn't it still break in cases like this? https://codesandbox.io/s/wonderful-sutherland-jtebo |
Hi everyone, we're still evaluating this issue — but it may take a rather fundamental change in how Stitches interfaces with CSS classes to make this possible. It likely won't be a quick fix. So please keep using workarounds for now and we'll keep you updated. @tigranpetrossian and anyone that mentioned expecting this to work because you come from SC/Emotion, can you put together a quick example of exactly what you were doing? Would it look like this?
|
Hi @StephenHaney, Thanks so much for keeping this on the radar. Your example is exactly what I would put together. This has proven the most useful in cases where we extend a component with more domain-specific variants like in my example above |
I was pointed here from discord, and just for posterity's sake I'll copy over some of my message and the example case I put together. Hopefully more examples are helpful! I believe there was a change in 0.2.0 with the shift to CSSOM that caused some changes in the ordering of styles. The general problem is when you have a composition chain of components like As far as I can tell this is because I made a demo of this here: https://codesandbox.io/s/jqbgk?file=/src/App.js. If you uncomment the first |
Looking at the docs, in this first example const BaseButton = styled('button', {
// I added these styles for demonstration.
backgroundColor: 'black',
borderRadius: '10px',
color: 'white',
});
const CheckoutButton = styled(BaseButton, {
borderRadius: 0,
backgroundColor: 'hotpink',
color: 'white',
'&:hover': {
backgroundColor: 'deeppink',
},
}); The I tried creating different |
@mikeytown19 I updated your example here: https://codesandbox.io/s/adoring-perlman-1dvnh?file=/src/App.tsx:956-966 It's looking good to me. What am I missing? |
What about this ? |
@frgarciames it's a known bug and @hadihallak is working on it as we speak 👀 |
Hey @frgarciames I just had a quick look at your sandbox and I think the behavior you're seeing is correct and isn't related to the bug reported in this issue. You seem to have a `kind="primary" variant that's giving the H2 the red color from the css custom property and variants win over base styles even when extending so you're gonna have to not trigger the default variant or override the styles in the wrapping component. Screenshot of the variant I'm talking about: t |
Hi @hadihallak, so the only way to have a component with default variants and override some properties is adding Edit: But adding css property to the component it overrides that property. Shouldn't be the same behaviour with styled function ? |
@frgarciames Taking a second look here, @hadihallak is correct. This isnt related. Can you open up a new issue about this so we dont add noise to this one? And we can discuss further. Alternatively, join our Discord and post a message in #help and I'll start a thread there. |
Hi @peduarte, I think you mentioned the team is working on the Stitches style injection issues. Just wanted to share a few thoughts with regards to StyleX's atomic strategy, which I really like. cc: @hadihallak
Here's an example of multi-level /* StyledBase renders "button". */
const StyledBase = styled("button", {
display: "inline-flex",
color: "inherit"
});
/* Base renders StyledBase and passes through the className prop */
const Base = (props) => {
return <StyledBase {...props} />;
};
/* Button1 renders StyledBase. Button1 styles will overwrite StyledBase. */
const Button1 = styled(StyledBase, {
color: "red"
});
/* Button2 renders Base.
Button2 styles are passed to Base in the form of atomic classes as part of the className prop.
Base passes the atomic classes through to StyledBase which combines them with its own styles.
Button2 styles will overwrite StyledBase. */
const Button2 = styled(Base, {
color: "red"
}); |
This is now fixed in #875 and is pending release so i'm gonna keep the issue open until we release it publicly |
Thank you so much for addressing this. Deleting all the |
@tigranpetrossian Thanks a lot for your patience and the simple reproduction of the issue🙏 |
This is now fixed in a release candidate under the version 1.2.6-0 under a canary tag. Please, don't hesitate in providing us with any feedback regarding this release. very much appreciated 🙏 |
Just got around testing this and looks like it works when wrapping regular functional components with Here's a sandbox with reproduction |
Oh right, sorry i missed that. |
Published under |
@hadihallak will this new behavior be available to |
I am suffering from what I think is the same bug in v1.2.8. Edit: I experimented a bit, and the CodeSandbox seems to reproduce this with a basic style composition. This seems beyond the immediate situation in this ticket, but I'm wondering if I'm missing someting (?). Reproduction: https://codesandbox.io/s/determined-almeida-wbizik?file=/src/App.js I'm not sure if this is 100% reproducible (perhaps it's flaky?), so a screenshot is included below. I am on Linux Firefox 99.0.1. |
Bug report
Description
Styles are applied in incorrect order depending on how styled component are composed.
Things get even more unpredictable when using Foo & Bar on the same view. Depending on the order of Foo & Bar the styles might or might not apply correctly, see reproduction below.
To Reproduce
https://codesandbox.io/s/nifty-blackburn-m792w?file=/src/App.js
Expected behavior
When using
styled(Foo, styleObject)
, rules defined instyleObject
should overrideFoo
's existing styles.System information
The text was updated successfully, but these errors were encountered: