Basic styling
Overview
Section titled “Overview”- Define reusable style using
style
function - Define inline styles using
def
function - Define conditional styles using
toggle
,rotary
and `variants functions
Example 1: Define style tokens
Section titled “Example 1: Define style tokens”Extract and define common styles tokens in your components to maximize reusability.
const layout = tw.style({
// ... Core layout
})
const border = tw.style({
// ... Border styles
})
Export utility styles
export const token = {
layout,
border,
} as const
Example 2: Compose(Merge) styles
Section titled “Example 2: Compose(Merge) styles”tw.style
returns a styler instance with three main methods:
class()
: returns classNamestring
style()
: returns styleSheetobject
compose(...styles)
: returns a new styler instance with composed styles.
The
compose
function allows you to overwrite styles without the need for aclassName
merging library liketailwind-merge
.
Usage 1 - merge styleSheets
Section titled “Usage 1 - merge styleSheets”const basic = tw
.style({
alignItems: "items-start",
})
.compose(
{
alignItems: "items-end",
},
{
alignItems: "items-stretch",
}
)
// ✨ items-stretch
Usage 2 - separate util styles and compose it
Section titled “Usage 2 - separate util styles and compose it”Define design token
const container = tw.style({
// ... Container styles
})
export const token = {
container,
} as const
Compose utility styles where you need.
import { token } from "@/token"
const box = tw
.style({
display: "flex",
alignItems: "items-center",
justifyContent: "justify-center",
})
.compose(token.container.style())
.class()
const Box = ({ children }) => <div className={box}>{children}</div>