diff --git a/src/__tests__/native/color-mix.test.tsx b/src/__tests__/native/color-mix.test.tsx index c6dcb8a0..d600e8fe 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,24 @@ 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)) as typeof 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;