Skip to content

Commit 4fddd2d

Browse files
authored
Polish matchVariant API (#9313)
* convert the `matchVariant` to look more like `addVariant` With the biggest difference that the `matchVariant` will have a callback function that receives the current value of the variant. * use object as argument for `matchVariant` callback This will allow us to add more properties in the future if needed without breaking changes. - This is a breaking change: `(value) => ...` -> `({ value, other }) => ...` - This is **not** a breaking change: `({ value }) => ...` -> `({ value, other }) => ...` * add types for `matchVariant`
1 parent be429b7 commit 4fddd2d

File tree

3 files changed

+28
-45
lines changed

3 files changed

+28
-45
lines changed

src/lib/setupContextUtils.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -500,18 +500,16 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
500500
}
501501

502502
if (flagEnabled(tailwindConfig, 'matchVariant')) {
503-
api.matchVariant = function (variants, options) {
504-
for (let variant in variants) {
505-
for (let [k, v] of Object.entries(options?.values ?? {})) {
506-
api.addVariant(`${variant}-${k}`, variants[variant](v))
507-
}
508-
509-
api.addVariant(
510-
variant,
511-
Object.assign(({ args }) => variants[variant](args), { [MATCH_VARIANT]: true }),
512-
options
513-
)
503+
api.matchVariant = function (variant, variantFn, options) {
504+
for (let [key, value] of Object.entries(options?.values ?? {})) {
505+
api.addVariant(`${variant}-${key}`, variantFn({ value }))
514506
}
507+
508+
api.addVariant(
509+
variant,
510+
Object.assign(({ args }) => variantFn({ value: args }), { [MATCH_VARIANT]: true }),
511+
options
512+
)
515513
}
516514
}
517515

tests/match-variants.test.js

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ test('partial arbitrary variants', () => {
1111
corePlugins: { preflight: false },
1212
plugins: [
1313
({ matchVariant }) => {
14-
matchVariant({
15-
potato: (flavor) => `.potato-${flavor} &`,
16-
})
14+
matchVariant('potato', ({ value: flavor }) => `.potato-${flavor} &`)
1715
},
1816
],
1917
}
@@ -47,9 +45,7 @@ test('partial arbitrary variants with at-rules', () => {
4745
corePlugins: { preflight: false },
4846
plugins: [
4947
({ matchVariant }) => {
50-
matchVariant({
51-
potato: (flavor) => `@media (potato: ${flavor})`,
52-
})
48+
matchVariant('potato', ({ value: flavor }) => `@media (potato: ${flavor})`)
5349
},
5450
],
5551
}
@@ -86,9 +82,7 @@ test('partial arbitrary variants with at-rules and placeholder', () => {
8682
corePlugins: { preflight: false },
8783
plugins: [
8884
({ matchVariant }) => {
89-
matchVariant({
90-
potato: (flavor) => `@media (potato: ${flavor}) { &:potato }`,
91-
})
85+
matchVariant('potato', ({ value: flavor }) => `@media (potato: ${flavor}) { &:potato }`)
9286
},
9387
],
9488
}
@@ -125,17 +119,12 @@ test('partial arbitrary variants with default values', () => {
125119
corePlugins: { preflight: false },
126120
plugins: [
127121
({ matchVariant }) => {
128-
matchVariant(
129-
{
130-
tooltip: (side) => `&${side}`,
122+
matchVariant('tooltip', ({ value: side }) => `&${side}`, {
123+
values: {
124+
bottom: '[data-location="bottom"]',
125+
top: '[data-location="top"]',
131126
},
132-
{
133-
values: {
134-
bottom: '[data-location="bottom"]',
135-
top: '[data-location="top"]',
136-
},
137-
}
138-
)
127+
})
139128
},
140129
],
141130
}
@@ -170,19 +159,14 @@ test('matched variant values maintain the sort order they are registered in', ()
170159
corePlugins: { preflight: false },
171160
plugins: [
172161
({ matchVariant }) => {
173-
matchVariant(
174-
{
175-
alphabet: (side) => `&${side}`,
162+
matchVariant('alphabet', ({ value: side }) => `&${side}`, {
163+
values: {
164+
a: '[data-value="a"]',
165+
b: '[data-value="b"]',
166+
c: '[data-value="c"]',
167+
d: '[data-value="d"]',
176168
},
177-
{
178-
values: {
179-
a: '[data-value="a"]',
180-
b: '[data-value="b"]',
181-
c: '[data-value="c"]',
182-
d: '[data-value="d"]',
183-
},
184-
}
185-
)
169+
})
186170
},
187171
],
188172
}
@@ -223,9 +207,9 @@ test('matchVariant can return an array of format strings from the function', ()
223207
corePlugins: { preflight: false },
224208
plugins: [
225209
({ matchVariant }) => {
226-
matchVariant({
227-
test: (selector) => selector.split(',').map((selector) => `&.${selector} > *`),
228-
})
210+
matchVariant('test', ({ value: selector }) =>
211+
selector.split(',').map((selector) => `&.${selector} > *`)
212+
)
229213
},
230214
],
231215
}

types/config.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ export interface PluginAPI {
291291
addBase(base: CSSRuleObject | CSSRuleObject[]): void
292292
// for registering custom variants
293293
addVariant(name: string, definition: string | string[] | (() => string) | (() => string)[]): void
294+
matchVariant(name: string, cb: (options: { value: string }) => string | string[]): void
294295
// for looking up values in the user’s theme configuration
295296
theme: <TDefaultValue = Config['theme']>(
296297
path?: string,

0 commit comments

Comments
 (0)