Derive Macro tailwind_fuse::TwClass

source ·
#[derive(TwClass)]
{
    // Attributes available to this derive:
    #[tw]
}
Expand description

Derives a class for use with Tailwind CSS in Rust components.

Allows building components with first-class support for Tailwind.

Defaults to using [crate::tw_merge()] to resolve conflicts.

Resolves conflicts using the following merge order:

§Example

use tailwind_fuse::*;

#[derive(TwClass, Debug)]
// Optional base class.
#[tw(class = "flex")]
struct Btn {
    size: BtnSize,
    color: BtnColor,
}

#[derive(TwVariant, Debug)]
enum BtnSize {
    #[tw(default, class = "h-9 px-4 py-2")]
    Default,
    #[tw(class = "h-8 px-3")]
    Sm,
    #[tw(class = "h-10 px-8")]
    Lg,
}

#[derive(TwVariant, Debug)]
enum BtnColor {
    #[tw(default, class = "bg-blue-500 text-blue-100")]
    Blue,
    #[tw(class = "bg-red-500 text-red-100")]
    Red,
}

let btn = Btn { size: BtnSize::Default, color: BtnColor::Blue };
assert_eq!(btn.to_class(), "flex h-9 px-4 py-2 bg-blue-500 text-blue-100");

let btn_variant = Btn::builder().color(BtnColor::Red).to_class();
assert_eq!(btn_variant, "flex h-9 px-4 py-2 bg-red-500 text-red-100");