Skip to content
GitHub

toggle

Core style generator function to handle toggle conditions.

;(toggleVariants: { truthy: StyleType; falsy: StyleType; base?: StyleType }) =>
    ToggleStyler<StyleType, StyleLiteral>

A ToggleStyler instance has the following methods:

  • class(condition: boolean, ...extraClassList): Returns the className string.
  • style(condition: boolean, ...extraStyles): Returns the style object.
  • compose(...styles): Returns a new, composed ToggleStyler instance.
tw.toggle({
    base: baseStyle,
    truthy: truthyStyle,
    falsy: falsyStyle,
})
  • type: Tailwindest
  • usage: common style for true and false condition
  • type: Tailwindest
  • usage: if condition is true, it will applied.
  • type: Tailwindest
  • usage: if condition is false, it will applied.

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"

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 additional styles into the base style and returns a new ToggleStyler instance.

The functionality of compose is same as style’s compose.