https://github.com/sparten11740/classes
Dependency-free rust macro that simplifies the process of building class strings for DOM elements for usage in frameworks like Dioxus
https://github.com/sparten11740/classes
classnames dioxus dom helper macro rust web
Last synced: over 1 year ago
JSON representation
Dependency-free rust macro that simplifies the process of building class strings for DOM elements for usage in frameworks like Dioxus
- Host: GitHub
- URL: https://github.com/sparten11740/classes
- Owner: sparten11740
- Created: 2023-04-15T19:22:33.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-23T06:16:33.000Z (about 3 years ago)
- Last Synced: 2025-02-28T03:26:56.261Z (over 1 year ago)
- Topics: classnames, dioxus, dom, helper, macro, rust, web
- Language: Rust
- Homepage: https://crates.io/crates/classes
- Size: 7.81 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://github.com/sparten11740/classes/actions/workflows/ci.yml)
# classes
`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.
## Usage
You can supply string types or types that can be transformed into a string to the macro:
- `Option` / `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 is
- The special syntax `string_expr => bool_expr` will use the `string_expr` when `bool_expr` evaluates to true
Using the Classes macro can simplify your code by reducing the boilerplate needed to build class strings.
### Example
```rust
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'
}
```
### Dioxus Example
```rust
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 }
})
}
```