22A [ Tailwind CSS] plugin that creates lower-specificity utility classes that can
33be used to create components with overridable defaults.
44
5-
65## Installation
76
87``` sh
@@ -20,21 +19,21 @@ module.exports = {
2019
2120## Getting started
2221
23- To make a class unimportant, use the ` ~ :` prefix .
22+ To make a class unimportant, use the ` - :` variant .
2423
2524``` html
26- <p class =" ~ :font-bold" >Hello world</p >
25+ <p class =" - :font-bold" >Hello world</p >
2726```
2827
29- These classes will always have lower precedence than classes without the prefix .
28+ These classes will always have lower precedence than classes without the variant .
3029
3130``` php
3231// The text will have normal or bold weight depending on the order of the
3332// `font-bold` and `font-normal` classes in the compiled CSS.
3433<p class =" font-bold font-normal" >Hello world</p >
3534
3635// The text will always have normal weight
37- <p class =" ~ :font-bold font-normal" >Hello world</p >
36+ <p class =" - :font-bold font-normal" >Hello world</p >
3837```
3938
4039This is useful when making components where you want to be able to override the
@@ -43,7 +42,7 @@ default classes.
4342``` php
4443/// paragraph.blade.php
4544<p {{
46- $attributes- >merge(['class' => '~ :text-base ~ :font-normal ~:text-grey-800 '])
45+ $attributes- >merge(['class' => '- :text-base - :font-normal'])
4746}}>
4847 {{ $slot }}
4948</p >
@@ -54,18 +53,64 @@ default classes.
5453<x-paragraph class =" text-lg font-bold" >Hello world!</x-paragraph >
5554```
5655
56+ ### Making classes even more unimportant
57+
58+ Sometimes it's useful to extend components within other components, and you can
59+ run into the same problem of clashing Tailwind classes.
60+
61+ This plugin provides additional class variants for up to 10 levels of
62+ unimportance: ` --: ` , ` ---: ` , ` ----: ` , etc.
63+
64+ ``` php
65+ // -:text-blue takes precedence over --:text-green, which takes precedence over
66+ // ---:text-red
67+ <a class =" ---:text-red --:text-green -:text-blue" >
68+ The link is blue
69+ </a >
70+ ```
71+
72+ As the variants become larger, you may prefer to use the number variants, where
73+ the lowest number takes precedence: ` --1: ` , ` --2: ` , ` --3: ` , etc. The number and
74+ symbol variants can be used interchangeably.
75+
76+ ``` php
77+ // This example is identical to the previous example
78+ <a class =" --5:text-red --:text-green --1:text-blue" >
79+ The link is blue
80+ </a >
81+ ```
82+
83+ Arbitrary values are also supported. For example, ` --[5]: ` is equivalent to
84+ ` -----: ` . Zero and negative values can be used to make classes of higher
85+ precedence.
86+
87+ | Unimportance | Symbolic variant | Numberic variant |
88+ | --------------| ------------------| ------------------|
89+ | 1 | ` -: ` | ` --1: ` |
90+ | 2 | ` --: ` | ` --2: ` |
91+ | 3 | ` ---: ` | ` --3: ` |
92+ | 4 | ` ----: ` | ` --4: ` |
93+ | 5 | ` -----: ` | ` --5: ` |
94+ | 6 | ` ------: ` | ` --6: ` |
95+ | 7 | ` -------: ` | ` --7: ` |
96+ | 8 | ` --------: ` | ` --8: ` |
97+ | 9 | ` ---------: ` | ` --9: ` |
98+ | 10 | ` ----------: ` | ` --10: ` |
99+ | ` n ` | _ n/a_ | ` --[n]: ` |
100+
101+
57102## How it works
58103
59- The ` ~ :` prefix wraps the selector for a given class in the css ` :where() `
104+ The ` - :` variant wraps the selector for a given class in the css ` :where() `
60105function, reducing its [ specificity] to ` 0-0-0 ` . This means classes you use
61- without the unimportant prefix will be applied preferentially.
106+ without the unimportant variant will be applied preferentially.
62107
63108``` css
64109/* Specificity of 0-1-0 */
65110.bg-white
66111
67112/* Specificity of 0-0-0 */
68- :where(.\~ \: bg-white )
113+ :where(.- \: bg-white )
69114```
70115
71116However, having a specificity of ` 0-0-0 ` means that the default tailwind base
80125}
81126
82127/* Specificity of 0-0-0 */
83- :where(.\~ \: text-red ) {
128+ :where(.- \: text-red ) {
84129 color : red ;
85130}
86131 </style >
87132
88- <a class =" ~ :text-red" >The link is blue</a >
133+ <a class =" - :text-red" >The link is blue</a >
89134```
90135
91136To fix this problem, the plugin sets the ` important ` Tailwind configuration
@@ -106,13 +151,26 @@ a {
106151}
107152
108153/* Specificity of 0-1-0 */
109- :root :where(.\~ \: text-red ) {
154+ :root :where(.- \: text-red ) {
110155 color : red ;
111156}
112157 </style >
113158
114- <a class =" ~:text-red" >The link is red</a >
115- <a class =" ~:text-red text-green" >The link is green</a >
159+ <a class =" -:text-red" >The link is red</a >
160+ <a class =" -:text-red text-green" >The link is green</a >
161+ ```
162+
163+ ### Levels of unimportance
164+
165+ Classes with greater levels of unimportance are output first in the generated
166+ CSS file so that the less unimportant classes take precedence.
167+
168+ ``` css
169+ /* The more unimportant class is output first */
170+ .--\: bg-white {...}
171+
172+ /* The later CSS rule takes precedence */
173+ .-\: bg-black {...}
116174```
117175
118176[ Tailwind CSS ] : https://github.com/tailwindlabs/tailwindcss
0 commit comments