You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/pages/docs/plugins.mdx
+54-5
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,8 @@ Plugin functions receive a single object argument that can be [destructured](htt
31
31
-`addComponents()`, for registering new static component styles
32
32
-`matchComponents()`, for registering new dynamic component styles
33
33
-`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
35
36
-`theme()`, for looking up values in the user's theme configuration
36
37
-`config()`, for looking up values in the user's Tailwind configuration
37
38
-`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
442
443
443
444
## Adding variants
444
445
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`.
446
447
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.
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
+
constplugin=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
+
<divclass="**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:
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