toggle
Briefly
Section titled “Briefly”Core style generator function
to handle toggle conditions.
1. Type definition
Section titled “1. Type definition”;(toggleVariants: { truthy: StyleType; falsy: StyleType; base?: StyleType }) =>
ToggleStyler<StyleType, StyleLiteral>
A ToggleStyler
instance has the following methods:
class(condition: boolean, ...extraClassList)
: Returns theclassName
string.style(condition: boolean, ...extraStyles)
: Returns the style object.compose(...styles)
: Returns a new, composedToggleStyler
instance.
2. Spec
Section titled “2. Spec”tw.toggle({
base: baseStyle,
truthy: truthyStyle,
falsy: falsyStyle,
})
Parameter: base
(optional)
Section titled “Parameter: base (optional)”- type:
Tailwindest
- usage: common style for
true
andfalse
condition
Parameter: truthy
Section titled “Parameter: truthy”- type:
Tailwindest
- usage: if condition is
true
, it will applied.
Parameter: falsy
Section titled “Parameter: falsy”- type:
Tailwindest
- usage: if condition is
false
, it will applied.
3. Returns
Section titled “3. Returns”class()
Section titled “class()”Briefly
Section titled “Briefly”Returns className
string
based on the boolean condition.
const themeBtn = tw.toggle({
base: { padding: "p-2" },
truthy: { backgroundColor: "bg-black" },
falsy: { backgroundColor: "bg-white" },
})
const lightMode = themeBtn.class(false) // "p-2 bg-white"
const darkMode = themeBtn.class(true) // "p-2 bg-black"
style()
Section titled “style()”Briefly
Section titled “Briefly”Returns input styleSheet
object
based on the boolean condition.
const themeBtn = tw.toggle({
base: { padding: "p-2" },
truthy: { backgroundColor: "bg-black" },
falsy: { backgroundColor: "bg-white" },
})
const lightStyle = themeBtn.style(false) // { padding: "p-2", backgroundColor: "bg-white" }
const darkStyle = themeBtn.style(true) // { padding: "p-2", backgroundColor: "bg-black" }
compose()
Section titled “compose()”Briefly
Section titled “Briefly”Compose additional styles into the base
style and returns a new ToggleStyler
instance.
The functionality of compose is same as style’s compose.