unocss-classes

HTML class builder macro with UnoCSS variant group transformer for Rust web frameworks

This crate provides a wrapper around classes! macro that additionally transforms string literals in harmony with @unocss/transformer-variant-group. It is meant to simplify building compound DOM classes in frontend Rust frameworks like Leptos / Dioxus / Yew, while also taking full advantage of UnoCSS's features.

Exports

Examples

Note that only pure string literals inside the macro's scope are transformed.

```rust uno![Some("text-(sm center)")] // doesn't work

let class = "text-(sm center)"; uno![class] // doesn't work ```

That's where the to_uno! macro comes in handy.

```rust to_uno![Some("text-(sm center)")]

let class = "text-(sm center)"; to_uno![class] ```

Leptos example

```rust use leptos::*; use unocssvariantgroup_transformer::uno;

[component]

fn App() -> impl IntoView { view! {

} // equivalent to:
Some text
}

// or as a reactive derived signal:

[component]

fn App() -> impl IntoView { let reactivecondition = createrwsignal(true); view! {

condition()]> "Some text"
} } ```

You can check out also this example Leptos app.

Dioxus example

```rust use dioxus::prelude::*; use unocssvariantgroup_transformer::uno;

pub fn App(cx: Scope) -> Element { render! {div { class: uno!["hover:(bg-gray-400 font-medium)", "font-(light mono)"], "Some text" }} } ```

Yew example

```rust use yew::prelude::*; use unocssvariantgroup_transformer::uno;

[function_component]

pub fn App() -> Html { html! {

} } ```

Using uno! macro globally

```rust

[macro_use]

extern crate yewunocsstransformer; ```

Using UnoCSS with a Rust front-end framework

You basically need to run @unocss/cli first to build a CSS file embedded in your app and then run a bundler (e.g. Trunk) to compile the rest of your project. To do this automatically you can use Cargo's build scripts or Trunk's pre-build hooks. For larger projects running in watch mode, it may be better to run @unocss/cli in watch mode as well, so building the output CSS file is a bit faster.

You can refer to the already mentioned Leptos & Trunk example taking advantage of Trunk's hooks.

It is also possible to combine Vite with Trunk / Dioxus CLI / Leptos CLI, but it requires a more hacky setup and, unless you really need some Vite-exclusive features (e.g. Webfont self-hosting), it's probably not worth the effort.

License

MIT License

Copyright (c) 2023-PRESENT Kajetan Welc

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.