Replies: 2 comments 3 replies
-
You can add multiple classes to the same element and dioxus will merge them together: div {
class: if true { "w-6" },
class: if false { "bg-red" }
} |
Beta Was this translation helpful? Give feedback.
-
The way @ealmloff said works fine for simple conditions but for more complex logic it also throws me the error The function accepts a list of tuples where the first element is the class and the second element is a boolean that will determine whether the class will be included or not. #[must_use]
pub(crate) fn class_list<'a, T>(list: T) -> String
where
T: IntoIterator<Item = (&'a str, bool)>,
{
list.into_iter()
.filter(|(_, v)| *v)
.map(|(s, _)| s)
.collect::<Vec<&'a str>>()
.join(" ")
} You can use it like this: rsx! {
li {
button {
class: class_list([
("flex cursor-pointer *:m-auto", true),
("size-12", context.is_vertical()),
("text-neutral-400 hover:text-neutral-500", !is_active()),
("border-orange-500", is_active()),
(
match context.position {
TabsPosition::Top => "border-t-4",
TabsPosition::Bottom => "border-b-4",
TabsPosition::Left => "border-l-4",
TabsPosition::Right => "border-r-4",
},
is_active(),
),
]),
onclick,
r#type: "button",
{children}
}
}
} And if you are using the {
"tailwindCSS.experimental.classRegex": [
"class: \"(.*)\"",
[
"class_list\\(\\s*\\[([\\s\\S]*?)\\]\\s*\\)",
"\"([^\"']*)\""
]
],
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
How can I conditionally add a tailwind class to an rsx element?
So far my attempts have resulted in tailiwind not seeing the conditional code and as a result not generating the required class.
I see solutions applicable for Javascript, not Rust.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions