Skip to content

Commit 634a0e6

Browse files
authored
Merge pull request #2211 from tailwindlabs/apply-to-rule-with-multiple-selectors
Fix issue where couldn't apply variant classes to rule with multiple selectors
2 parents 8fc7087 + 3839c74 commit 634a0e6

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

__tests__/applyComplexClasses.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,3 +961,35 @@ test('you can apply utility classes when a selector is used for the important op
961961
expect(result.warnings().length).toBe(0)
962962
})
963963
})
964+
965+
test('you can apply classes to a rule with multiple selectors', () => {
966+
const input = `
967+
@supports (display: grid) {
968+
.foo, h1 > .bar * {
969+
@apply float-left opacity-50 hover:opacity-100 md:float-right;
970+
}
971+
}
972+
`
973+
974+
const expected = `
975+
@supports (display: grid) {
976+
.foo, h1 > .bar * {
977+
float: left;
978+
opacity: 0.5;
979+
}
980+
.foo:hover, h1 > .bar *:hover {
981+
opacity: 1;
982+
}
983+
@media (min-width: 768px) {
984+
.foo, h1 > .bar * {
985+
float: right;
986+
}
987+
}
988+
}
989+
`
990+
991+
return run(input).then(result => {
992+
expect(result.css).toMatchCss(expected)
993+
expect(result.warnings().length).toBe(0)
994+
})
995+
})

src/flagged/applyComplexClasses.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const tailwindApplyPlaceholder = selectorParser.attribute({
3939
attribute: '__TAILWIND-APPLY-PLACEHOLDER__',
4040
})
4141

42-
function generateRulesFromApply({ rule, utilityName: className, classPosition }, replaceWith) {
42+
function generateRulesFromApply({ rule, utilityName: className, classPosition }, replaceWiths) {
4343
const parser = selectorParser(selectors => {
4444
let i = 0
4545
selectors.walkClasses(c => {
@@ -49,11 +49,13 @@ function generateRulesFromApply({ rule, utilityName: className, classPosition },
4949
})
5050
})
5151

52-
const processedSelectors = rule.selectors.map(selector => {
52+
const processedSelectors = _.flatMap(rule.selectors, selector => {
5353
// You could argue we should make this replacement at the AST level, but if we believe
5454
// the placeholder string is safe from collisions then it is safe to do this is a simple
5555
// string replacement, and much, much faster.
56-
return parser.processSync(selector).replace('[__TAILWIND-APPLY-PLACEHOLDER__]', replaceWith)
56+
return replaceWiths.map(replaceWith =>
57+
parser.processSync(selector).replace('[__TAILWIND-APPLY-PLACEHOLDER__]', replaceWith)
58+
)
5759
})
5860

5961
const cloned = rule.clone()
@@ -242,7 +244,7 @@ function processApplyAtRules(css, lookupTree, config) {
242244
// Get new rules with the utility portion of the selector replaced with the new selector
243245
const rulesToInsert = [
244246
...applys.map(applyUtility => {
245-
return generateRulesFromApply(applyUtility, rule.selector)
247+
return generateRulesFromApply(applyUtility, rule.selectors)
246248
}),
247249
afterRule,
248250
]

0 commit comments

Comments
 (0)