From f94d1f13d78c061358c935128ef42f21545dc0fd Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 23 Sep 2026 01:02:51 +0800 Subject: [PATCH 1/4] fix(compiler): omit absent color-mix weights from serialized descriptors --- src/__tests__/compiler/declarations.test.tsx | 42 ++++++++++++++++++++ src/compiler/declarations.ts | 19 ++++----- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/__tests__/compiler/declarations.test.tsx b/src/__tests__/compiler/declarations.test.tsx index 4dbd378f..ffdead23 100644 --- a/src/__tests__/compiler/declarations.test.tsx +++ b/src/__tests__/compiler/declarations.test.tsx @@ -1,3 +1,4 @@ +import { compile } from "react-native-css/compiler"; import { compileWithAutoDebug } from "react-native-css/jest"; // prettier-ignore @@ -29,3 +30,44 @@ test.each(tests)("declarations for %s", (declarations, expected) => { expect(myClassDeclarations).toStrictEqual(expected); }); + +test.each([ + [ + "left weight only", + "color-mix(in oklab, var(--left) 90%, transparent)", + ["oklab", [{}, "var", "left", 1], "90%", "transparent"], + ], + [ + "right weight only", + "color-mix(in srgb, var(--left), var(--right) 25%)", + ["srgb", [{}, "var", "left", 1], [{}, "var", "right", 1], "25%"], + ], + [ + "neither weight", + "color-mix(in srgb, var(--left), var(--right))", + ["srgb", [{}, "var", "left", 1], [{}, "var", "right", 1]], + ], +] as const)( + "color-mix serializes without absent weights: %s", + (_, value, expected) => { + const stylesheet = JSON.parse( + JSON.stringify( + compile(`.test { background-color: ${value}; }`, { + inlineVariables: false, + }).stylesheet(), + ), + ) as ReturnType["stylesheet"]>; + const declaration = stylesheet.s?.[0]?.[1]?.[0]?.d?.find( + (entry) => + Array.isArray(entry) && + Array.isArray(entry[0]) && + entry[0][1] === "colorMix", + ); + + expect( + Array.isArray(declaration) && Array.isArray(declaration[0]) + ? declaration[0][2] + : undefined, + ).toStrictEqual(expected); + }, +); diff --git a/src/compiler/declarations.ts b/src/compiler/declarations.ts index 1591d0ab..1dbeeff9 100644 --- a/src/compiler/declarations.ts +++ b/src/compiler/declarations.ts @@ -2798,17 +2798,14 @@ export function parseColorMix( return; } - return [ - {}, - "colorMix", - [ - colorSpaceArg, - leftColorArg, - leftColorPercentage, - rightColorArg, - rightColorPercentage, - ], - ]; + // Optional weights must not leave undefined array entries: Metro serializes + // them as null, which the native colorMix resolver cannot consume. + const args: StyleDescriptor[] = [colorSpaceArg, leftColorArg]; + if (leftColorPercentage !== undefined) args.push(leftColorPercentage); + args.push(rightColorArg); + if (rightColorPercentage !== undefined) args.push(rightColorPercentage); + + return [{}, "colorMix", args]; } export function parseCalcArguments( From e5776bb8f5d88fa0a7a38e509dae45e649d00b28 Mon Sep 17 00:00:00 2001 From: "Nate I." Date: Wed, 28 Oct 2026 09:08:35 +0200 Subject: [PATCH 2/4] fix: consume omitted color-mix weights after native serialization --- src/__tests__/native/color-mix.test.tsx | 21 +++++++++++++++++++++ src/native/styles/functions/color-mix.ts | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/src/__tests__/native/color-mix.test.tsx b/src/__tests__/native/color-mix.test.tsx index c6dcb8a0..0c210a2b 100644 --- a/src/__tests__/native/color-mix.test.tsx +++ b/src/__tests__/native/color-mix.test.tsx @@ -1,6 +1,8 @@ import { render, screen } from "@testing-library/react-native"; +import { compile } from "react-native-css/compiler"; import { View } from "react-native-css/components/View"; import { registerCSS, testID } from "react-native-css/jest"; +import { StyleCollection } from "react-native-css/native"; test("color-mix() - keyword", () => { registerCSS( @@ -115,3 +117,22 @@ test("mixing an already translucent color preserves its alpha and restores chang expect(Number(color.slice(5, -1).split(",")[3])).toBeCloseTo(expected, 6); } }); + +test.each([ + ["50%, transparent", "rgba(255, 255, 255, 0.5)"], + [", transparent 75%", "rgba(255, 255, 255, 0.25)"], + [", transparent", "rgba(255, 255, 255, 0.5)"], +])( + "serialized native injection preserves omitted color weights: %s", + (tail, expected) => { + const compiled = compile( + `:root { --white: #fff; } .test { color: color-mix(in oklab, var(--white) ${tail}); }`, + { inlineVariables: false }, + ).stylesheet(); + // Metro's getNativeInjectionCode serializes the stylesheet as JSON. Omitted + // percentage array slots become null, unlike registerCSS's direct injection. + StyleCollection.inject(JSON.parse(JSON.stringify(compiled))); + render(); + expect(screen.getByTestId(testID).props.style?.color).toBe(expected); + }, +); diff --git a/src/native/styles/functions/color-mix.ts b/src/native/styles/functions/color-mix.ts index c134e497..31621ae0 100644 --- a/src/native/styles/functions/color-mix.ts +++ b/src/native/styles/functions/color-mix.ts @@ -28,6 +28,11 @@ export const colorMix: StyleFunctionResolver = (resolveValue, value) => { const left = parse(leftValue); const percentage = () => { const next = args[0]; + // Metro serializes omitted percentage slots in arrays as null. + if (next === null) { + args.shift(); + return undefined; + } if (typeof next !== "string" || !next.endsWith("%")) return undefined; args.shift(); return Number(next.slice(0, -1)) / 100; From abf66bbdca6c970be7c2ec93ad2d8e4478a08ef9 Mon Sep 17 00:00:00 2001 From: "Nate I." Date: Wed, 28 Oct 2026 09:14:47 +0200 Subject: [PATCH 3/4] test: retain serialized stylesheet type in color mix regression --- src/__tests__/native/color-mix.test.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/__tests__/native/color-mix.test.tsx b/src/__tests__/native/color-mix.test.tsx index 0c210a2b..d600e8fe 100644 --- a/src/__tests__/native/color-mix.test.tsx +++ b/src/__tests__/native/color-mix.test.tsx @@ -131,7 +131,9 @@ test.each([ ).stylesheet(); // Metro's getNativeInjectionCode serializes the stylesheet as JSON. Omitted // percentage array slots become null, unlike registerCSS's direct injection. - StyleCollection.inject(JSON.parse(JSON.stringify(compiled))); + StyleCollection.inject( + JSON.parse(JSON.stringify(compiled)) as typeof compiled, + ); render(); expect(screen.getByTestId(testID).props.style?.color).toBe(expected); }, From 561abb09a2da8174498830853983355f58a270b2 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Thu, 19 Nov 2026 18:15:32 -0500 Subject: [PATCH 4/4] refactor: reduce color-mix fix to the compiler filter and e2e test Keep only the compiler-side fix, written as a filter on the existing argument list, and the serialized-injection regression test. Drop the runtime null branch: with the compiler no longer emitting holes it was unreachable for fresh builds, and it let the regression test pass whether or not the compiler fix was present. Drop the compiler test that asserted on the internal stylesheet layout; the end-to-end test covers the same cases through Metro's JSON path. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01D4jU1dT613ZBytY8eaFjgE --- src/__tests__/compiler/declarations.test.tsx | 42 -------------------- src/compiler/declarations.ts | 21 ++++++---- src/native/styles/functions/color-mix.ts | 5 --- 3 files changed, 13 insertions(+), 55 deletions(-) diff --git a/src/__tests__/compiler/declarations.test.tsx b/src/__tests__/compiler/declarations.test.tsx index ffdead23..4dbd378f 100644 --- a/src/__tests__/compiler/declarations.test.tsx +++ b/src/__tests__/compiler/declarations.test.tsx @@ -1,4 +1,3 @@ -import { compile } from "react-native-css/compiler"; import { compileWithAutoDebug } from "react-native-css/jest"; // prettier-ignore @@ -30,44 +29,3 @@ test.each(tests)("declarations for %s", (declarations, expected) => { expect(myClassDeclarations).toStrictEqual(expected); }); - -test.each([ - [ - "left weight only", - "color-mix(in oklab, var(--left) 90%, transparent)", - ["oklab", [{}, "var", "left", 1], "90%", "transparent"], - ], - [ - "right weight only", - "color-mix(in srgb, var(--left), var(--right) 25%)", - ["srgb", [{}, "var", "left", 1], [{}, "var", "right", 1], "25%"], - ], - [ - "neither weight", - "color-mix(in srgb, var(--left), var(--right))", - ["srgb", [{}, "var", "left", 1], [{}, "var", "right", 1]], - ], -] as const)( - "color-mix serializes without absent weights: %s", - (_, value, expected) => { - const stylesheet = JSON.parse( - JSON.stringify( - compile(`.test { background-color: ${value}; }`, { - inlineVariables: false, - }).stylesheet(), - ), - ) as ReturnType["stylesheet"]>; - const declaration = stylesheet.s?.[0]?.[1]?.[0]?.d?.find( - (entry) => - Array.isArray(entry) && - Array.isArray(entry[0]) && - entry[0][1] === "colorMix", - ); - - expect( - Array.isArray(declaration) && Array.isArray(declaration[0]) - ? declaration[0][2] - : undefined, - ).toStrictEqual(expected); - }, -); diff --git a/src/compiler/declarations.ts b/src/compiler/declarations.ts index 1dbeeff9..197a9c69 100644 --- a/src/compiler/declarations.ts +++ b/src/compiler/declarations.ts @@ -2798,14 +2798,19 @@ export function parseColorMix( return; } - // Optional weights must not leave undefined array entries: Metro serializes - // them as null, which the native colorMix resolver cannot consume. - const args: StyleDescriptor[] = [colorSpaceArg, leftColorArg]; - if (leftColorPercentage !== undefined) args.push(leftColorPercentage); - args.push(rightColorArg); - if (rightColorPercentage !== undefined) args.push(rightColorPercentage); - - return [{}, "colorMix", args]; + return [ + {}, + "colorMix", + // Omitted weights must not leave holes: Metro serializes them as null, + // which the native resolver rejects. + [ + colorSpaceArg, + leftColorArg, + leftColorPercentage, + rightColorArg, + rightColorPercentage, + ].filter((arg) => arg !== undefined), + ]; } export function parseCalcArguments( diff --git a/src/native/styles/functions/color-mix.ts b/src/native/styles/functions/color-mix.ts index 31621ae0..c134e497 100644 --- a/src/native/styles/functions/color-mix.ts +++ b/src/native/styles/functions/color-mix.ts @@ -28,11 +28,6 @@ export const colorMix: StyleFunctionResolver = (resolveValue, value) => { const left = parse(leftValue); const percentage = () => { const next = args[0]; - // Metro serializes omitted percentage slots in arrays as null. - if (next === null) { - args.shift(); - return undefined; - } if (typeof next !== "string" || !next.endsWith("%")) return undefined; args.shift(); return Number(next.slice(0, -1)) / 100;