-
-
Notifications
You must be signed in to change notification settings - Fork 650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditional tailwind classes are not automatically included #2890
Comments
I'm not sure this is an actionable issue, from the framework's perspective: i.e., there's no commit to the codebase of Leptos that could solve this problem for you. I think it's something that can either be fixed by adjusting the Tailwind config (setting up Tailwind to check for If it is possible to do it by adjusting Tailwind config then I guess a PR to update the example Tailwind config would be welcome! |
Good suggestions! I'll try them out, and see what the outcome is. |
@gbj As you suspected, this doesn't sound like it is a Leptos problem, but more of a Tailwind problem. Still, I would argue that it's a bit confusing that adding code comments fixes the problem. Using the "complicated class" syntax also does the trick, as does adding a space after Working exampleuse leptos::*;
fn main() {
mount_to_body(|| {
view! { <p
class: text-orange-600=true
>"This text will be orange"</p> }
})
} I made an attempt to solve the problem through the tailwind config. I'm no JS expert, but this seems to work for both regular class definitions, and those using the /** @type {import('tailwindcss').Config} */
module.exports = {
content: {
files: ["./*.html", "./src/**/*.rs"],
extract: (content) => {
const matches = content.match(/(class:)?(\w+[-\w]*\w+)/g) || [];
return matches.map((match) => match.replace(/^class:/, ''));
},
},
theme: {
extend: {},
},
plugins: [],
}; Feel free to suggest changes to the above tailwind config, but I think it could be a good idea to update the documentation, and describe the problem briefly in the getting started guide for Tailwind CSR in Leptos. |
I cleaned up the tailwind config a bit, and made sure that it works as expected. Tailwind config/** @type {import('tailwindcss').Config} */
module.exports = {
content: {
files: ["./*.html", "./src/**/*.rs"],
extract: (content) => {
const matches = [...content.matchAll(/(class:)?(\w+[-\w]*\w+)/g)];
return matches.map((match) => match[2]);
},
},
theme: {
extend: {},
},
plugins: [],
}; Example
|
Transforming the input is less work than changing the extractor:
|
When using Tailwind CSS with the
view!
macro, there are some quirks related to conditional Tailwind classes. Within the macro, using theclass:class-name=move || condition
syntax won't trigger Tailwind to include the class. But adding the class name as a comment within any view macro will cause the class to be included, and will render the component as expected.Non-working example
Working example
Leptos Dependencies
To Reproduce
Steps to reproduce the behavior:
main.rs
trunk serve --open
Expected behavior
I would expect the conditional Tailwind classes to get compiled too, without having to add them separately as a comment.
Screenshots
The text was updated successfully, but these errors were encountered: