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.
uno!
- works just like classes!
macro, but also transforms Variant Groups in string literals. The transformation is done at compile time, so there is no overhead.to_uno!
- it has the same purpose, but the transformation is executed at runtime. It can be useful to transform dynamically combined classes, but generally should be avoided. Requires runtime
feature.\
Also UnoCSS may not be able to pick up utilities generated at runtime, so it may be necessary to use Safelist or runtime engine.rust
uno!["hover:(bg-gray-400 font-medium)", "font-(light mono)"]; // -> "hover:bg-gray-400 hover:font-medium font-light font-mono"
rust
let some = Some("text-red");
let none = None::<String>;
let truthy = true;
let falsy = false;
uno![
some,
none,
"text-green" => truthy,
"text-black" => falsy,
Some("text-white").map(|_| "text-blue")
]; // -> "text-red text-green text-blue"
rust
let truthy = true;
uno!["text-(sm center)" => truthy]; // -> "text-sm text-center"
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] ```
```rust use leptos::*; use unocssvariantgroup_transformer::uno;
fn App() -> impl IntoView { view! {
// or as a reactive derived signal:
fn App() -> impl IntoView {
let reactivecondition = createrwsignal(true);
view! {
You can check out also this example Leptos app.
```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" }} } ```
```rust use yew::prelude::*; use unocssvariantgroup_transformer::uno;
pub fn App() -> Html { html! {
uno!
macro globally```rust
extern crate yewunocsstransformer; ```
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.
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.