🍭MUI Global styleOverrides

TSS Support MUI Global style overrides from createTheme out of the box. Previously in material-ui v4 it was: global theme overrides.

By default, however, only the theme object is passed to the callbacks, if you want to pass the correct props, and a specific ownerState have a look at the following example:

MyComponent.tsx
import { tss } from "tss-react/mui";

export type Props = {
    className?: string;
    classes?: Partial<ReturnType<typeof useStyles>["classes"]>;
    lightBulbBorderColor: string;
}

function MyComponent(props: Props) {

    const { className } = props;

    const [isOn, toggleIsOn] = useReducer(isOn => !isOn, false);

    const { classes, cx } = useStyles({ 
        muiStyleOverridesParams: { 
            props, 
            "ownerState": { isOn } 
        }
    });

    return (
        <div className={cx(classes.root, className)} >
            <div className={classes.lightBulb}></div>
            <button onClick={toggleIsOn}>{`Turn ${isOn?"off":"on"}`}</button>
            <p>Div should have a border, background should be white</p>
            <p>Light bulb should have black border, it should be yellow when turned on.</p>
        </div>
    );

}

const useStyles = tss
    .withName("MyComponent")
    .create({
        root: {
            border: "1px solid black",
            width: 500,
            height: 200,
            position: "relative",
            color: "black"
        },
        lightBulb: {
            position: "absolute",
            width: 50,
            height: 50,
            top: 120,
            left: 500/2 - 50,
            borderRadius: "50%"
        }
    });

Declaration of the theme:

Usage of the component:

Result:

You can see the code here and it's live here (near the bottom of the page).

Last updated

Was this helpful?