Skip to content

Commit a38f15f

Browse files
authored
Merge pull request tailwindlabs#642 from justinanastos/fix/641/resposive-nested-atrules
Adding responsive variants for custom variants with atRules does not work as exepcted
2 parents 8b1b797 + 40bb6c6 commit a38f15f

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

__tests__/responsiveAtRule.test.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,114 @@ function run(input, opts = config) {
66
return postcss([plugin(opts)]).process(input, { from: undefined })
77
}
88

9+
test('it can generate responsive variants for nested at rules', () => {
10+
const input = `
11+
@responsive {
12+
.banana { color: yellow; }
13+
.chocolate { color: brown; }
14+
15+
@supports(display: grid) {
16+
.grid\\:banana { color: blue; }
17+
.grid\\:chocolate { color: green; }
18+
}
19+
}
20+
`
21+
22+
const output = `
23+
.banana {
24+
color: yellow;
25+
}
26+
27+
.chocolate {
28+
color: brown;
29+
}
30+
31+
@supports(display: grid) {
32+
.grid\\:banana {
33+
color: blue;
34+
}
35+
36+
.grid\\:chocolate {
37+
color: green;
38+
}
39+
}
40+
41+
@media (min-width: 500px) {
42+
.sm\\:banana {
43+
color: yellow;
44+
}
45+
46+
.sm\\:chocolate {
47+
color: brown;
48+
}
49+
50+
@supports(display: grid) {
51+
.sm\\:grid\\:banana {
52+
color: blue;
53+
}
54+
55+
.sm\\:grid\\:chocolate {
56+
color: green;
57+
}
58+
}
59+
}
60+
61+
@media (min-width: 750px) {
62+
.md\\:banana {
63+
color: yellow;
64+
}
65+
66+
.md\\:chocolate {
67+
color: brown;
68+
}
69+
70+
@supports(display: grid) {
71+
.md\\:grid\\:banana {
72+
color: blue;
73+
}
74+
75+
.md\\:grid\\:chocolate {
76+
color: green;
77+
}
78+
}
79+
}
80+
81+
@media (min-width: 1000px) {
82+
.lg\\:banana {
83+
color: yellow;
84+
}
85+
86+
.lg\\:chocolate {
87+
color: brown;
88+
}
89+
90+
@supports(display: grid) {
91+
.lg\\:grid\\:banana {
92+
color: blue;
93+
}
94+
95+
.lg\\:grid\\:chocolate {
96+
color: green;
97+
}
98+
}
99+
}
100+
`
101+
102+
return run(input, {
103+
screens: {
104+
sm: '500px',
105+
md: '750px',
106+
lg: '1000px',
107+
},
108+
options: {
109+
separator: ':',
110+
},
111+
}).then(result => {
112+
expect(result.css).toMatchCss(output)
113+
expect(result.warnings().length).toBe(0)
114+
})
115+
})
116+
9117
test('it can generate responsive variants', () => {
10118
const input = `
11119
@responsive {

src/lib/substituteResponsiveAtRules.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export default function(config) {
2525
})
2626

2727
mediaQuery.append(
28-
responsiveRules.map(rule => {
28+
// Filter out nested `atRules`; we'll process those separately
29+
responsiveRules.filter(rule => rule.type !== 'atrule').map(rule => {
2930
const cloned = rule.clone()
3031
cloned.selectors = _.map(rule.selectors, selector =>
3132
buildSelectorVariant(selector, screen, separator, message => {
@@ -36,6 +37,22 @@ export default function(config) {
3637
})
3738
)
3839

40+
mediaQuery.append(
41+
// Process nested `atRules`.
42+
responsiveRules.filter(rule => rule.type === 'atrule').map(atRule => {
43+
const clonedAtRule = atRule.clone()
44+
clonedAtRule.nodes.forEach(rule => {
45+
rule.selectors = _.map(rule.selectors, selector => {
46+
const selectorVariant = buildSelectorVariant(selector, screen, separator, message => {
47+
throw rule.error(message)
48+
})
49+
return selectorVariant
50+
})
51+
})
52+
return clonedAtRule
53+
})
54+
)
55+
3956
finalRules.push(mediaQuery)
4057
})
4158

0 commit comments

Comments
 (0)