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
  1. API References

withStyles

PreviousmakeStyles -> useStylesNext<GlobalStyles />

Last updated 2 years ago

Was this helpful?

It's like but type safe by design.

IMPORTANT NOTICE: when you get red squiggly lines.

You can pass as first argument any component that accept a className props:

function MyComponent(props: { className?: string; colorSmall: string }) {
    return (
        <div className={props.className}>
            The background color should be different when the screen is small.
        </div>
    );
}

const MyComponentStyled = withStyles(
    MyComponent, 
    (theme, props) => ({
        "root": {
            "backgroundColor": theme.palette.primary.main,
            "height": 100
        },
        "@media (max-width: 960px)": {
            "root": {
                "backgroundColor": props.colorSmall
            }
        }
    })
);
import Button from "@mui/material/Button";

const MyStyledButton = withStyles(Button, {
    "root": {
        "backgroundColor": "grey"
    }
    "text": {
        "color": "red"
    },
    "@media (max-width: 960px)": {
        "text": {
            "color": "blue"
        }
    }
});

It's also possible to start from a builtin HTML component:

const MyAnchorStyled = withStyles("a", (theme, { href }) => ({
    "root": {
        "border": "1px solid black",
        "backgroundColor": href?.startsWith("https")
            ? theme.palette.primary.main
            : "red"
    }
}));

You can also pass a mui component like for example <Button /> and you'll be able to overwrite (it uses the classes prop).

You can experiment with those examples live , you can also run it locally with .

๐Ÿ”
every rule name of the component
here
here
yarn start_spa
the material-ui v4 higher-order component API
Don't be afraid to use as const