-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Class precedence #17
Comments
I tried adding a custom cache just for the makeStyles Wrapper:
While the global default one just keeps the key "css" and is set up like defined in the MUIv5 nextjs example. That way, the custom classes there actually have priority over the MUI ones however it still breaks the precedence of a Before in MUIv4 with makeStyles: Edit: So I just verified, that only comes up when using a different cache. Using the same cache, I have the original error of |
Sorry if I am missing the point, I haven't read your message in detail. I will do if the following code do not solve your problem:
export const cache= createCache({ key: "my-prefix-key" })
const { makeStyles } = createMakeStyles({
useTheme,
cache
});
import { createDocument } from "tss-react/nextJs";
import { getCache } from "tss-react/cache";
import { cache } from "../shared/makeStyles";
const { Document } = createDocument({ "caches": [ getCache(), cache ] });
export default Document;
import { getCache } from "tss-react/cache";
export default function Home() {
return (
<>
<Head>
//...
</Head>
<CacheProvider value={getCache()}>
<MuiThemeProvider theme={theme}>
<CssBaseline />
<Root />
</MuiThemeProvider>
</CacheProvider>,
</>
);
} |
Hey @garronej, thank you for the reply, yes that is basically what I'm doing with a few exceptions:
I will try to create a minimal example on codesandbox to demonstrate the issue. |
Thank you, sorry it didn't do it for you. You can start from the test app located at you can run it by doing: git clone https://github.com/garronej/tss-react
cd tss-react
yarn
yarn build
yarn start_ssr You can fork, modify the example in order to show me what isn't working as expected. |
Oh, Im seeing this a bit too late, I already crated a quick example here: |
Ok thanks for taking the time to create a sandbox, I see that:
I have to fix that! For now this is the best workaround I can offer: https://stackblitz.com/edit/nextjs-5bjvvn?file=src%2Fcomponents%2FsomeButton.tsx |
If you feel you can do it, PR are welcome. |
Thanks for the quick response! Happy to submit a PR once I find out how to control the order of styles here :) |
I just started wondering, since Material UI internally uses
However, this doesn't seem to be the case, since the text would properly render in orange,.
And realized that indeed Feel free to close this, or maybe keep it open since this would be the first starting point for users coming from v4 and expecting to be able to continue styling MUI components the same way they did before. I will search for any mention of that change in behavior in the MUI repo. |
Update: I got on the wrong track here, order of css classes was not the issue. The emotion styles I define in some component would need to come AFTER the ones generated from the MUI component rendered INSIDE that component in the created CSS if I'm not mistaken. Although, simply using a separate emotion cache and making all classes there come after the MUI ones / have priority over them does not seem to be the full solution either, since as described above in #17 (comment) that could still break order, with a custom |
Ok I thee where where a lot of back and forth. Sorry I wasn't able to to go through tss-react issues the past few days. I will have a carfull look at this as soon as I can. |
Ok I think it's solved, let me publish a fix |
Hi @janus-reith, Do not esitate to reopen if you still have issues but it have been carefully tested |
See the diff of the README to know how to update your configuration to make things work as expected. Thank you for reporting. |
I, however, opened another issue on the material-ui repo following up yours. |
Hi @janus-reith, |
Hey @garronej, thanks, yes it seems to be the solution that I also found in the meantime and described above. That one issue however still remains unsolved if I'm not mistaken:
Since emotion will derive the priority of styles based on where in the three they are defined, that still doesn't work. Now, I have some outer component that actually uses this Button, lets call it In v4 that would work fine. While easy to work around this in a new project, this can make things quite hard with an existing codebase and is a dealbreaker for a simple transition from v4 to v5 if that pattern was used before. One workaround that can help: On the outer class defined in
Looks a bit weird, but probably better than using In a bigger project, the workflow for this can be like this:
This could cover usual usecases, but certainly not all of them. There could be situations where you have composition of multiple levels of nested styles, this could probably be covered using "&&&", "&&&&" and so on, you get the idea. |
Tanks for rising this concern.
I agree, this is a problem. We want to make the migration as smooth as possible. Maybe if material-ui devs address this issue we could, by moving back to using a unique cache, make it work like it used to but I don't see any other way from where I stand. What I would recommend doing is using tss's export type Props = {
className?: string;
children: ReactNode;
};
export function MyButton(props: Props){
const { className, children } = props;
const { classes, cx }= useStyles();
return (
<MuiButton classes={{
...classes,
"root": cx(classes.root, classeName)
}}>
{children}
<MuiButton>
);
} I don't know how common is the pattern you describe. Maybe I should add some instruction in the readme about that. |
Yes, thats the same impression I got.
Interesting, I wasn't aware of that. I wonder if it could be wrapped in some clever way, to avoid needing to do this per component, but so far none comes to mind.
That could be helpful - While I'm not sure how many people do it like that usually, passing a className as a prop doesn't seem to uncommon. |
Hi @janus-reith, correcty me if I am wrong but this doesn't seem to be actuate. sandbox |
Hey @garronej , I can't really make sense of that sandbox, it only shows one h1 and two h2 each receiving a class built with |
Sorry I didn't save: https://codesandbox.io/s/makestyles-forked-txfry?file=/src/index.js |
Ah, I just read your issue in the MUI repo too and figured you were referring to the same thing. I took your Sandbox and added another Button that demonstrates the issue: As far as I understand things, you won't be able to recreate the same logic with MUI v5 and tss-react currently - The definiton of the cyan inside the button comes later, so that one is used instead of the pink background from |
The example you gave, behave exactly the same with mui v5 and And if I move the definition of |
Sure, that's expected
This is confusing, I'm struggling to puzzle the pieces together, since with my earlier testings, this did not work properly. I think this was due to the two separate Caches being necessary, is that not the case anymore? I might need to follow up more on the changes in the latest release then. |
In the latest release of |
Context:
makeStyles
with tss-reactI'm used to passing classes to MUI components in a
classes
object in order to overwrite styles.However, the Material UI default classes always seems to be appended later, causing the custom styles to be ignored.
For example:
The resulting css will list that green background, but it will be overwritten by the own MUI Button styling, since that has a background of the primary theme color.
The custom
makeStyles
classes should come after/have priority over the default ones of that component.The text was updated successfully, but these errors were encountered: