🆘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?

You can fix the indivudual problems increasing specificity with && but it's a very time consuming process.

Setting up emotion like that will probably fix all your issues:

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"

    //NOTE: Don't use <StyledEngineProvider injectFirst/>
    <CacheProvider value={muiCache}>
        <TssCacheProvider value={tssCache}> 
            <App />

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.

Last updated