#macro #web #proc-macro


A reactive helper that ensures normalized class list strings in frontend frameworks like Leptos

7 releases

0.1.7 Aug 23, 2023
0.1.6 Aug 23, 2023

#197 in WebAssembly

42 downloads per month

MIT license

174 lines


GitHub Badge crates.io Badge docs.rs Badge Build Status Badge

A reactive helper that ensures normalized class list strings in frontend frameworks like Leptos.


Examples provided will be for the Leptos framework post-{context removal}, but it will work pre-{context removal} and might work in other similar frameworks.

This library is meant to be agnostic and has no runtime dependencies, but it has only been tested with Leptos.

cargo add class_list


class_list![] by default wraps itself in a move closure, meaning it will be reactive by default.

let (count, set_count) = create_signal(0);

	move || {
		set_count.update(|count| *count += 1);

let count_class = move || format!("count-{}", count());
let count_is_even = move || count() % 2 == 0;

view! {
	<div class=class_list![
		// Closures get called automatically.
		// Closures can be written directly into the macro.
		move || format!("count-{}", count()),
		// Both Option and Result can be used as values.
		// None and Err result in no class name.
		// More conveniently, class names can be bound to reactive toggles.
		// "even" will only be applied when `count_is_even()` is true.
		// You also don't need to call closures here.
		"even" <=> count_is_even
	] />


Each option must be followed by a ;.


To generate a non-reactive String, add the raw option to the beginning.



Rarely, you may need to clone something before passing it in.

The macro makes this easy with the clone option which clones the variable before it gets moved into the closure.

If possible try to avoid needing this option in the first place.



This should never be needed because it's automatically supplied by a wrapper macro_rules!.

In a case where the trait imports cannot be resolved--such as when used inside of another library--, the path can be redefined.

The path should lead to the root of this library.

	crate = ::your_lib::class_list;

Implementing Traits

If you'd like to simply pass a type to the macro instead of converting it every time, you can implement the ClassList and ClassToggle types.

Check out traits.rs to see the default implementations which are good examples of implementing them.

// If you're using a type you don't own,
// you must wrap it in a new struct.
struct Bool(bool);

impl ClassList for Bool {
	fn to_class_list(&self, normalize: bool) -> String {
		// If the string could contain multiple class names
		// you should use `normalize` to determine whether
		// or not to call `.to_class_list()` on it before
		// returning.
		// If you're lazy you could always normalize, but
		// then the string will be normalized multiple
		// times for every update in the macro.
		if self.0 {
		} else {
impl ClassToggle for Bool {
	fn to_class_toggle(&self) -> bool {

// Option, Result, and Fn are implemented in a way which
// allows any new type you implement to be automatically
// passed through.
		// ClassList
		move || Bool(false),
		// ClassToggle
		"hidden" <=> move || Bool(false),
	"false true class list".to_string()


~20K SLoC