Skip to content

Commit 587a246

Browse files
committed
Merge branch 'wongjn-document-matchVariant'
2 parents 995eebd + 6bb7246 commit 587a246

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

src/pages/docs/plugins.mdx

+54-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ Plugin functions receive a single object argument that can be [destructured](htt
3131
- `addComponents()`, for registering new static component styles
3232
- `matchComponents()`, for registering new dynamic component styles
3333
- `addBase()`, for registering new base styles
34-
- `addVariant()`, for registering custom variants
34+
- `addVariant()`, for registering custom static variants
35+
- `matchVariant()`, for registering custom dynamic variants
3536
- `theme()`, for looking up values in the user's theme configuration
3637
- `config()`, for looking up values in the user's Tailwind configuration
3738
- `corePlugins()`, for checking if a core plugin is enabled
@@ -442,9 +443,11 @@ Since base styles are meant to target bare selectors like `div` or `h1`, they do
442443

443444
## Adding variants
444445

445-
The `addVariant` function allows you to register your own custom [modifiers](/docs/hover-focus-and-other-states) that can be used just like the built-in hover, focus, active, etc. variants.
446+
The `addVariant` and `matchVariant` functions allow you to register your own custom [modifiers](/docs/hover-focus-and-other-states) that can be used just like built-in variants like `hover`, `focus`, or `supports`.
446447

447-
To add a new variant, call the `addVariant` function, passing in the name of your custom variant, and a format string that represents how the selector should be modified.
448+
### Static variants
449+
450+
Use the `addVariant` function for simple custom variants, passing in the name of your custom variant, and a format string that represents how the selector should be modified.
448451

449452
```js tailwind.config.js
450453
const plugin = require('tailwindcss/plugin')
@@ -455,7 +458,7 @@ module.exports = {
455458
plugin(function({ addVariant }) {
456459
addVariant('optional', '&:optional')
457460
addVariant('hocus', ['&:hover', '&:focus'])
458-
addVariant('supports-grid', '@supports (display: grid)')
461+
addVariant('inverted-colors', '@media (inverted-colors: inverted)')
459462
})
460463
]
461464
}
@@ -464,12 +467,58 @@ module.exports = {
464467
The first argument is the modifier name that users will use in their HTML, so the above example would make it possible to write classes like these:
465468

466469
```html
467-
<form class="flex **supports-grid:grid** ...">
470+
<form class="flex **inverted-colors:outline** ...">
468471
<input class="**optional:border-gray-300** ..." />
469472
<button class="bg-blue-500 **hocus:bg-blue-600**">...</button>
470473
</form>
471474
```
472475

476+
### Dynamic variants
477+
478+
Use the `matchVariant` function to register new parameterized variants like the built-in `supports-*`, `data-*`, and `aria-*` variants:
479+
480+
```js tailwind.config.js
481+
const plugin = require('tailwindcss/plugin')
482+
483+
module.exports = {
484+
plugins: [
485+
plugin(function({ matchVariant }) {
486+
matchVariant(
487+
'nth',
488+
(value) => {
489+
return `&:nth-child(${value})`;
490+
},
491+
{
492+
values: {
493+
1: '1',
494+
2: '2',
495+
3: '3',
496+
}
497+
}
498+
);
499+
})
500+
]
501+
}
502+
```
503+
504+
Variants defined with `matchVariant` also support arbitrary values using square bracket notation:
505+
506+
```html
507+
<div class="**nth-[3n+1]:bg-blue-500** ...">
508+
<!-- ... -->
509+
</div>
510+
```
511+
512+
Use the `sort` option to control the source order of the generated CSS if needed to avoid precedence issues with other values that come from the same variant:
513+
514+
```js
515+
matchVariant("min", (value) => `@media (min-width: ${value})`, {
516+
sort(a, z) {
517+
return parseInt(a) - parseInt(z);
518+
},
519+
});
520+
```
521+
473522
### Parent and sibling states
474523

475524
Your custom modifiers won't automatically work with Tailwind's [parent](/docs/hover-focus-and-other-states#styling-based-on-parent-state) and [sibling](/docs/hover-focus-and-other-states#styling-based-on-sibling-state) state modifiers.

0 commit comments

Comments
 (0)