Skip to content
GitHub

Basic styling

  • Define reusable style using style function
  • Define inline styles using def function
  • Define conditional styles using toggle, rotary and `variants functions

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

tw.style returns a styler instance with three main methods:

  1. class(): returns className string
  2. style(): returns styleSheet object
  3. compose(...styles): returns a new styler instance with composed styles.

The compose function allows you to overwrite styles without the need for a className merging library like tailwind-merge.

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>