style
Briefly
Core style generator function, with no variants.
1. Type Definition
interface StyleGeneratorStyle<StyleType extends NestedObject> {
class: string
style: StyleType
compose: (...styles: StyleType[]) => {
class: string
style: StyleType
}
}
declare const createTools: <StyleType extends NestedObject>() => {
style: (style: StyleType) => StyleGeneratorStyle<StyleType>
}2. Spec
Usage
tw.style(style)Parameter: style
- type:
Tailwindest - usage: Define tailwind style
Example
const center = tw.style({
display: "flex",
alignItems: "items-center",
justifyContent: "justify-center",
})3. Returns
class
Briefly
Returns className string
Usage
tw.style(style).classExample
const centerClass = center.class
// flex items-center justify-centerstyle
Briefly
Returns input styleSheet object
Usage
tw.style(style).styleExample
const centerStyle = center.style
/*
{
display: "flex",
alignItems: "items-center",
justifyContent: "justify-center",
}
*/compose
Briefly
Compose Array of styleSheet object into single styleSheet.
Usage
tw.style(style).compose(...styleSheets)The order of ...styleSheets is very important.
If styleSheet is placed behind in ...styleSheets, it has higher priority
in styling.
const paddingWillBe4 = tw
.style({
padding: "p-0.5",
})
.compose(
{
// first compose target
padding: "p-1",
},
{
// second compose target
padding: "p-2",
},
{
// ✅ last compose target
padding: "p-4",
}
)
const result = paddingWillBe4.class
// p-4⚠️
compose will change styleSheets permanently.
Parameter: ...styleSheets
- type:
Array<Tailwindest> - usage: compose
...styleSheetsinto onestyleSheetobject
Example
- Make
styleinstances
const center = tw.style({
display: "flex",
alignItems: "items-center",
justifyContent: "justify-center",
})
const border = tw.style({
borderWidth: "border",
borderColor: "border-gray-50",
borderRadius: "rounded-sm",
})- Compose all
styleSheet
const box = tw
.style({
backgroundColor: "bg-white",
})
.compose(center.style, border.style)- Get composed result
const boxClass = box.class
// "flex items-center justify-center border border-gray-50 rounded-sm bg-white"
const boxStyle = box.style
/*
{
display: "flex",
alignItems: "items-center",
justifyContent: "justify-center",
borderWidth: "border",
borderColor: "border-gray-50",
borderRadius: "rounded-sm",
backgroundColor: "bg-white",
}
*/