so I was trying out tailwindcss with leptos framework, what I noticed is when classes are included in static class attribute, the tailwindcss config recognize and build up those in output.css. however if I was to include class name using Dynamic Classes in leptos framework, those classes are not being auto included in output.css. Eg. 'rounded-lg' style won't be included in output.css
use leptos::*;
use leptos_meta::*;
#[component]
fn App(cx: Scope) -> impl IntoView {
let (count, set_count) = create_signal(cx, 0);
provide_meta_context(cx);
view! { cx,
<Stylesheet id="leptos" href="/pkg/tailwind.css"/>
<button
class="p-1 px-2.5 bg-sky-500 hover:bg-sky-700 focus:outline-none focus:ring focus:ring-sky-300 hover:text-white"
class:rounded-lg=move || count() % 2 == 1
on:click=move |_| {
set_count.update(|n| *n += 1);
}
>
"Click me: " {move || count()}
</button>
}
}
fn main() {
leptos::mount_to_body(|cx| view!{ cx, <App/> })
}
I had to manually add those styles to output.css, wondering if there is better way to auto extract that rather than adding it manually all the time.
Leptos also supports a tuple-based syntax for dynamic classes, and this works with the default Tailwind parser:
class=("rounded-lg", move || count() % 2 == 1)
That said, you can make the other syntax work as well. If you add a space after
class:
, Tailwind will find the dynamic class names, and Leptos will continue to compile successfully.That is to say, turn this:
class:rounded-lg=move || count() % 2 == 1
Into this:
class: rounded-lg=move || count() % 2 == 1
Or this:
class: rounded-lg = move || count() % 2 == 1
It's also possible to customize the Tailwind parsing for Rust files. The following example tailwind.config.js configures Tailwind to successfully parse the tailwind classes from
class="p-1 bg-sky-500"
andclass:rounded-lg=move || count() % 2 == 1
andclass=("rounded-lg", move || count() % 2 == 1)
in Rust files.