Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Don't create a `color-mix(…)` fallback with the wrong color when the `color-mix(…)` has no color interpolation method (e.g. `color-mix(currentcolor 50%, transparent)`)
23 changes: 19 additions & 4 deletions packages/tailwindcss/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,22 @@ export function optimizeAst(
walk(ast, (node) => {
if (node.kind !== 'function' || node.value !== 'color-mix') return

// The color interpolation method is optional, e.g.: `color-mix(red 50%, blue)`. In that
// case the node after the first `,` is the second color, not the first one. Lightning CSS
// also doesn't compute a `color-mix(…)` without a color interpolation method, so a
// fallback with inlined colors would be dropped by browsers without `color-mix(…)`.
//
// Custom properties keep their comments, e.g.: `color-mix(/* … */ in oklab, …)`, and the
// value parser doesn't know about comments, so we drop them before we look for `in`. We
// also parse the arguments again without them, otherwise a `,` in a comment would be seen
// as the start of the first color.
let args = ValueParser.toCss(node.nodes)
if (args.includes('/*')) {
args = args.replace(/\/\*[\s\S]*?\*\//g, ' ')
node.nodes = ValueParser.parse(args)
}
if (!/^in\s/i.test(args.trimStart())) return
Comment thread
RobinMalfait marked this conversation as resolved.
Resolved

let containsUnresolvableVars = false
let containsCurrentcolor = false
walk(node.nodes, (node) => {
Expand Down Expand Up @@ -582,10 +598,9 @@ export function optimizeAst(
} else if (requiresPolyfill) {
// Change the colorspace to `srgb` since the fallback values should not be represented as
// `oklab(…)` functions again as their support in Safari <16 is very limited.
//
// The color interpolation method is optional, so there might not be a
// colorspace, e.g.: `color-mix(var(--color-red-500))`.
let colorspace = node.nodes[2]
// The color space follows `in`, which is not necessarily the first node when there is
// leading whitespace.
let colorspace = node.nodes[node.nodes.findIndex((n) => n.kind !== 'separator') + 2]
if (
colorspace?.kind === 'word' &&
(colorspace.value === 'oklab' ||
Expand Down
153 changes: 145 additions & 8 deletions packages/tailwindcss/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6668,7 +6668,7 @@ describe('`color-mix(…)` polyfill', () => {
}

.stacked {
color: lch(55.5764% 89.7903 33.1932 / .25098);
color: #fb2c3640;
}

@supports (color: color-mix(in lab, red, red)) {
Expand Down Expand Up @@ -6768,13 +6768,7 @@ describe('`color-mix(…)` polyfill', () => {
}

.bg-\\[color-mix\\(var\\(--color-red-500\\)\\)\\] {
background-color: color-mix(oklch(63.7% .237 25.331));
}

@supports (color: color-mix(in lab, red, red)) {
.bg-\\[color-mix\\(var\\(--color-red-500\\)\\)\\] {
background-color: color-mix(var(--color-red-500));
}
background-color: color-mix(var(--color-red-500));
}
"
`)
Expand Down Expand Up @@ -6846,6 +6840,149 @@ describe('`color-mix(…)` polyfill', () => {
`)
})

it('uses the first color value as the fallback when the color interpolation method has leading whitespace', async () => {
expect(
await compileCss(
// prettier-ignore
css`
@tailwind utilities;
.foo {
color: color-mix( in oklab, currentcolor 50%, transparent);
}
`,
),
).toMatchInlineSnapshot(`
"
.foo {
color: currentColor;
}

@supports (color: color-mix(in lab, red, red)) {
.foo {
color: color-mix(in oklab, currentcolor 50%, transparent);
}
}
"
`)
})

it('converts the color space of the fallback when the color interpolation method has leading whitespace', async () => {
expect(
await compileCss(
// prettier-ignore
css`
@theme {
--color-red-500: oklch(0.637 0.237 25.331);
}
@tailwind utilities;
.foo {
color: color-mix( in oklab, var(--color-red-500), transparent);
}
`,
),
).toMatchInlineSnapshot(`
"
:root, :host {
--color-red-500: oklch(63.7% .237 25.331);
}

.foo {
color: #fb2c3680;
}

@supports (color: color-mix(in lab, red, red)) {
.foo {
color: color-mix(in oklab, var(--color-red-500), transparent);
}
}
"
`)
})

it('uses the first color value as the fallback when a comment comes before the color interpolation method', async () => {
expect(
await compileCss(css`
@tailwind utilities;
.foo {
--tint: color-mix(/* tint */ in oklab, currentcolor 50%, transparent);
}
`),
).toMatchInlineSnapshot(`
"
.foo {
--tint: currentcolor;
}

@supports (color: color-mix(in lab, red, red)) {
.foo {
--tint: color-mix(/* tint */ in oklab, currentcolor 50%, transparent);
}
}
"
`)
})

it('uses the first color value as the fallback when a comment with a comma comes before the color interpolation method', async () => {
expect(
await compileCss(css`
@tailwind utilities;
.foo {
--tint: color-mix(/* tint, blue */ in oklab, currentcolor 50%, transparent);
}
`),
).toMatchInlineSnapshot(`
"
.foo {
--tint: currentcolor;
}

@supports (color: color-mix(in lab, red, red)) {
.foo {
--tint: color-mix(/* tint, blue */ in oklab, currentcolor 50%, transparent);
}
}
"
`)
})

it('does not create a fallback when the `color-mix(…)` function has no color interpolation method', async () => {
expect(
await compileCss(css`
@theme {
--color-red-500: oklch(63.7% 0.237 25.331);
}
@tailwind utilities;
.current {
color: color-mix(currentcolor 50%, transparent);
}
.unknown {
color: color-mix(var(--unknown), red);
}
.theme {
color: color-mix(var(--color-red-500) 50%, transparent);
}
`),
).toMatchInlineSnapshot(`
"
:root, :host {
--color-red-500: oklch(63.7% .237 25.331);
}

.current {
color: color-mix(currentcolor 50%, transparent);
}

.unknown {
color: color-mix(var(--unknown), red);
}

.theme {
color: color-mix(var(--color-red-500) 50%, transparent);
}
"
`)
})

it('uses the first color value as the fallback when the `color-mix(…)` function contains theme variables that resolves to other variables', async () => {
expect(
await run(
Expand Down
Loading