Two main utils are included in this crate:
- Fuse: Fuse multiple Tailwind classes, with optional conflict resolution.
Inspired by Tailwind Merge
- Variants: Compose type-safe variant classes
Inspired by Class Variance Authority
Variants requires the variants
feature to be enabled.
cargo add tailwind-fuse --features variants
cargo add tailwind-fuse
You can use [tw_join!
] to join Tailwind classes, and [tw_merge!
] to merge Tailwind Classes handling conflicts.
use tailwind_fuse::*;
// No conflict resolution
// "flex items-center justify-center"
let joined_class = tw_join!("flex items-center", "justify-center");
// You can use Option to handle conditional rendering
// You can pass in &str, String, Option<String>, or Option<&str>
// "text-sm font-bold"
let classes = tw_join!(
"text-sm",
Some("font-bold"),
None::<String>,
Some("ring").filter(|_| false),
Some(" "),
"".to_string(),
);
// Conflict resolution
// Right most class takes precedence
// p-4
let merged_class = tw_merge!("py-2 px-4", "p-4");
// Refinements are permitted
// p-4 py-2
let merged_class = tw_merge!("p-4", "py-2");
Useful for building components with first class support for tailwind. By default, conflicts are merged using [tw_merge()
].
Each [TwClass
] represents a UI element with customizable properties (property is a "variant"). Each variant is represented by a [TwVariant
], which must be an enum with a default case.
The merge order is, where the last class takes preferences:
- [
TwClass
] base class - [
TwVariant
] base class - [
TwVariant
] enum variant class - Override class [
IntoTailwindClass::with_class
] on the struct or builder
use tailwind_fuse::*;
// Your Component Type
#[derive(TwClass)]
// Optional base class
#[tw(class = "flex")]
struct Btn {
size: BtnSize,
color: BtnColor,
}
// Variant for size
#[derive(TwVariant)]
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,
}
// Variant for color
#[derive(TwVariant)]
enum BtnColor {
#[tw(default, class = "bg-blue-500 text-blue-100")]
Blue,
#[tw(class = "bg-red-500 text-red-100")]
Red,
}
You can now use the Btn
struct to generate Tailwind classes, using builder syntax, or using the struct directly
let button = Btn {
size: BtnSize::Default,
color: BtnColor::Blue,
};
// h-9 px-4 py-2 bg-blue-500 text-blue-100
button.to_class();
// Conflicts are resolved.
// h-9 px-4 py-2 text-blue-100 bg-green-500
button.with_class("bg-green-500");
You access the builder using the variants
method. Every variant that is not provided will be replaced with the default variant.
// h-8 px-3 bg-red-500 text-red-100
let class = Btn::variant()
.size(BtnSize::Sm)
.color(BtnColor::Red)
.to_class();
// h-8 px-3 text-red-100 bg-green-500
let class = Btn::variant()
.size(BtnSize::Sm)
.color(BtnColor::Red)
.with_class("bg-green-500");
You can enable autocompletion inside #[tw()]
using the steps below:
-
Install the "Tailwind CSS IntelliSense" Visual Studio Code extension
-
Add the following to your
settings.json
:
{
"tailwindCSS.experimental.classRegex": [
["#[tw\\\\([^\\]]*class\\s*=\\s*\"([^\"]*)\"\\)]", "\"([^\"]*)\""]
]
}