Tailwind Fuse
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
Installation
Variants requires the variants feature to be enabled.
With variants
Without variants
Usage: Fuse
You can use [tw_join!] to join Tailwind classes, and [tw_merge!] to merge Tailwind Classes handling conflicts.
use *;
// No conflict resolution
// "flex items-center justify-center"
let joined_class = tw_join!;
// 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!;
// Conflict resolution
// Right most class takes precedence
// p-4
let merged_class = tw_merge!;
// Refinements are permitted
// p-4 py-2
let merged_class = tw_merge!;
Usage: Variants
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 *;
// Your Component Type
// Optional base class
// Variant for size
// Variant for color
You can now use the Btn struct to generate Tailwind classes, using builder syntax, or using the struct directly
Struct Syntax
let button = Btn ;
// 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;
Builder Syntax
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 = variant
.size
.color
.to_class;
// h-8 px-3 text-red-100 bg-green-500
let class = variant
.size
.color
.with_class;
VSCode Intellisense
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: