Skip to content

why adding @apply to .field_with_errors doesnt work? #191

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

Closed
buncis opened this issue Aug 30, 2022 · 5 comments
Closed

why adding @apply to .field_with_errors doesnt work? #191

buncis opened this issue Aug 30, 2022 · 5 comments

Comments

@buncis
Copy link

buncis commented Aug 30, 2022

I got this warning in my inspect element
image

application.tailwind.css

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer components {
  .field_with_errors {
    @apply flex w-full
  }
}

it works normally when the class is added from the start
but when its added by a form that has error its not work
ref active_record_validations#displaying-validation-errors-in-views

I'm also using turbo and it seems work normally without turbo

my current solution to this is I'm adding empty div with the class

 <div class="field_with_errors"></div>

I kinda got confused too sometimes it works sometimes its not

@rafaelfranca
Copy link
Member

Thank you for the issue. tailwindcss-rails is mostly a wrapper around the tailwindcss command line tool. All the behavior is defined by tailwindcss, so if something isn't working with your css, it is likely it is a tailwindcss issue. So it is better to report to tailwindcss.

@rafaelfranca rafaelfranca closed this as not planned Won't fix, can't repro, duplicate, stale Sep 1, 2022
@iamjohnford
Copy link

@buncis I ran into something similar and used the safelist in tailwind.config.js. That might work as well since it lets tailwind know that the class exists.

module.exports = {
  safelist: [
    'field_with_errors'
  ]
}

@buncis
Copy link
Author

buncis commented Sep 16, 2022

because the .field_with_errors is added dynamically to the webpage

and tailwind css doing tree-shaking when compiling, it will try to remove all custom css that is not used

therefore .field_with_errors will be removed thus why the css is not applied

to make it stay we could declare the class without using @layer as per this guide

the final correct syntax are

@tailwind base;
@tailwind components;
@tailwind utilities;

.field_with_errors {
  @apply flex w-full
}

@buncis
Copy link
Author

buncis commented Sep 16, 2022

@iamjohnford I also found another solution that is applying the code outside layer

its because the guide say safelist is less favoured to use https://tailwindcss.com/docs/content-configuration#safelisting-classes

thanks for you for mentioning me with ur solution that I could finally found a solution

@iamjohnford
Copy link

@buncis Nice. Thanks for the info! What you mentioned above works for me as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants