Skip to content

Commit 205590b

Browse files
committed
WIP
1 parent be51739 commit 205590b

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/lib/generateRules.js

+9
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ function applyVariant(variant, matches, context) {
127127
return matches
128128
}
129129

130+
let args
131+
132+
// Find partial arbitrary variants
133+
if (variant.endsWith(']') && !variant.startsWith('[]')) {
134+
args = variant.slice(variant.lastIndexOf('[') + 1, -1)
135+
variant = variant.slice(0, variant.indexOf(args) - 1 /* - */ - 1 /* [ */)
136+
}
137+
130138
// Register arbitrary variants
131139
if (isArbitraryValue(variant) && !context.variantMap.has(variant)) {
132140
let selector = normalize(variant.slice(1, -1))
@@ -200,6 +208,7 @@ function applyVariant(variant, matches, context) {
200208
format(selectorFormat) {
201209
collectedFormats.push(selectorFormat)
202210
},
211+
args,
203212
})
204213

205214
if (typeof ruleWithVariant === 'string') {

src/lib/setupContextUtils.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import isValidArbitraryValue from '../util/isValidArbitraryValue'
2222
import { generateRules } from './generateRules'
2323
import { hasContentChanged } from './cacheInvalidation.js'
2424

25+
let MATCH_VARIANT = Symbol()
26+
2527
function prefix(context, selector) {
2628
let prefix = context.tailwindConfig.prefix
2729
return typeof prefix === 'function' ? prefix(selector) : prefix + selector
@@ -215,13 +217,18 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
215217
return context.tailwindConfig.prefix + identifier
216218
}
217219

218-
return {
220+
let api = {
219221
addVariant(variantName, variantFunctions, options = {}) {
220222
variantFunctions = [].concat(variantFunctions).map((variantFunction) => {
221223
if (typeof variantFunction !== 'string') {
222224
// Safelist public API functions
223-
return ({ modifySelectors, container, separator }) => {
224-
return variantFunction({ modifySelectors, container, separator })
225+
return ({ args, modifySelectors, container, separator }) => {
226+
return variantFunction(
227+
Object.assign(
228+
{ modifySelectors, container, separator },
229+
variantFunction[MATCH_VARIANT] && { args }
230+
)
231+
)
225232
}
226233
}
227234

@@ -444,7 +451,18 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
444451
context.candidateRuleMap.get(prefixedIdentifier).push(withOffsets)
445452
}
446453
},
454+
matchVariant: function (variants, options) {
455+
for (let variant in variants) {
456+
api.addVariant(
457+
variant,
458+
Object.assign(({ args }) => variants[variant](args), { [MATCH_VARIANT]: true }),
459+
options
460+
)
461+
}
462+
},
447463
}
464+
465+
return api
448466
}
449467

450468
let fileModifiedMapCache = new WeakMap()

0 commit comments

Comments
 (0)