Skip to content

Commit dafb3d3

Browse files
committed
Implement modifySelectors API for addVariant
1 parent 86d7ab3 commit dafb3d3

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/lib/generateRules.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ const postcss = require('postcss')
22
const { default: parseObjectStyles } = require('tailwindcss/lib/util/parseObjectStyles')
33
const { transformAllSelectors } = require('../pluginUtils')
44
const { toPostCssNode, isPlainObject } = require('./utils')
5+
const selectorParser = require('postcss-selector-parser')
6+
7+
let classNameParser = selectorParser((selectors) => {
8+
return selectors.first.filter(({ type }) => type === 'class').pop().value
9+
})
10+
11+
function getClassNameFromSelector(selector) {
12+
return classNameParser.transformSync(selector)
13+
}
514

615
// Generate match permutations for a class candidate, like:
716
// ['ring-offset-blue', '100']
@@ -47,7 +56,25 @@ function applyVariant(variant, matches, { variantMap }) {
4756

4857
let container = postcss.root({ nodes: [rule] })
4958

50-
let ruleWithVariant = applyThisVariant({ container })
59+
function modifySelectors(modifierFunction) {
60+
container.each((rule) => {
61+
if (rule.type !== 'rule') {
62+
return
63+
}
64+
65+
rule.selectors = rule.selectors.map((selector) => {
66+
return modifierFunction({
67+
get className() {
68+
return getClassNameFromSelector(selector)
69+
},
70+
selector,
71+
})
72+
})
73+
})
74+
return container
75+
}
76+
77+
let ruleWithVariant = applyThisVariant({ container, separator: ':', modifySelectors })
5178

5279
if (ruleWithVariant === null) {
5380
continue

0 commit comments

Comments
 (0)