Skip to content

Commit 20217b0

Browse files
committed
2.0.0 Add support for multiple levels of unimportance
1 parent 46e7273 commit 20217b0

File tree

5 files changed

+122
-18
lines changed

5 files changed

+122
-18
lines changed

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
node_modules
1+
*
2+
!src
3+
!.gitignore
4+
!CHANGELOG.md
5+
!LICENSE
6+
!package.json
7+
!package-lock.json
8+
!README.md

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 2.0.0 [2023-09-29]
2+
- (BC BREAK) Variant changed from `~:` to `-:`
3+
- Added support for additional levels of unimportance
4+
15
### 1.0.1 [2023-09-29]
26
- Create project readme
37

README.md

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
A [Tailwind CSS] plugin that creates lower-specificity utility classes that can
33
be 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

4039
This 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()`
60105
function, 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

71116
However, having a specificity of `0-0-0` means that the default tailwind base
@@ -80,12 +125,12 @@ a {
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

91136
To 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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tailwindcss-unimportant",
3-
"version": "1.0.1",
3+
"version": "2.0.0",
44
"description": "Adds lower specificity tailwindcss utility classes to be used in components",
55
"main": "src/index.js",
66
"peerDependencies": {

src/index.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,43 @@
11
const plugin = require("tailwindcss/plugin");
22

33
const unimportant = plugin(
4-
({ addVariant }) => {
5-
addVariant("~", ":where(&)");
4+
({ matchVariant }) => {
5+
matchVariant(
6+
'-',
7+
(unimportance) => {
8+
return ':where(&)'
9+
},
10+
{
11+
values: {
12+
DEFAULT: 1,
13+
'': 2,
14+
'-': 3,
15+
'--': 4,
16+
'---': 5,
17+
'----': 6,
18+
'-----': 7,
19+
'------': 8,
20+
'-------': 9,
21+
'--------': 10,
22+
1: 1,
23+
2: 2,
24+
3: 3,
25+
4: 4,
26+
5: 5,
27+
6: 6,
28+
7: 7,
29+
8: 8,
30+
9: 9,
31+
10: 10,
32+
},
33+
sort(a, z) {
34+
const aValue = parseInt(typeof a.value === 'symbol' ? 0 : a.value);
35+
const zValue = parseInt(typeof z.value === 'symbol' ? 0 : z.value);
36+
37+
return zValue - aValue;
38+
}
39+
},
40+
);
641
},
742
{important: ':root'}
843
);

0 commit comments

Comments
 (0)