@variants not really working
I followed the document from https://tailwindcss.com/docs/pseudo-class-variants#generative-variants-for-custom-utilities and try to generate variants. I used a plugin from https://github.com/ChanceArthur/tailwindcss-dark-mode. Those variants are working well on inline classes but not in the variant.
// tailwind.config.js
{
variants: {
backgroundColor: [ 'dark', 'dark-hover', 'dark-group-hover', 'dark-even', 'dark-odd'],
borderColor: ['responsive', 'hover', 'focus','dark', 'dark-focus', 'dark-focus-within'],
textColor: ['dark', 'dark-hover', 'dark-active'],
}
}
Example:
.table th,
.table td {
@apply border border-gray-200;
@variants dark {
@apply border-gray-700;
}
}
Those codes will overwrite @apply border-gray-700 at the last but not in the variant dark.
@variants is used to wrap rulesets and keeps the ruleset as is + generates copies of it with the variants applied.
Above it is used on properties.
Example:
@variants hover {
.table th,
.table td {
@apply text-red-500;
}
}
Generates:
.table th,
.table td {
color: #f56565;
}
.hover\:table:hover th,
.hover\:table:hover td {
color: #f56565;
}
Note how it
- Generates utilities with the
hover:prefix. - Keeps the original ruleset too, doesn't remove it and generates a new one in addition.
These are because it is really useful for custom utilities, not components.
Thanks, @tlgreg. It's clear enough.