Skip to content

Commit d776e74

Browse files
committed
add values option to the matchVariant API
1 parent da6f8df commit d776e74

File tree

2 files changed

+97
-63
lines changed

2 files changed

+97
-63
lines changed

src/lib/setupContextUtils.js

+4
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,10 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
471471
},
472472
matchVariant: function (variants, options) {
473473
for (let variant in variants) {
474+
for (let [k, v] of Object.entries(options?.values ?? {})) {
475+
api.addVariant(`${variant}-${k}`, variants[variant](v))
476+
}
477+
474478
api.addVariant(
475479
variant,
476480
Object.assign(

tests/arbitrary-variants.test.js

+93-63
Original file line numberDiff line numberDiff line change
@@ -410,52 +410,58 @@ test('partial arbitrary variants', () => {
410410
let config = {
411411
content: [
412412
{
413-
raw: html`
414-
<div class="container-[sidebar] container-type-inline-size">
415-
<div class="contain-[sidebar,min:500px]:flex"></div>
416-
<div class="contain-[sidebar,max:500px]:flex"></div>
417-
<div class="contain-[min:500px]:flex"></div>
418-
<div class="contain-[max:500px]:flex"></div>
419-
<div class="contain-[500px]:flex"></div>
420-
</div>
421-
`,
413+
raw: html`<div class="potato-[yellow]:bg-yellow-200 potato-[baked]:w-3"></div> `,
422414
},
423415
],
424416
corePlugins: { preflight: false },
425417
plugins: [
426-
({ addUtilities, matchVariant, matchUtilities }) => {
427-
addUtilities({
428-
'.container-type-size': { 'container-type': 'size' },
429-
'.container-type-inline-size': { 'container-type': 'inline-size' },
430-
'.container-type-block-size': { 'container-type': 'block-size' },
431-
'.container-type-style': { 'container-type': 'style' },
432-
'.container-type-state': { 'container-type': 'state' },
418+
({ matchVariant }) => {
419+
matchVariant({
420+
potato: (flavor) => `.potato-${flavor} &`,
433421
})
422+
},
423+
],
424+
}
434425

435-
matchUtilities({
436-
container: (value) => {
437-
return {
438-
'container-name': value,
439-
}
440-
},
441-
})
426+
let input = css`
427+
@tailwind utilities;
428+
`
442429

443-
matchVariant({
444-
contain: (args) => {
445-
if (args.includes(',')) {
446-
let [name, query] = args.split(',')
447-
let [type, value] = query.split(':')
448-
return `@container ${name} (${
449-
{ min: 'min-width', max: 'max-width' }[type]
450-
}: ${value})`
451-
} else if (args.includes(':')) {
452-
let [type, value] = args.split(':')
453-
return `@container (${{ min: 'min-width', max: 'max-width' }[type]}: ${value})`
454-
} else {
455-
return `@container (min-width: ${args})`
456-
}
430+
return run(input, config).then((result) => {
431+
expect(result.css).toMatchFormattedCss(css`
432+
.potato-baked .potato-\[baked\]\:w-3 {
433+
width: 0.75rem;
434+
}
435+
436+
.potato-yellow .potato-\[yellow\]\:bg-yellow-200 {
437+
--tw-bg-opacity: 1;
438+
background-color: rgb(254 240 138 / var(--tw-bg-opacity));
439+
}
440+
`)
441+
})
442+
})
443+
444+
test('partial arbitrary variants with default values', () => {
445+
let config = {
446+
content: [
447+
{
448+
raw: html`<div class="tooltip-bottom:mt-2 tooltip-top:mb-2"></div>`,
449+
},
450+
],
451+
corePlugins: { preflight: false },
452+
plugins: [
453+
({ matchVariant }) => {
454+
matchVariant(
455+
{
456+
tooltip: (side) => `&${side}`,
457457
},
458-
})
458+
{
459+
values: {
460+
bottom: '[data-location="bottom"]',
461+
top: '[data-location="top"]',
462+
},
463+
}
464+
)
459465
},
460466
],
461467
}
@@ -466,42 +472,66 @@ test('partial arbitrary variants', () => {
466472

467473
return run(input, config).then((result) => {
468474
expect(result.css).toMatchFormattedCss(css`
469-
.container-type-inline-size {
470-
container-type: inline-size;
475+
.tooltip-bottom\:mt-2[data-location='bottom'] {
476+
margin-top: 0.5rem;
471477
}
472478
473-
.container-\[sidebar\] {
474-
container-name: sidebar;
479+
.tooltip-top\:mb-2[data-location='top'] {
480+
margin-bottom: 0.5rem;
475481
}
482+
`)
483+
})
484+
})
476485

477-
@container sidebar (min-width: 500px) {
478-
.contain-\[sidebar\2c min\:500px\]\:flex {
479-
display: flex;
480-
}
481-
}
486+
test('matched variant values maintain the sort order they are registered in', () => {
487+
let config = {
488+
content: [
489+
{
490+
raw: html`<div
491+
class="alphabet-c:underline alphabet-a:underline alphabet-d:underline alphabet-b:underline"
492+
></div>`,
493+
},
494+
],
495+
corePlugins: { preflight: false },
496+
plugins: [
497+
({ matchVariant }) => {
498+
matchVariant(
499+
{
500+
alphabet: (side) => `&${side}`,
501+
},
502+
{
503+
values: {
504+
a: '[data-value="a"]',
505+
b: '[data-value="b"]',
506+
c: '[data-value="c"]',
507+
d: '[data-value="d"]',
508+
},
509+
}
510+
)
511+
},
512+
],
513+
}
482514

483-
@container sidebar (max-width: 500px) {
484-
.contain-\[sidebar\2c max\:500px\]\:flex {
485-
display: flex;
486-
}
515+
let input = css`
516+
@tailwind utilities;
517+
`
518+
519+
return run(input, config).then((result) => {
520+
expect(result.css).toMatchFormattedCss(css`
521+
.alphabet-a\:underline[data-value='a'] {
522+
text-decoration-line: underline;
487523
}
488524
489-
@container (min-width: 500px) {
490-
.contain-\[min\:500px\]\:flex {
491-
display: flex;
492-
}
525+
.alphabet-b\:underline[data-value='b'] {
526+
text-decoration-line: underline;
493527
}
494528
495-
@container (max-width: 500px) {
496-
.contain-\[max\:500px\]\:flex {
497-
display: flex;
498-
}
529+
.alphabet-c\:underline[data-value='c'] {
530+
text-decoration-line: underline;
499531
}
500532
501-
@container (min-width: 500px) {
502-
.contain-\[500px\]\:flex {
503-
display: flex;
504-
}
533+
.alphabet-d\:underline[data-value='d'] {
534+
text-decoration-line: underline;
505535
}
506536
`)
507537
})

0 commit comments

Comments
 (0)