1 unstable release
0.1.0 | Dec 27, 2024 |
---|
#2012 in Web programming
117 downloads per month
7KB
82 lines
clsx-r
A Rust macro utility for conditionally constructing strings, primarily used for CSS class names. Inspired by the JavaScript clsx package.
Installation
Add this to your Cargo.toml
:
[dependencies]
clsx-r = "0.1.0"
Usage
use clsx_r::clsx;
fn main() {
// Basic usage
let classes = clsx!("foo", "bar"); // => "foo bar"
// With conditions
let is_active = true;
let is_disabled = false;
let classes = clsx!(
"btn",
"primary",
"active" => is_active,
"disabled" => is_disabled
); // => "btn primary active"
// With dynamic values
let dynamic_class = "special";
let classes = clsx!(
"base",
dynamic_class,
"highlighted" => true
); // => "base special highlighted"
}
Features
- Simple string concatenation
- Conditional class names
- Supports static and dynamic strings
- Filters out empty strings automatically
- Zero dependencies
API
The clsx!
macro accepts:
-
Simple string literals or expressions:
clsx!("class1", "class2")
-
Conditional classes using the
=>
syntax:clsx!("class1" => condition, "class2" => other_condition)
-
Mixed usage:
clsx!("static", dynamic_var, "conditional" => condition)
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.