Skip to content

Error in Conditional rendering using map #3132

Closed Answered by abhi3700
abhi3700 asked this question in Q&A
Discussion options

You must be logged in to vote

I figured out the issue.

Tailwind CSS uses a static CSS generation process, meaning it cannot interpret dynamic class names generated at runtime.

As "bg-{}-300" is constructed dynamically, Tailwind doesn't recognize it, and thus the color is not applied.

To work around this limitation, we need to explicitly handle the colors by matching them and using the static class names like this:

let bg_class = match color {
        "red" => "bg-red-300",
        "blue" => "bg-blue-300",
        "green" => "bg-green-300",
        _ => "bg-gray-300", // Fallback color
    };

...

class: format!(
                "{} hover:bg-gray-400 py-1.5 px-5 rounded-md hover:text-gray-50",
                bg_cla…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@abhi3700
Comment options

Answer selected by abhi3700
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant