Using hover and focus variants when not disabled #3574
-
I want to apply classes on hover and focus only if the button is not disabled. What approach should I take? Should I customize variants so it would only apply styles when |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
Hey! In short: you can do the opposite of what you want: Add disabled styles if the button is disabled. These will "cancel" any hover/focus styles you have applied on the same element. Detailed answer You can enable the If a https://play.tailwindcss.com/LOaudRbGXs Notice how I enabled the Hope it helps 👍 |
Beta Was this translation helpful? Give feedback.
-
@simonswiss Thanks for all your work as a maintainer! I found some strange behavior that differs from the detailed answer above. I was wondering if it was a bug or expected behavior.
The |
Beta Was this translation helpful? Give feedback.
-
@selemondev From my knowledge dynamically generated classes doesn't work at all in Tailwind |
Beta Was this translation helpful? Give feedback.
-
I used the |
Beta Was this translation helpful? Give feedback.
Hey!
In short: you can do the opposite of what you want: Add disabled styles if the button is disabled. These will "cancel" any hover/focus styles you have applied on the same element.
Detailed answer
You can enable the
disabled
variant for what you need (likebackgroundColor, textColor
) in your config file'svariants
.If a
disabled:
style is set for a button, it will never allow hover/focus styles to show. Here's an example in Tailwind Play:https://play.tailwindcss.com/LOaudRbGXs
Notice how I enabled the
disabled
variant for both the background and text colors, and then applieddisabled
styles to both buttons (one of them having thedisabled
attribute).Hope it helps 👍