Skip to content

Commit 0e411d9

Browse files
Make polyfill work when the theme variable resolves to another var
1 parent 57e55a6 commit 0e411d9

File tree

2 files changed

+93
-9
lines changed

2 files changed

+93
-9
lines changed

packages/tailwindcss/src/ast.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,43 @@ export function optimizeAst(
340340
requiresPolyfill = true
341341
return
342342
}
343-
if (node.kind !== 'function' || node.value !== 'var') return
344-
let firstChild = node.nodes[0]
345-
if (!firstChild || firstChild.kind !== 'word') return
346343

347-
requiresPolyfill = true
344+
let varNode: ValueParser.ValueAstNode | null = node
345+
let inlinedColor: string | null = null
346+
let seenVariables = new Set<string>()
347+
do {
348+
if (varNode.kind !== 'function' || varNode.value !== 'var') return
349+
let firstChild = varNode.nodes[0]
350+
if (!firstChild || firstChild.kind !== 'word') return
348351

349-
let inlinedColor = designSystem.theme.resolveValue(null, [firstChild.value as any])
350-
if (!inlinedColor) {
351-
containsUnresolvableVars = true
352-
return
353-
}
352+
let variableName = firstChild.value
353+
354+
if (seenVariables.has(variableName)) {
355+
containsUnresolvableVars = true
356+
return
357+
}
358+
359+
seenVariables.add(variableName)
360+
361+
requiresPolyfill = true
362+
363+
inlinedColor = designSystem.theme.resolveValue(null, [firstChild.value as any])
364+
if (!inlinedColor) {
365+
containsUnresolvableVars = true
366+
return
367+
}
368+
if (inlinedColor.toLowerCase() === 'currentcolor') {
369+
containsCurrentcolor = true
370+
return
371+
}
372+
373+
if (inlinedColor.startsWith('var(')) {
374+
let subAst = ValueParser.parse(inlinedColor)
375+
varNode = subAst[0]
376+
} else {
377+
varNode = null
378+
}
379+
} while (varNode)
354380

355381
replaceWith({ kind: 'word', value: inlinedColor })
356382
})

packages/tailwindcss/src/index.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,6 +4764,36 @@ describe('`color-mix(…)` polyfill', () => {
47644764
`)
47654765
})
47664766

4767+
it('creates an inlined variable version of the color-mix(…) usages when it resolves to a var(…) containing another theme variable', async () => {
4768+
await expect(
4769+
compileCss(
4770+
css`
4771+
@theme {
4772+
--color-red: var(--color-red-500);
4773+
--color-red-500: oklch(63.7% 0.237 25.331);
4774+
}
4775+
@tailwind utilities;
4776+
`,
4777+
['text-red/50'],
4778+
),
4779+
).resolves.toMatchInlineSnapshot(`
4780+
":root, :host {
4781+
--color-red: var(--color-red-500);
4782+
--color-red-500: oklch(63.7% .237 25.331);
4783+
}
4784+
4785+
.text-red\\/50 {
4786+
color: #fb2c3680;
4787+
}
4788+
4789+
@supports (color: color-mix(in lab, red, red)) {
4790+
.text-red\\/50 {
4791+
color: color-mix(in oklab, var(--color-red) 50%, transparent);
4792+
}
4793+
}"
4794+
`)
4795+
})
4796+
47674797
it('works for color values in the first and second position', async () => {
47684798
await expect(
47694799
compileCss(
@@ -4971,6 +5001,34 @@ describe('`color-mix(…)` polyfill', () => {
49715001
`)
49725002
})
49735003

5004+
it('uses the first color value as the fallback when the `color-mix(…)` function contains theme variables that resolves to other variables', async () => {
5005+
await expect(
5006+
compileCss(
5007+
css`
5008+
@tailwind utilities;
5009+
@theme {
5010+
--color-red: var(--my-red);
5011+
}
5012+
`,
5013+
['text-red/50'],
5014+
),
5015+
).resolves.toMatchInlineSnapshot(`
5016+
".text-red\\/50 {
5017+
color: var(--color-red);
5018+
}
5019+
5020+
@supports (color: color-mix(in lab, red, red)) {
5021+
.text-red\\/50 {
5022+
color: color-mix(in oklab, var(--color-red) 50%, transparent);
5023+
}
5024+
}
5025+
5026+
:root, :host {
5027+
--color-red: var(--my-red);
5028+
}"
5029+
`)
5030+
})
5031+
49745032
it('uses the first color value of the inner most `color-mix(…)` function as the fallback when nested `color-mix(…)` function all contain non-theme variables', async () => {
49755033
await expect(
49765034
compileCss(

0 commit comments

Comments
 (0)