1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
#![forbid(missing_docs)]
//! # Tailwind Fuse
//! [<img alt="github" src="https://img.shields.io/badge/github-gaucho--labs/tailwind--fuse-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/gaucho-labs/tailwind-fuse)
//! [<img alt="crates.io" src="https://img.shields.io/crates/v/tailwind-fuse.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/tailwind-fuse)
//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-tailwind--fuse-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/tailwind-fuse)
//! [<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/gaucho-labs/tailwind-fuse/ci.yml?branch=main&style=for-the-badge" height="20">](https://github.com/gaucho-labs/tailwind-fuse/actions?query=branch%3Amain)
//!
//! Two main utils are included in this crate:
//!
//! 1. Fuse: Fuse multiple Tailwind classes, with optional conflict resolution.
//! > Inspired by [Tailwind Merge](https://github.com/dcastil/tailwind-merge)
//! 2. Variants: Compose type-safe variant classes
//! > Inspired by [Class Variance Authority](https://github.com/joe-bell/cva)
//!
//!
//! ## Installation
//!
//! Variants requires the `variant` feature to be enabled.
//!
//! #### With variant
//! ```bash
//! cargo add tailwind-fuse --features variant
//! ```
//!
//! #### Without variant
//! ```bash
//! cargo add tailwind-fuse
//! ```
//!
//! ## Usage: Fuse
//!
//! You can use [`tw_join!`] to join Tailwind classes, and [`tw_merge!`] to merge Tailwind Classes handling conflicts.
//!
//!
//! You can use anything that implements [`AsTailwindClass`]
//!
//! ```
//! use tailwind_fuse::*;
//!
//! // No conflict resolution
//! assert_eq!(
//! "flex items-center justify-center",
//! tw_join!("flex", "items-center", "justify-center")
//! );
//!
//! // Conflict resolution
//! // Right most class takes precedence
//! assert_eq!(
//! "p-4",
//! tw_merge!("py-2 px-4", "p-4")
//! );
//!
//! // Refinements are permitted
//! assert_eq!(
//! "p-4 py-2",
//! tw_merge!("p-4", "py-2")
//! );
//! ```
//!
//! You can use Options to exclude certain classes from being merged
//!
//! ```
//! use tailwind_fuse::*;
//!
//! assert_eq!(
//! "flex justify-center",
//! tw_join!("flex", (false).then_some("items-center"), (true).then_some("justify-center"))
//! )
//! ```
//!
//! ### Custom Tailwind Prefix/Separator
//!
//! Use [`merge::set_merge_options`] to set global options for [`tw_merge!`] and variant macros.
//!
//! This can only be set once. Subsequent calls will be ignored.
//!
//! ```
//! use tailwind_fuse::{*, merge::*};
//!
//! const OPTIONS: MergeOptions = MergeOptions {
//! prefix: "tw-",
//! separator: ":",
//! };
//!
//! // Before setting options, the default (no prefix) is used
//! assert_eq!(
//! "tw-bg-black tw-bg-white",
//! tw_merge!("tw-bg-black", "tw-bg-white"),
//! );
//!
//! set_merge_options(OPTIONS);
//!
//! assert_eq!(
//! "tw-bg-white",
//! tw_merge!("tw-bg-black", "tw-bg-white"),
//! );
//!
//! ```
//!
//!
//! ## 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 classes are merged in the following order, with the last class takes precedence:
//! 1. Base class from [`TwClass`]
//! 2. Base class from [`TwVariant`]
//! 3. Enum variant class from [`TwVariant`]
//! 4. Override class using [`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
//!
//! ### Struct Syntax
//! ```
//! # 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,
//! # }
//! let button = Btn {
//! size: BtnSize::Default,
//! color: BtnColor::Blue,
//! };
//!
//! assert_eq!(
//! "flex h-9 px-4 py-2 bg-blue-500 text-blue-100",
//! button.to_class()
//! );
//!
//! // Conflicts are resolved (bg-blue-500 is knocked out in favor of override)
//! assert_eq!(
//! "flex h-9 px-4 py-2 text-blue-100 bg-green-500",
//! button.with_class("bg-green-500")
//! );
//! ```
//!
//! ### Builder Syntax
//! You access the builder using the `variants` method. Every variant that is not provided will be replaced with the default variant.
//!
//! ```
//! # use tailwind_fuse::*;
//! #
//! # #[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,
//! # }
//!
//! assert_eq!(
//! "flex h-8 px-3 bg-red-500 text-red-100",
//! Btn::builder()
//! .size(BtnSize::Sm)
//! .color(BtnColor::Red)
//! .to_class()
//! );
//!
//! assert_eq!(
//! "flex h-8 px-3 text-red-100 bg-green-500",
//! Btn::builder()
//! .size(BtnSize::Sm)
//! .color(BtnColor::Red)
//! .with_class("bg-green-500")
//! );
//!
//! ```
//!
//! #### VSCode Intellisense
//!
//! You can enable autocompletion inside `#[tw()]` using the steps below:
//!
//! 1. [Install the "Tailwind CSS IntelliSense" Visual Studio Code extension](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss)
//!
//! 2. Add the following to your [`settings.json`](https://code.visualstudio.com/docs/getstarted/settings):
//!
//! ```json
//! {
//! "tailwindCSS.experimental.classRegex": [
//! ["#[tw\\\\([^\\]]*class\\s*=\\s*\"([^\"]*)\"\\)]", "\"([^\"]*)\""]
//! ]
//! }
//! ```
//!
pub use crate::core::merge;
pub use crate::core::*;
mod ast;
mod core;
#[cfg(feature = "variant")]
pub use variant::*;
#[cfg(feature = "variant")]
mod variant {
/// Used to Fuse Tailwind Classes together.
pub trait TailwindFuse {
/// Strings are not guaranteed to be single class nor free of whitespace.
fn fuse_classes(&self, class: &[&str]) -> String;
}
/// Will merge Tailwind classes and handle conflicts using [`crate::merge::tw_merge_slice`]
pub struct TailwindMerge;
impl TailwindFuse for TailwindMerge {
fn fuse_classes(&self, class: &[&str]) -> String {
crate::merge::tw_merge_slice(class)
}
}
/// Will simply join Tailwind classes together without handling conflicts
pub struct TailwindJoin;
impl TailwindFuse for TailwindJoin {
fn fuse_classes(&self, class: &[&str]) -> String {
class
.iter()
.flat_map(|s| s.split_whitespace())
.map(|s| s.trim())
.filter(|s| !s.is_empty())
.fold(String::new(), |mut acc, s| {
if !acc.is_empty() {
acc.push(' ');
}
acc.push_str(s);
acc
})
}
}
/// 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:
/// - [`TwClass`] base class
/// - [`TwVariant`] base class
/// - [`TwVariant`] enum variant class
/// - Override class with `with_class`
///
/// # Example
///
/// ```rust
/// 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");
/// ```
///
pub use tailwind_fuse_macro::TwClass;
/// Represents a customizable property (variant) of a UI element.
/// Each variant must be an enum with a default case.
///
/// Use `.to_class()` to get the class for the variant and `.with_class()` to append a class.
///
/// # Example
///
/// ```rust
/// use tailwind_fuse::*;
///
/// #[derive(TwVariant, Debug)]
/// // Optional base class
/// #[tw(class = "hover:brightness-50")]
/// enum BtnColor {
/// #[tw(default, class = "bg-blue-500 text-blue-100")]
/// Default,
/// #[tw(class = "bg-red-500 text-red-100")]
/// Red,
/// }
///
/// assert_eq!("hover:brightness-50 bg-blue-500 text-blue-100", BtnColor::Default.as_class());
/// assert_eq!("hover:brightness-50 bg-red-500 text-red-100", BtnColor::Red.as_class());
/// ```
///
pub use tailwind_fuse_macro::TwVariant;
/// A trait to convert a type into a Tailwind class.
/// Implemented automatically for usages of [`TwClass`] and [`TwVariant`].
pub trait IntoTailwindClass {
/// Convert the type into a Tailwind class.
fn to_class(&self) -> String;
/// Append to the class (with override precedence) and return the new class.
fn with_class(&self, class: impl AsRef<str>) -> String;
}
/// Converts a type into it's builder.
/// Automatically implemented for usages of [`TwClass`].
pub trait IntoBuilder {
/// The builder type.
type Builder;
/// Get a builder instance
fn builder() -> Self::Builder;
/// Convert the instance into the builder.
fn into_builder(self) -> Self::Builder;
}
}