import { useStyles }ย from"tss-react"// or "tss-react/mui";functionMyComponent(){const { css,//<- Like the css function of @emotion/css cx //<- Like the cx function of @emotion/css, also known as clsx. It's smarter though, classes that comes last take priority.
} =useStyles();return ( <divclassName={cx(css({ backgroundColor:"black" }),"myClassName")}> <spanclassName={css({ color:"red" })}> Hello World </span> </div> );}
tss.create(...)
tss.create(...) enables to separate the definition of styles from their usage.
tss.withParams<O>() enables to dynamically generate styles based on parameters.
import { useState }ย from"react";import { tss }ย from"tss-react";functionMyComponent(){const [clickCount,setClickCount] =useState(0);const { cx,classes } =useStyles({ isClicked: clickCount >0 });return ( <divclassName={cx(classes.root,"myClassName")}onClick={() =>setClickCount(clickCount +1)} > <spanclassName={classes.text}> {/* The text is red when the component has been clicked at least once */} Hello World </span> </div> );}constuseStyles= tss.withParams<{ isClicked:boolean; }>().create(({ isClicked }) => ({ root: { backgroundColor:"black", }, text: { color: isClicked ?"red":"blue", } }));
tss.withName(name)
Providing a name is useful when you open the debugger and want to quickly find the useStyles that generated a class name.
import { tss }ย from"tss-react";functionMyComponent(){ ... }constuseStyles= tss.withName("MyComponent")// or .withName({ MyComponent }), if you pass an object, the first key is used as the name..create(...);
tss.withNestedSelectors<"a" | "b" | "c">()
Enables to writes styles that reference each other.
import { tss }ย from"tss-react";exportfunctionMyComponent() {const { classes,cx } =useStyles();return ( <divclassName={classes.root}> <divclassName={classes.child}> The Background is green when the mouse is hover the parent. </div> <divclassName={cx(classes.child,classes.small)}> The Background is green when the mouse is hover the parent. I am smaller than the other child. </div> </div> );}constuseStyles= tss.withNestedSelectors<"child"|"small">().create(({ classes }) => ({ root: { padding:30, [`&:hover .${classes.child}`]: { backgroundColor:"green" } }, small: {}, child: { border:"1px solid black", height:50, [`&.${classes.small}`]: { height:30 } } }));
The render of the above code
WARNING: In SSR setups you must provide a unique name when using nested selectors. tss.withName("SomethingUnique").withNestedSelectors<...>().create(...)
createTss()
createTss() enables to create a tss instance with a custom context.
The context will be passed as argument to the function you provide to tss.create(...).
Let's see an example with a dark mode context:
src/tss.ts:
import { createContext }ย from"react";import { createTss } from"tss-react";constcontextIsDarkMode=createContext<boolean|undefined>(undefined);exportfunctionuseContextIsDarkMode(){constisDarkMode=useContext(contextIsDarkMode);if(isDarkMode ===undefined){thrownewError("You must wrap your app with a <Provider> of contextIsDarkMode"); }return isDarkMode;}exportconstDarkModeProvider=contextIsDarkMode.Provider;exportconst { tss } =createTss({useContext:functionuseContext(){constisDarkMode=useContextIsDarkMode();return { isDarkMode }; }});