docs/config/variants.md
Variants allow you to apply some variations to your existing rules, like the hover: variant from Tailwind CSS.
variants: [
// hover:
(matcher) => {
if (!matcher.startsWith('hover:'))
return matcher
return {
// slice `hover:` prefix and passed to the next variants and rules
matcher: matcher.slice(6),
selector: s => `${s}:hover`,
}
},
],
rules: [
[/^m-(\d)$/, ([, d]) => ({ margin: `${d / 4}rem` })],
]
matcher controls when the variant is enabled. If the return value is a string, it will be used as the selector for matching the rules.selector provides the availability of customizing the generated CSS selector.Let's have a tour of what happened when matching for hover:m-2:
hover:m-2 is extracted from users usageshover:m-2 send to all variants for matchinghover:m-2 is matched by our variant and returns m-2m-2 will be used for the next round of variants matchingm-2 will then goes to match the rules.m-2 { margin: 0.5rem; }:hover to the selector hookAs a result, the following CSS will be generated:
<!-- eslint-skip -->.hover\:m-2:hover { margin: 0.5rem; }
With this, we could have m-2 applied only when users hover over the element.
The variant system is very powerful and can't be covered fully in this guide, you can check the default preset's implementation to see more advanced usages.