From 48ab491d3a77bb1485147359f63f54670952ceae Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Sun, 26 Sep 2021 11:39:03 -0400 Subject: [PATCH] Support arbitrary color with opacity modifier --- src/lib/expandTailwindAtRules.js | 2 +- src/util/pluginUtils.js | 13 ++++-- tests/color-opacity-modifiers.test.js | 65 +++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/lib/expandTailwindAtRules.js b/src/lib/expandTailwindAtRules.js index bdc8e68d4a92..bf8273223801 100644 --- a/src/lib/expandTailwindAtRules.js +++ b/src/lib/expandTailwindAtRules.js @@ -13,7 +13,7 @@ const PATTERNS = [ /([^<>"'`\s]*\[\w*\("[^"'`\s]*"\)\])/.source, // bg-[url("...")] /([^<>"'`\s]*\['[^"'`\s]*'\])/.source, // `content-['hello']` but not `content-['hello']']` /([^<>"'`\s]*\["[^"'`\s]*"\])/.source, // `content-["hello"]` but not `content-["hello"]"]` - /([^<>"'`\s]*\[[^"'`\s]+\])/.source, // `fill-[#bada55]` + /([^<>"'`\s]*\[[^"'`\s]+\][^<>"'`\s]*)/.source, // `fill-[#bada55]`, `fill-[#bada55]/50` /([^<>"'`\s]*[^"'`\s:])/.source, // `px-1.5`, `uppercase` but not `uppercase:`].join('|') ].join('|') const BROAD_MATCH_GLOBAL_REGEXP = new RegExp(PATTERNS, 'g') diff --git a/src/util/pluginUtils.js b/src/util/pluginUtils.js index 73c4170a9b36..9fdbda93b381 100644 --- a/src/util/pluginUtils.js +++ b/src/util/pluginUtils.js @@ -207,16 +207,23 @@ export function asColor(modifier, lookup = {}, tailwindConfig = {}) { let [color, alpha] = splitAlpha(modifier) - if (lookup[color] !== undefined) { + if (alpha !== undefined) { + let normalizedColor = + lookup[color] ?? (isArbitraryValue(color) ? color.slice(1, -1) : undefined) + + if (normalizedColor === undefined) { + return undefined + } + if (isArbitraryValue(alpha)) { - return withAlphaValue(lookup[color], alpha.slice(1, -1)) + return withAlphaValue(normalizedColor, alpha.slice(1, -1)) } if (tailwindConfig.theme?.opacity?.[alpha] === undefined) { return undefined } - return withAlphaValue(lookup[color], tailwindConfig.theme.opacity[alpha]) + return withAlphaValue(normalizedColor, tailwindConfig.theme.opacity[alpha]) } return asValue(modifier, lookup, { validate: validateColor }) diff --git a/tests/color-opacity-modifiers.test.js b/tests/color-opacity-modifiers.test.js index 7dd78f3c8b60..51da84aacae2 100644 --- a/tests/color-opacity-modifiers.test.js +++ b/tests/color-opacity-modifiers.test.js @@ -60,6 +60,71 @@ test('missing alpha generates nothing', async () => { }) }) +test('arbitrary color with opacity from scale', async () => { + let config = { + mode: 'jit', + purge: [ + { + raw: 'bg-[wheat]/50', + }, + ], + theme: {}, + plugins: [], + } + + let css = `@tailwind utilities` + + return run(css, config).then((result) => { + expect(result.css).toMatchFormattedCss(` + .bg-\\[wheat\\]\\/50 { + background-color: rgb(245 222 179 / 0.5); + } + `) + }) +}) + +test('arbitrary color with arbitrary opacity', async () => { + let config = { + mode: 'jit', + purge: [ + { + raw: 'bg-[#bada55]/[0.2]', + }, + ], + theme: {}, + plugins: [], + } + + let css = `@tailwind utilities` + + return run(css, config).then((result) => { + expect(result.css).toMatchFormattedCss(` + .bg-\\[\\#bada55\\]\\/\\[0\\.2\\] { + background-color: rgb(186 218 85 / 0.2); + } + `) + }) +}) + +test('undefined theme color with opacity from scale', async () => { + let config = { + mode: 'jit', + purge: [ + { + raw: 'bg-garbage/50', + }, + ], + theme: {}, + plugins: [], + } + + let css = `@tailwind utilities` + + return run(css, config).then((result) => { + expect(result.css).toMatchFormattedCss(``) + }) +}) + test('values not in the opacity config are ignored', async () => { let config = { content: [{ raw: html`
` }],