v4
Search…
⌃K
Links
🆘

Fix broken styles after upgrading to MUI v5 with TSS

You upgraded to MUIv5 using tss-react but the somme styles doesn't apply the same way they uses to?
Setting up emotion like that will probably fix your issues:
Create React App, Vite, and other SPA frameworks
Next.js
import React from "react";
import ReactDOM from "react-dom/client";
import { CacheProvider } from "@emotion/react";
import { TssCacheProvider } from "tss-react";
import createCache from "@emotion/cache";
import App from "./App";
​
const muiCache = createCache({
"key": "mui",
"prepend": true
});
​
const tssCache = createCache({
"key": "tss"
});
​
ReactDOM.createRoot(document.getElementById("root")!).render(
//NOTE: Don't use <StyledEngineProvider injectFirst/>
<CacheProvider value={muiCache}>
<TssCacheProvider value={tssCache}>
<App />
</TssCacheProvider>
</CacheProvider>
);

pages/_app.tsx

import Head from "next/head";
import App from "next/app";
import { createEmotionSsrAdvancedApproach } from "tss-react/next";
import { TssCacheProvider } from "tss-react";
​
const {
augmentDocumentWithEmotionCache: augmentDocumentWithEmotionCache_mui,
withAppEmotionCache: withAppEmotionCache_mui
} = createEmotionSsrAdvancedApproach({ "key": "mui", "prepend": true });
​
export { augmentDocumentWithEmotionCache_mui };
​
const {
augmentDocumentWithEmotionCache: augmentDocumentWithEmotionCache_tss,
withAppEmotionCache: withAppEmotionCache_tss
} = createEmotionSsrAdvancedApproach({ "key": "tss" }, TssCacheProvider);
​
export { augmentDocumentWithEmotionCache_tss };
​
export default withAppEmotionCache_mui(withAppEmotionCache_tss(App));

pages/_document.tsx

//pages/_document.tsx
​
import Document from "next/document";
import {
augmentDocumentWithEmotionCache_mui,
augmentDocumentWithEmotionCache_tss
} from "./_app";
​
augmentDocumentWithEmotionCache_mui(Document);
augmentDocumentWithEmotionCache_tss(Document);
​
export default Document;
If your issues are fixed by doing this, please open an issue about it so I can address the root cause of the problem by issuing a PR on the MUI repo.

Why and how does it work?

In theory, when TSS and MUI uses the same emotion cache, the styles that you provide via className or classes should always take priority over MUI's default styles.
It's almost always the case but in some edge cases involving media queries on the MUI side, it isn't.
You always have the option to artificially increase the specificity with && or using !important but if you are just upgrading to MUI v5 you probably don't want to spend hours troubleshooting issues one by one.
By explicitly telling MUI to use one cache and TSS to use another and by making sure the MUI styles are injected before in the <head /> (prepend: true) you ensure that TSS-generated styles always overwrite MUI's default styles.