tss - the Modern API
useStyles
import { useStyles } from "tss-react" // or "tss-react/mui";
function MyComponent(){
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 (
<div className={cx(css({ backgroundColor: "black" }), "myClassName")}>
<span className={css({ color: "red" })}>
Hello World
</span>
</div>
);
}tss.create(...)
tss.create(...) enables to separate the definition of styles from their usage.
tss.withParams()
tss.withParams<O>() enables to dynamically generate styles based on parameters.
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.
tss.withNestedSelectors<"a" | "b" | "c">()
Enables to writes styles that reference each other.
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:
src/MyComponent.tsx:
Last updated
Was this helpful?
