TSS
HomeGitHubPlayground
v3
v3
  • 🚀Why TSS
  • 🔧Setup
  • 🔍API References
    • makeStyles -> useStyles
    • withStyles
    • <GlobalStyles />
    • keyframes
    • useMergedClasses
  • 💽Cache
  • 💫Nested selectors (ex $ syntax)
  • ⚡SSR
    • Gatsby
    • Next.js
    • Other backends
  • 🦱Your own classes prop
  • 🍭MUI Theme styleOverrides
  • 🧹Detecting unused classes
  • 📦Publish a module that uses TSS
  • 🔩single-spa
  • 📲React Native
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Your own classes prop

Enable users of the components to overrides the internal styles by accepting a class props.

PreviousOther backendsNextMUI Theme styleOverrides

Last updated 2 years ago

Was this helpful?

Added in v3.6.0

Every MUI components accepts a classes props that enables you override the internal styles ().

With TSS you can easily do the same for your components, it's done by merging the internal classes and the one that might have been provided as props.

This is the new way for .

import { useMergedClasses } from "tss-react";

type Props = {
    //classes?: { foo?: string; bar?: string; };
    classes?: Partial<ReturnType<typeof useStyles>["classes"]>;
};

function MyTestComponentForMergedClassesInternal(props: Props) {
    const { classes } = useStyles({ "color": "pink" }, { props });
    //NOTE: Only the classes will be read from props, 
    //you could write { props: { classes: props.classes } } instead of { props }
    //and it would work the same. 

    return (
        <div className={classes.foo}>
            <span className={classes.bar}>
                The background should be green, the box should have a dotted
                border and the text should be pink
            </span>
        </div>
    );
}

const useStyles = makeStyles<{ color: string; }>()({
    "foo": {
        "border": "3px dotted black",
        "backgroundColor": "red"
    }
    "bar": {
        color
    }
});

//...

render(
    <MyTestComponentForMergedClassesInternal
        classes={{ "foo": css({ "backgroundColor": "green" }) }}
    />
);

🦱
see MUI's doc
Overriding styles - classes prop
Result