From d776e747fd72dec4bf6c4e23190f3913de14ba89 Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Tue, 17 May 2022 16:57:32 +0200 Subject: [PATCH] add `values` option to the `matchVariant` API --- src/lib/setupContextUtils.js | 4 + tests/arbitrary-variants.test.js | 156 ++++++++++++++++++------------- 2 files changed, 97 insertions(+), 63 deletions(-) diff --git a/src/lib/setupContextUtils.js b/src/lib/setupContextUtils.js index b233ab76a16f..d572c2137333 100644 --- a/src/lib/setupContextUtils.js +++ b/src/lib/setupContextUtils.js @@ -471,6 +471,10 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs }, matchVariant: function (variants, options) { for (let variant in variants) { + for (let [k, v] of Object.entries(options?.values ?? {})) { + api.addVariant(`${variant}-${k}`, variants[variant](v)) + } + api.addVariant( variant, Object.assign( diff --git a/tests/arbitrary-variants.test.js b/tests/arbitrary-variants.test.js index 534091cfb470..191d7fe3ca85 100644 --- a/tests/arbitrary-variants.test.js +++ b/tests/arbitrary-variants.test.js @@ -410,52 +410,58 @@ test('partial arbitrary variants', () => { let config = { content: [ { - raw: html` -
-
-
-
-
-
-
- `, + raw: html`
`, }, ], corePlugins: { preflight: false }, plugins: [ - ({ addUtilities, matchVariant, matchUtilities }) => { - addUtilities({ - '.container-type-size': { 'container-type': 'size' }, - '.container-type-inline-size': { 'container-type': 'inline-size' }, - '.container-type-block-size': { 'container-type': 'block-size' }, - '.container-type-style': { 'container-type': 'style' }, - '.container-type-state': { 'container-type': 'state' }, + ({ matchVariant }) => { + matchVariant({ + potato: (flavor) => `.potato-${flavor} &`, }) + }, + ], + } - matchUtilities({ - container: (value) => { - return { - 'container-name': value, - } - }, - }) + let input = css` + @tailwind utilities; + ` - matchVariant({ - contain: (args) => { - if (args.includes(',')) { - let [name, query] = args.split(',') - let [type, value] = query.split(':') - return `@container ${name} (${ - { min: 'min-width', max: 'max-width' }[type] - }: ${value})` - } else if (args.includes(':')) { - let [type, value] = args.split(':') - return `@container (${{ min: 'min-width', max: 'max-width' }[type]}: ${value})` - } else { - return `@container (min-width: ${args})` - } + return run(input, config).then((result) => { + expect(result.css).toMatchFormattedCss(css` + .potato-baked .potato-\[baked\]\:w-3 { + width: 0.75rem; + } + + .potato-yellow .potato-\[yellow\]\:bg-yellow-200 { + --tw-bg-opacity: 1; + background-color: rgb(254 240 138 / var(--tw-bg-opacity)); + } + `) + }) +}) + +test('partial arbitrary variants with default values', () => { + let config = { + content: [ + { + raw: html`
`, + }, + ], + corePlugins: { preflight: false }, + plugins: [ + ({ matchVariant }) => { + matchVariant( + { + tooltip: (side) => `&${side}`, }, - }) + { + values: { + bottom: '[data-location="bottom"]', + top: '[data-location="top"]', + }, + } + ) }, ], } @@ -466,42 +472,66 @@ test('partial arbitrary variants', () => { return run(input, config).then((result) => { expect(result.css).toMatchFormattedCss(css` - .container-type-inline-size { - container-type: inline-size; + .tooltip-bottom\:mt-2[data-location='bottom'] { + margin-top: 0.5rem; } - .container-\[sidebar\] { - container-name: sidebar; + .tooltip-top\:mb-2[data-location='top'] { + margin-bottom: 0.5rem; } + `) + }) +}) - @container sidebar (min-width: 500px) { - .contain-\[sidebar\2c min\:500px\]\:flex { - display: flex; - } - } +test('matched variant values maintain the sort order they are registered in', () => { + let config = { + content: [ + { + raw: html`
`, + }, + ], + corePlugins: { preflight: false }, + plugins: [ + ({ matchVariant }) => { + matchVariant( + { + alphabet: (side) => `&${side}`, + }, + { + values: { + a: '[data-value="a"]', + b: '[data-value="b"]', + c: '[data-value="c"]', + d: '[data-value="d"]', + }, + } + ) + }, + ], + } - @container sidebar (max-width: 500px) { - .contain-\[sidebar\2c max\:500px\]\:flex { - display: flex; - } + let input = css` + @tailwind utilities; + ` + + return run(input, config).then((result) => { + expect(result.css).toMatchFormattedCss(css` + .alphabet-a\:underline[data-value='a'] { + text-decoration-line: underline; } - @container (min-width: 500px) { - .contain-\[min\:500px\]\:flex { - display: flex; - } + .alphabet-b\:underline[data-value='b'] { + text-decoration-line: underline; } - @container (max-width: 500px) { - .contain-\[max\:500px\]\:flex { - display: flex; - } + .alphabet-c\:underline[data-value='c'] { + text-decoration-line: underline; } - @container (min-width: 500px) { - .contain-\[500px\]\:flex { - display: flex; - } + .alphabet-d\:underline[data-value='d'] { + text-decoration-line: underline; } `) })