Skip to content
Closed
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
23 changes: 23 additions & 0 deletions src/__tests__/native/color-mix.test.tsx
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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(<View testID={testID} className="test" />);
expect(screen.getByTestId(testID).props.style?.color).toBe(expected);
},
);
5 changes: 5 additions & 0 deletions src/native/styles/functions/color-mix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down