Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit 64ed7dd

Browse files
authored
Add support for selectors in option (#175)
1 parent e7e4544 commit 64ed7dd

File tree

4 files changed

+189
-14
lines changed

4 files changed

+189
-14
lines changed

src/lib/generateRules.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,8 @@ function* resolveMatches(candidate, context) {
261261
}
262262
}
263263

264-
function inKeyframes(d) {
265-
return (
266-
d.parent.parent && d.parent.parent.type === 'atrule' && d.parent.parent.name === 'keyframes'
267-
)
268-
}
269-
270-
function makeImportant(rule) {
271-
rule.walkDecls((d) => {
272-
if (d.parent.type === 'rule' && !inKeyframes(d)) {
273-
d.important = true
274-
}
275-
})
264+
function inKeyframes(rule) {
265+
return rule.parent && rule.parent.type === 'atrule' && rule.parent.name === 'keyframes'
276266
}
277267

278268
function generateRules(candidates, context) {
@@ -300,8 +290,26 @@ function generateRules(candidates, context) {
300290
}
301291

302292
return allRules.flat(1).map(([{ sort, layer, options }, rule]) => {
303-
if (context.tailwindConfig.important === true && options.respectImportant) {
304-
makeImportant(rule)
293+
if (options.respectImportant) {
294+
if (context.tailwindConfig.important === true) {
295+
rule.walkDecls((d) => {
296+
if (d.parent.type === 'rule' && !inKeyframes(d.parent)) {
297+
d.important = true
298+
}
299+
})
300+
} else if (typeof context.tailwindConfig.important === 'string') {
301+
let container = postcss.root({ nodes: [rule] })
302+
container.walkRules((r) => {
303+
if (inKeyframes(r)) {
304+
return
305+
}
306+
307+
r.selectors = r.selectors.map((selector) => {
308+
return `${context.tailwindConfig.important} ${selector}`
309+
})
310+
})
311+
rule = container.nodes[0]
312+
}
305313
}
306314
return [sort | context.layerOrder[layer], rule]
307315
})

tests/important-selector.test.css

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
* {
2+
--tw-shadow: 0 0 #0000;
3+
--tw-ring-inset: var(--tw-empty, /*!*/ /*!*/);
4+
--tw-ring-offset-width: 0px;
5+
--tw-ring-offset-color: #fff;
6+
--tw-ring-color: rgba(59, 130, 246, 0.5);
7+
--tw-ring-offset-shadow: 0 0 #0000;
8+
--tw-ring-shadow: 0 0 #0000;
9+
}
10+
.container {
11+
width: 100%;
12+
}
13+
@media (min-width: 640px) {
14+
.container {
15+
max-width: 640px;
16+
}
17+
}
18+
@media (min-width: 768px) {
19+
.container {
20+
max-width: 768px;
21+
}
22+
}
23+
@media (min-width: 1024px) {
24+
.container {
25+
max-width: 1024px;
26+
}
27+
}
28+
@media (min-width: 1280px) {
29+
.container {
30+
max-width: 1280px;
31+
}
32+
}
33+
@media (min-width: 1536px) {
34+
.container {
35+
max-width: 1536px;
36+
}
37+
}
38+
#app .btn {
39+
button: yes;
40+
}
41+
@font-face {
42+
font-family: Inter;
43+
}
44+
@page {
45+
margin: 1cm;
46+
}
47+
.custom-component {
48+
font-weight: 700;
49+
}
50+
.custom-important-component {
51+
text-align: center !important;
52+
}
53+
@keyframes spin {
54+
to {
55+
transform: rotate(360deg);
56+
}
57+
}
58+
#app .animate-spin {
59+
animation: spin 1s linear infinite;
60+
}
61+
#app .font-bold {
62+
font-weight: 700;
63+
}
64+
.custom-util {
65+
button: no;
66+
}
67+
#app .group:hover .group-hover\:focus-within\:text-left:focus-within {
68+
text-align: left;
69+
}
70+
#app [dir='rtl'] .rtl\:active\:text-center:active {
71+
text-align: center;
72+
}
73+
@media (prefers-reduced-motion: no-preference) {
74+
#app .motion-safe\:hover\:text-center:hover {
75+
text-align: center;
76+
}
77+
}
78+
#app .dark .dark\:focus\:text-left:focus {
79+
text-align: left;
80+
}
81+
@media (min-width: 768px) {
82+
#app .md\:hover\:text-right:hover {
83+
text-align: right;
84+
}
85+
}

tests/important-selector.test.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div class="container"></div>
2+
<div class="btn"></div>
3+
<div class="animate-spin"></div>
4+
<div class="custom-util"></div>
5+
<div class="custom-component"></div>
6+
<div class="custom-important-component"></div>
7+
<div class="font-bold"></div>
8+
<div class="md:hover:text-right"></div>
9+
<div class="motion-safe:hover:text-center"></div>
10+
<div class="dark:focus:text-left"></div>
11+
<div class="group-hover:focus-within:text-left"></div>
12+
<div class="rtl:active:text-center"></div>

tests/important-selector.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const postcss = require('postcss')
2+
const tailwind = require('../src/index.js')
3+
const fs = require('fs')
4+
const path = require('path')
5+
6+
function run(input, config = {}) {
7+
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
8+
}
9+
10+
test('important selector', () => {
11+
let config = {
12+
important: '#app',
13+
darkMode: 'class',
14+
purge: [path.resolve(__dirname, './important-selector.test.html')],
15+
corePlugins: { preflight: false },
16+
theme: {},
17+
plugins: [
18+
function ({ addComponents, addUtilities }) {
19+
addComponents(
20+
{
21+
'.btn': {
22+
button: 'yes',
23+
},
24+
},
25+
{ respectImportant: true }
26+
)
27+
addComponents(
28+
{
29+
'@font-face': {
30+
'font-family': 'Inter',
31+
},
32+
'@page': {
33+
margin: '1cm',
34+
},
35+
},
36+
{ respectImportant: true }
37+
)
38+
addUtilities(
39+
{
40+
'.custom-util': {
41+
button: 'no',
42+
},
43+
},
44+
{ respectImportant: false }
45+
)
46+
},
47+
],
48+
}
49+
50+
let css = `
51+
@tailwind base;
52+
@tailwind components;
53+
@layer components {
54+
.custom-component {
55+
@apply font-bold;
56+
}
57+
.custom-important-component {
58+
@apply text-center !important;
59+
}
60+
}
61+
@tailwind utilities;
62+
`
63+
64+
return run(css, config).then((result) => {
65+
let expectedPath = path.resolve(__dirname, './important-selector.test.css')
66+
let expected = fs.readFileSync(expectedPath, 'utf8')
67+
68+
expect(result.css).toMatchCss(expected)
69+
})
70+
})

0 commit comments

Comments
 (0)