错误: 颜色透明度修饰符(`/80`、`/50` 等)因 `colorMix` 中的 `null` 而发生断点并返回 `undefined`
- 编译器(
parseColorMix在declarations.ts中): Tailwind CSS v4生成color-mix只有一个百分比:
color: color-mix(in oklab, var(--color-text-inverse) 80%, transparent);在React-native-css中,parseColorMix构造了具有5个位置参数的数组: return [ {}, "colorMix", [ colorSpaceArg, leftColorArg, leftColorPercentage, rightColorArg, rightColorPercentage, // is undefined/null ], ]; 由于rightColorPercentage被省略,undefined(在JSON序列化后变为null)被添加到数组中:['oklab', leftColor, '80%', 'transparent', null]。 运行时(color-mix.ts中的colorMix): const percentage = () => { const next = args[0]; if (typeof next !== "string" || !next.endsWith("%")) return undefined; args.shift(); return Number(next.slice(0, -1)) / 100; }; let leftWeight = percentage(); // 移除'80%' const rightValue = args.shift(); // 移除'transparent' let rightWeight = percentage(); // args[0]是null -> 返回undefined,而不进行移除! if (args.length) return; // <-- 在这里中止!args仍然包含[null] 由于args仍然包含[null],if (args.length) return;触发并返回undefined,导致属性完全没有颜色。 建议的修复
- 在src/compiler/declarations.ts中: 不要将undefined/null百分比推入到参数数组中:
- return [{}, "colorMix", [colorSpaceArg, leftColorArg, leftColorPercentage, rightColorArg, rightColorPercentage]];
- const colorMixArgs = [colorSpaceArg, leftColorArg];
- if (leftColorPercentage !== undefined && leftColorPercentage !== null) {
- colorMixArgs.push(leftColorPercentage);
- }
- colorMixArgs.push(rightColorArg);
- if (rightColorPercentage !== undefined && rightColorPercentage !== null) {
- colorMixArgs.push(rightColorPercentage);
- }
- return [{}, "colorMix", colorMixArgs];
- 在src/native/styles/functions/color-mix.ts中: 在运行时解析器中防御性地过滤nullish值:
- const args: unknown[] = [...(resolved as unknown[])];
- const args: unknown[] = (resolved as unknown[]).filter((arg) => arg !== null && arg !== undefined);
内容来源: nativewind/nativewind