myFavShrimp / turf

Macro based compile-time SCSS transpilation, CSS minification, and class name uniquification toolchain inspired by CSS modules.
MIT License
58 stars 3 forks source link

[DISCUSSION] How the struct constants should be used with Leptos dynamic classes #14

Closed SSeanin closed 9 months ago

SSeanin commented 9 months ago

There is a problem with the using the Leptos dynamic classes with turf. When using the dynamic classes with the following syntax for example

<a class=ClassName::BTN class:(ClassName::BTN_CTA)=cta>
</a>

the class:(ClassName::BTN_CTA)=cta part will produce an error because of the view macro.

How can we use turf with Leptos dynamic classes?

SSeanin commented 9 months ago

I've also tried the bellow syntax of the dynamic classes

<a class=ClassName::BTN class=(ClassName::BTN_CTA, cta)>
</a>

this will again produce an error because it expects a string literal by a struct field is passed.

SSeanin commented 9 months ago

I axed using of the Leptos view! macro for using the view builder syntax for now.

myFavShrimp commented 9 months ago

I achieved similar behavior using a closure that returns the class name

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

let my_class_name = move || {
    if count.get() % 2 == 0 {
        ClassName::SOME_CLASS
    } else {
        ""
    }
};

view! {
    <style>{STYLE_SHEET}</style>
    <div class=my_class_name>
    </div>
}
SSeanin commented 9 months ago

We can also create a class String that is manually and dynamically filled with ClassName values and the format! macro

let style = format("{} {}", if something_else {ClassName::SOMETHING_ELSE} else {""}, ClassName::SOMETHING);

view! {
    <div class=style></div>
}

This can then be used with a closure to be reactive. However I think this won't be the optimal way. Of course, this is more on the Leptos and view! side of things than the turf crate.