Crates.io | classes |
lib.rs | classes |
version | 1.0.0 |
source | src |
created_at | 2023-04-15 19:21:17.087872 |
updated_at | 2023-04-15 19:21:17.087872 |
description | Dependency-free macro that simplifies the process of building class strings for DOM elements |
homepage | |
repository | https://github.com/sparten11740/classes |
max_upload_size | |
id | 840122 |
size | 5,959 |
classes
is a lightweight and dependency-free macro that simplifies the process of building class strings for DOM
elements. It accepts a variable number of arguments and combines them into a single class string.
This macro is designed after the popular classnames
npm package, which is commonly used in React and other frameworks.
You can supply string types or types that can be transformed into a string to the macro:
Option<String>
/ Option<&str>
will use the inner value if the option is Some
, and ignore the option if
it's None
String
/ &str
will be applied as isstring_expr => bool_expr
will use the string_expr
when bool_expr
evaluates to trueUsing the Classes macro can simplify your code by reducing the boilerplate needed to build class strings.
use classes::classes;
fn main() {
let optional = Some("lumos");
let is_night = true;
let class = classes!["hogwarts", optional, "hogwarts--at-night" => is_night, "wingardium-leviosa" => false];
println!("{class}"); // => 'hogwarts lumos hogwarts--at-night'
}
use classes::classes;
pub fn Button<'a>(cx: Scope<'a, ButtonProps<'a>>) -> Element {
let class = classes!["button", "button--disabled" => cx.props.disabled, cx.props.class];
cx.render(rsx! {
button { class }
})
}