From 8d55e9acd058568574334c9050aca93721a254ca Mon Sep 17 00:00:00 2001 From: Adam Wathan <4323180+adamwathan@users.noreply.github.com> Date: Tue, 29 Nov 2022 10:02:26 -0500 Subject: [PATCH 1/2] Support variable shorthand for arbitrary modifiers --- src/util/pluginUtils.js | 12 ++++++++++-- tests/arbitrary-values.test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/util/pluginUtils.js b/src/util/pluginUtils.js index 5c4821d3cbca..e347f404ba8a 100644 --- a/src/util/pluginUtils.js +++ b/src/util/pluginUtils.js @@ -133,6 +133,14 @@ export function parseColorFormat(value) { return value } +function unwrapArbitraryModifier(modifier) { + modifier = modifier.slice(1, -1) + if (modifier.startsWith('--')) { + modifier = `var(${modifier})` + } + return modifier +} + export function asColor(modifier, options = {}, { tailwindConfig = {} } = {}) { if (options.values?.[modifier] !== undefined) { return parseColorFormat(options.values?.[modifier]) @@ -153,7 +161,7 @@ export function asColor(modifier, options = {}, { tailwindConfig = {} } = {}) { normalizedColor = parseColorFormat(normalizedColor) if (isArbitraryValue(alpha)) { - return withAlphaValue(normalizedColor, alpha.slice(1, -1)) + return withAlphaValue(normalizedColor, unwrapArbitraryModifier(alpha)) } if (tailwindConfig.theme?.opacity?.[alpha] === undefined) { @@ -287,7 +295,7 @@ export function* getMatchingTypes(types, rawModifier, options, tailwindConfig) { if (configValue !== null) { utilityModifier = configValue } else if (isArbitraryValue(utilityModifier)) { - utilityModifier = utilityModifier.slice(1, -1) + utilityModifier = unwrapArbitraryModifier(utilityModifier) } } } diff --git a/tests/arbitrary-values.test.js b/tests/arbitrary-values.test.js index ec3b9494161e..b4445907426c 100644 --- a/tests/arbitrary-values.test.js +++ b/tests/arbitrary-values.test.js @@ -575,3 +575,31 @@ it('can use CSS variables as arbitrary values without `var()`', () => { `) }) }) + +it('can use CSS variables as arbitrary modifiers without `var()`', () => { + let config = { + content: [ + { + raw: html`
`, + }, + ], + corePlugins: { preflight: false }, + plugins: [], + } + + let input = css` + @tailwind utilities; + ` + + return run(input, config).then((result) => { + expect(result.css).toMatchFormattedCss(css` + .bg-red-500\/\[--opacity\] { + background-color: rgb(239 68 68 / var(--opacity)); + } + .text-sm\/\[--line-height\] { + font-size: 0.875rem; + line-height: var(--line-height); + } + `) + }) +}) From beba2be9941115315c4ccc6ea79d4c4726c914d4 Mon Sep 17 00:00:00 2001 From: Adam Wathan <4323180+adamwathan@users.noreply.github.com> Date: Tue, 29 Nov 2022 10:10:10 -0500 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71e59f1c2719..aff6c5f9139f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Add `line-height` modifier support to `font-size` utilities ([#9875](https://github.com/tailwindlabs/tailwindcss/pull/9875)) -- Support using variables as arbitrary values without `var(...)` ([#9880](https://github.com/tailwindlabs/tailwindcss/pull/9880)) +- Support using variables as arbitrary values without `var(...)` ([#9880](https://github.com/tailwindlabs/tailwindcss/pull/9880), [#9962](https://github.com/tailwindlabs/tailwindcss/pull/9962)) ### Fixed