Change match variant wrap modifier in an object

Needed for compat w/ some group and peer plugins
This commit is contained in:
Jordan Pittman 2022-10-12 17:12:47 -04:00
parent 2049989184
commit a9015fc14c
3 changed files with 20 additions and 14 deletions

View file

@ -144,27 +144,27 @@ export let variantPlugins = {
}
let variants = {
group: ({ modifier }) =>
group: (_, { modifier }) =>
modifier ? [`:merge(.group\\/${modifier})`, ' &'] : [`:merge(.group)`, ' &'],
peer: ({ modifier }) =>
peer: (_, { modifier }) =>
modifier ? [`:merge(.peer\\/${modifier})`, ' ~ &'] : [`:merge(.peer)`, ' ~ &'],
}
for (let [name, fn] of Object.entries(variants)) {
matchVariant(
name,
(value = '', modifier) => {
if (modifier) {
(value = '', extra) => {
if (extra.modifier) {
log.warn(`modifier-${name}-experimental`, [
`The ${name} variant modifier feature in Tailwind CSS is currently in preview.`,
'Preview features are not covered by semver, and may be improved in breaking ways at any time.',
])
}
let result = normalize(typeof value === 'function' ? value(ctx) : value)
let result = normalize(typeof value === 'function' ? value(extra) : value)
if (!result.includes('&')) result = '&' + result
let [a, b] = fn({ modifier })
let [a, b] = fn('', extra)
return result.replace(/&(\S+)?/g, (_, pseudo = '') => a + pseudo + b)
},
{ values: Object.fromEntries(pseudoVariants) }

View file

@ -525,18 +525,24 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
for (let [key, value] of Object.entries(options?.values ?? {})) {
api.addVariant(
isSpecial ? `${variant}${key}` : `${variant}-${key}`,
Object.assign(({ args }) => variantFn(value, args.modifier), {
[MATCH_VARIANT]: true,
}),
Object.assign(
({ args, container }) => variantFn(value, { modifier: args.modifier, container }),
{
[MATCH_VARIANT]: true,
}
),
{ ...options, value, id }
)
}
api.addVariant(
variant,
Object.assign(({ args }) => variantFn(args.value, args.modifier), {
[MATCH_VARIANT]: true,
}),
Object.assign(
({ args, container }) => variantFn(args.value, { modifier: args.modifier, container }),
{
[MATCH_VARIANT]: true,
}
),
{ ...options, id }
)
},

4
types/config.d.ts vendored
View file

@ -298,9 +298,9 @@ export interface PluginAPI {
addVariant(name: string, definition: string | string[] | (() => string) | (() => string)[]): void
matchVariant<T = string>(
name: string,
cb: (value: T | string, modifier: string | null) => string | string[],
cb: (value: T | string, extra: { modifier: string | null }) => string | string[],
options?: {
values?: KeyValuePair<string, T>,
values?: KeyValuePair<string, T>
sort?(
a: { value: T | string; modifier: string | null },
b: { value: T | string; modifier: string | null }