tailwindcss icon indicating copy to clipboard operation
tailwindcss copied to clipboard

@variants not really working

Open hoquangthaiholy opened this issue 6 years ago • 2 comments

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.

hoquangthaiholy avatar Apr 17 '20 01:04 hoquangthaiholy

@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

  1. Generates utilities with the hover: prefix.
  2. 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.

tlgreg avatar Apr 17 '20 02:04 tlgreg

Thanks, @tlgreg. It's clear enough.

hoquangthaiholy avatar Apr 17 '20 03:04 hoquangthaiholy