Skip to content

Commit 6c0b8e8

Browse files
authored
Support arbitrary color with opacity modifier (#4723)
1 parent 635d0d8 commit 6c0b8e8

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

src/lib/expandTailwindAtRules.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const PATTERNS = [
1313
/([^<>"'`\s]*\[\w*\("[^"'`\s]*"\)\])/.source, // bg-[url("...")]
1414
/([^<>"'`\s]*\['[^"'`\s]*'\])/.source, // `content-['hello']` but not `content-['hello']']`
1515
/([^<>"'`\s]*\["[^"'`\s]*"\])/.source, // `content-["hello"]` but not `content-["hello"]"]`
16-
/([^<>"'`\s]*\[[^"'`\s]+\])/.source, // `fill-[#bada55]`
16+
/([^<>"'`\s]*\[[^"'`\s]+\][^<>"'`\s]*)/.source, // `fill-[#bada55]`, `fill-[#bada55]/50`
1717
/([^<>"'`\s]*[^"'`\s:])/.source, // `px-1.5`, `uppercase` but not `uppercase:`].join('|')
1818
].join('|')
1919
const BROAD_MATCH_GLOBAL_REGEXP = new RegExp(PATTERNS, 'g')

src/util/pluginUtils.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,23 @@ export function asColor(modifier, lookup = {}, tailwindConfig = {}) {
207207

208208
let [color, alpha] = splitAlpha(modifier)
209209

210-
if (lookup[color] !== undefined) {
210+
if (alpha !== undefined) {
211+
let normalizedColor =
212+
lookup[color] ?? (isArbitraryValue(color) ? color.slice(1, -1) : undefined)
213+
214+
if (normalizedColor === undefined) {
215+
return undefined
216+
}
217+
211218
if (isArbitraryValue(alpha)) {
212-
return withAlphaValue(lookup[color], alpha.slice(1, -1))
219+
return withAlphaValue(normalizedColor, alpha.slice(1, -1))
213220
}
214221

215222
if (tailwindConfig.theme?.opacity?.[alpha] === undefined) {
216223
return undefined
217224
}
218225

219-
return withAlphaValue(lookup[color], tailwindConfig.theme.opacity[alpha])
226+
return withAlphaValue(normalizedColor, tailwindConfig.theme.opacity[alpha])
220227
}
221228

222229
return asValue(modifier, lookup, { validate: validateColor })

tests/color-opacity-modifiers.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,71 @@ test('missing alpha generates nothing', async () => {
6060
})
6161
})
6262

63+
test('arbitrary color with opacity from scale', async () => {
64+
let config = {
65+
mode: 'jit',
66+
purge: [
67+
{
68+
raw: 'bg-[wheat]/50',
69+
},
70+
],
71+
theme: {},
72+
plugins: [],
73+
}
74+
75+
let css = `@tailwind utilities`
76+
77+
return run(css, config).then((result) => {
78+
expect(result.css).toMatchFormattedCss(`
79+
.bg-\\[wheat\\]\\/50 {
80+
background-color: rgb(245 222 179 / 0.5);
81+
}
82+
`)
83+
})
84+
})
85+
86+
test('arbitrary color with arbitrary opacity', async () => {
87+
let config = {
88+
mode: 'jit',
89+
purge: [
90+
{
91+
raw: 'bg-[#bada55]/[0.2]',
92+
},
93+
],
94+
theme: {},
95+
plugins: [],
96+
}
97+
98+
let css = `@tailwind utilities`
99+
100+
return run(css, config).then((result) => {
101+
expect(result.css).toMatchFormattedCss(`
102+
.bg-\\[\\#bada55\\]\\/\\[0\\.2\\] {
103+
background-color: rgb(186 218 85 / 0.2);
104+
}
105+
`)
106+
})
107+
})
108+
109+
test('undefined theme color with opacity from scale', async () => {
110+
let config = {
111+
mode: 'jit',
112+
purge: [
113+
{
114+
raw: 'bg-garbage/50',
115+
},
116+
],
117+
theme: {},
118+
plugins: [],
119+
}
120+
121+
let css = `@tailwind utilities`
122+
123+
return run(css, config).then((result) => {
124+
expect(result.css).toMatchFormattedCss(``)
125+
})
126+
})
127+
63128
test('values not in the opacity config are ignored', async () => {
64129
let config = {
65130
content: [{ raw: html`<div class="bg-red-500/29"></div>` }],

0 commit comments

Comments
 (0)