diff --git a/src/__tests__/compiler/compiler.test.tsx b/src/__tests__/compiler/compiler.test.tsx index 4e1cff16..1f3b205a 100644 --- a/src/__tests__/compiler/compiler.test.tsx +++ b/src/__tests__/compiler/compiler.test.tsx @@ -498,6 +498,73 @@ test("a transform value the compiler cannot convert is ignored, with a warning", }); }); +test("a scale along z or a rotate about an oblique axis warns", () => { + const compiled = compile(` +.my-class { + scale: 2 3 400%; + rotate: 1 1 0 45deg; +} +.unwarned { + scale: 2 3 100%; + translate: 10px 20px 30px; + rotate: 0 0 0 45deg; +}`); + + expect(compiled.stylesheet()).toStrictEqual({ + s: [ + [ + "my-class", + [ + { + d: [ + [ + [ + [{}, "scaleX", 2], + [{}, "scaleY", 3], + ], + "scale", + ], + ], + s: [1, 1], + }, + ], + ], + [ + "unwarned", + [ + { + d: [ + [ + [ + [{}, "scaleX", 2], + [{}, "scaleY", 3], + ], + "scale", + ], + [ + [ + [{}, "translateX", 10], + [{}, "translateY", 20], + ], + "translate", + ], + [[{}, "rotateZ", "0deg"], "rotate"], + ], + s: [2, 1], + }, + ], + ], + ], + }); + + expect(compiled.warnings()).toStrictEqual({ + values: { + scale: ["400%"], + rotate: ["1 1 0 45deg"], + }, + }); +}); + test("simplifies rem", () => { const compiled = compile(`.test { border-width: calc(10rem + 2px); diff --git a/src/__tests__/native/transform.test.tsx b/src/__tests__/native/transform.test.tsx index 6cfeeda7..7e269eef 100644 --- a/src/__tests__/native/transform.test.tsx +++ b/src/__tests__/native/transform.test.tsx @@ -410,12 +410,24 @@ test.each([ }, ); -// Chromium 153, Firefox 155 and WebKit 26.6 rotate about the axis a keyword or a vector along one axis names +// Chromium 153 and Firefox 155 rotate about the axis a vector names, reversing the angle about a negative axis, and rotate by none about the zero vector; WebKit 26.6 ignores the direction of a z axis test.each([ + ["rotate: 0 0 -1 45deg;", [{ rotateZ: "-45deg" }]], + ["--r: 0 0 -1 45deg; rotate: var(--r);", [{ rotateZ: "-45deg" }]], + ["rotate: -1 0 0 45deg;", [{ rotateX: "-45deg" }]], + ["--r: -1 0 0 45deg; rotate: var(--r);", [{ rotateX: "-45deg" }]], ["rotate: 0 0 2 45deg;", [{ rotateZ: "45deg" }]], + ["--r: 0 0 2 45deg; rotate: var(--r);", [{ rotateZ: "45deg" }]], ["rotate: x 45deg;", [{ rotateX: "45deg" }]], + ["--r: x 45deg; rotate: var(--r);", [{ rotateX: "45deg" }]], ["rotate: 45deg y;", [{ rotateY: "45deg" }]], - ["--r: 20deg; rotate: var(--r);", [{ rotate: "20deg" }]], + ["--r: 45deg y; rotate: var(--r);", [{ rotateY: "45deg" }]], + ["--r: 20deg; rotate: var(--r);", [{ rotateZ: "20deg" }]], + ["--r: x; rotate: var(--r) 45deg;", [{ rotateX: "45deg" }]], + ["--r: 0 0 -1; rotate: var(--r) 45deg;", [{ rotateZ: "-45deg" }]], + ["rotate: 0 0 0 45deg;", [{ rotateZ: "0deg" }]], + ["--r: 0 0 0 45deg; rotate: var(--r);", [{ rotateZ: "0deg" }]], + ["--r: none; rotate: var(--r);", [{ rotateZ: "0deg" }]], ] as const)("%s rotates about the axis it names", (declarations, transform) => { expect( renderStyle( @@ -435,9 +447,12 @@ test.each([ "--t: 20px 30px; translate: 10px var(--t);", [{ translateX: 10 }, { translateY: 20 }], ], + ["--t: none; translate: var(--t);", [{ translateX: 0 }, { translateY: 0 }]], + ["--t: NONE; translate: var(--t);", [{ translateX: 0 }, { translateY: 0 }]], ["scale: 2 3 4;", [{ scaleX: 2 }, { scaleY: 3 }]], ["--s: 2 3 4; scale: var(--s);", [{ scaleX: 2 }, { scaleY: 3 }]], ["--s: 3 4; scale: 2 var(--s);", [{ scaleX: 2 }, { scaleY: 3 }]], + ["--s: none; scale: var(--s);", [{ scaleX: 1 }, { scaleY: 1 }]], ] as const)("%s renders its x and y", (declarations, transform) => { expect( renderStyle( @@ -483,11 +498,23 @@ test.each([ }); }); -// React Native's rotate keys turn only about an axis, so a substituted rotate about a vector off every axis is none -test("--r: 1 1 0 45deg; rotate: var(--r); rotates by none", () => { +// React Native's rotate keys turn only about an axis, so a rotate about a vector off every axis is ignored when written and none when substituted +test.each([ + ["rotate: 1 1 0 45deg;", [{ rotateZ: "10deg" }]], + ["--r: 1 1 0 45deg; rotate: var(--r);", []], +] as const)("%s rotates by none", (declarations, transform) => { + expect( + renderStyle( + `${decoy} .earlier { rotate: 10deg; } .my-class { ${declarations} }`, + ), + ).toStrictEqual(transform.length === 0 ? {} : { transform }); +}); + +// Chromium 153, Firefox 155 and WebKit 26.6: a later transform without a value replaces an earlier one, and is none (css-variables-1 §3.1) +test("transform: var(--missing) replaces an earlier rule's transform", () => { expect( renderStyle( - `${decoy} .earlier { rotate: 10deg; } .my-class { --r: 1 1 0 45deg; rotate: var(--r); }`, + `${decoy} .earlier { transform: rotate(10deg); } .my-class { transform: var(--missing); }`, ), - ).toStrictEqual({}); + ).toStrictEqual({ transform: [] }); }); diff --git a/src/__tests__/native/variable-fold-parity.test.tsx b/src/__tests__/native/variable-fold-parity.test.tsx index 1bdd3863..85afd12d 100644 --- a/src/__tests__/native/variable-fold-parity.test.tsx +++ b/src/__tests__/native/variable-fold-parity.test.tsx @@ -123,17 +123,7 @@ const VALUE_CENSUS: Record = { { property: "margin-top", value: "-4px" }, { property: "width", value: "1rem" }, ], - "angle": [ - { - property: "rotate", - value: "45deg", - knownDivergence: { - kept: { transform: [{ rotate: "45deg" }] }, - openDefect: - "the runtime keeps `rotate`, where the compiler writes rotateZ; #389 writes rotateZ on both", - }, - }, - ], + "angle": [{ property: "rotate", value: "45deg" }], "function": [ { property: "width", value: "calc(10px + 2px)" }, { property: "transform", value: "translateX(10px)" }, diff --git a/src/compiler/declarations.ts b/src/compiler/declarations.ts index 771fde0f..82e4da7b 100644 --- a/src/compiler/declarations.ts +++ b/src/compiler/declarations.ts @@ -37,7 +37,7 @@ import type { UnresolvedColor, } from "lightningcss"; -import { isStyleFunction } from "../utilities"; +import { isStyleFunction, negateAngle, rotationAxis } from "../utilities"; import { isMappingProperty } from "./atRules"; import type { StyleDescriptor, @@ -120,8 +120,9 @@ const unparsedRuntimeParsing = new Set([ "translate", ]); -// css-transforms-2 §3: the translate and scale properties take an optional z, which the functions of the same name do not +// css-transforms-2 §3: the individual transform properties each have a grammar of their own, which the transform function of the same name does not share const INDIVIDUAL_TRANSFORM_RESOLVERS: ReadonlyMap = new Map([ + ["rotate", "individualRotate"], ["scale", "individualScale"], ["translate", "individualTranslate"], ]); @@ -868,35 +869,50 @@ function parseRotate( { value }: DeclarationType<"rotate">, builder: StylesheetBuilder, ) { - const angle = parseAngle(value.angle, builder); - const rotations: TransformFunction[] = []; + const rotation = rotationAxis(value.x, value.y, value.z); - if (value.x) { - rotations.push([{}, "rotateX", angle]); - } - - if (value.y) { - rotations.push([{}, "rotateY", angle]); - } - - if (value.z) { - rotations.push([{}, "rotateZ", angle]); - } - - if (hasUnconvertibleArgument(rotations)) { - return; + switch (rotation.type) { + case "none": + builder.addDescriptor("rotate", [{}, "rotateZ", "0deg"]); + return; + case "axis": { + const angle = parseAngle(value.angle, builder); + + if (angle !== undefined) { + builder.addDescriptor("rotate", [ + {}, + rotation.rotation, + rotation.reversed ? negateAngle(angle) : angle, + ]); + } + return; + } + // React Native's rotate keys turn only about an axis, so a vector off every axis has none to take it + case "oblique": + builder.addWarning( + "value", + `${value.x} ${value.y} ${value.z} ${value.angle.value}${value.angle.type}`, + ); + return; + default: + rotation satisfies never; } - - builder.addDescriptor( - "rotate", - rotations.length === 1 ? rotations[0] : rotations, - ); } function parseScale( { value }: DeclarationType<"scale">, builder: StylesheetBuilder, ) { + // React Native has no z axis to scale along, and lightningcss holds a percentage as its fraction + if (value !== "none" && value.z.value !== 1) { + builder.addWarning( + "value", + value.z.type === "percentage" + ? `${round(value.z.value * 100)}%` + : `${round(value.z.value)}`, + ); + } + builder.addDescriptor("scale", [ [{}, "scaleX", parseScaleValue(value, "x", builder)], [{}, "scaleY", parseScaleValue(value, "y", builder)], diff --git a/src/native/styles/functions/transform-functions.ts b/src/native/styles/functions/transform-functions.ts index 8c04a8a1..be03882a 100644 --- a/src/native/styles/functions/transform-functions.ts +++ b/src/native/styles/functions/transform-functions.ts @@ -2,6 +2,8 @@ import type { StyleDescriptor } from "react-native-css/compiler"; import { commaListItems, isStyleDescriptorArray, + negateAngle, + rotationAxis, } from "react-native-css/utilities"; import type { SimpleResolveValue, StyleFunctionResolver } from "../resolve"; @@ -35,6 +37,11 @@ export const individualTranslate: StyleFunctionResolver = ( descriptor, ) => { const operands = resolveOperands(resolveValue, descriptor[2]); + + if (operands.length === 1 && isNoneKeyword(operands[0])) { + return [{ translateX: 0 }, { translateY: 0 }]; + } + const z = operands.length > 2 ? operands[2] : 0; return operands.length <= 3 && typeof z === "number" @@ -48,6 +55,11 @@ export const individualScale: StyleFunctionResolver = ( descriptor, ) => { const operands = resolveOperands(resolveValue, descriptor[2]); + + if (operands.length === 1 && isNoneKeyword(operands[0])) { + return [{ scaleX: 1 }, { scaleY: 1 }]; + } + const z = operands.length > 2 ? operands[2] : 1; return operands.length <= 3 && scaleFactor(z) !== null @@ -55,6 +67,89 @@ export const individualScale: StyleFunctionResolver = ( : null; }; +// css-transforms-2 §3: `rotate: none | | [x | y | z | {3}] && ` +export const individualRotate: StyleFunctionResolver = ( + resolveValue, + descriptor, +) => { + const operands = resolveOperands(resolveValue, descriptor[2]); + + if (operands.length === 1) { + const angle = operands[0]; + + if (isNoneKeyword(angle)) { + return { rotateZ: "0deg" }; + } + + return isTransformComponent(angle) ? { rotateZ: angle } : null; + } + + const axisIndex = operands.findIndex(isAxisKeyword); + + if (axisIndex !== -1) { + const axis = operands[axisIndex]; + const angle = operands[axisIndex === 0 ? 1 : 0]; + + return operands.length === 2 && + isAxisKeyword(axis) && + isTransformComponent(angle) + ? { [AXIS_ROTATIONS[axis]]: angle } + : null; + } + + const angleIndex = operands.length === 4 ? angleOperandIndex(operands) : -1; + const angle = operands[angleIndex]; + const [x, y, z] = operands.filter((_, index) => index !== angleIndex); + + if ( + angleIndex === -1 || + !isTransformComponent(angle) || + typeof x !== "number" || + typeof y !== "number" || + typeof z !== "number" + ) { + return null; + } + + const rotation = rotationAxis(x, y, z); + + switch (rotation.type) { + case "none": + return { rotateZ: "0deg" }; + case "axis": + return { + [rotation.rotation]: rotation.reversed ? negateAngle(angle) : angle, + }; + case "oblique": + return null; + default: + rotation satisfies never; + return null; + } +}; + +const AXIS_ROTATIONS = { + x: "rotateX", + y: "rotateY", + z: "rotateZ", +} as const; + +function isAxisKeyword(value: unknown): value is keyof typeof AXIS_ROTATIONS { + return value === "x" || value === "y" || value === "z"; +} + +// The angle of a vector rotation comes first or last, around three numbers +function angleOperandIndex(operands: readonly unknown[]): number { + const index = operands.findIndex((operand) => typeof operand === "string"); + + return index === 0 || index === operands.length - 1 ? index : -1; +} + +// `none` is the identity transform, and React Native refuses the keyword itself +function isNoneKeyword(value: unknown): boolean { + return typeof value === "string" && value.toLowerCase() === "none"; +} + // The first `count` operands, so a caller can leave out a z without copying the list function scalePair(operands: readonly unknown[], count: number): unknown { const x = scaleFactor(operands[0]); diff --git a/src/utilities/index.ts b/src/utilities/index.ts index a421c7b6..d9d7ee44 100644 --- a/src/utilities/index.ts +++ b/src/utilities/index.ts @@ -2,3 +2,4 @@ export * from "./equality"; export * from "./specificity"; export * from "./style-descriptor"; export * from "./dot-notation.types"; +export * from "./rotation-axis"; diff --git a/src/utilities/rotation-axis.ts b/src/utilities/rotation-axis.ts new file mode 100644 index 00000000..9a4fa30a --- /dev/null +++ b/src/utilities/rotation-axis.ts @@ -0,0 +1,37 @@ +export type RotationAxis = + | { readonly type: "none" } + | { + readonly type: "axis"; + readonly rotation: "rotateX" | "rotateY" | "rotateZ"; + readonly reversed: boolean; + } + | { readonly type: "oblique" }; + +// css-transforms-2 §3: a rotation turns about the direction its vector names, and the zero vector names none +export function rotationAxis(x: number, y: number, z: number): RotationAxis { + if (x === 0 && y === 0 && z === 0) { + return { type: "none" }; + } + + if (y === 0 && z === 0) { + return { type: "axis", rotation: "rotateX", reversed: x < 0 }; + } + + if (x === 0 && z === 0) { + return { type: "axis", rotation: "rotateY", reversed: y < 0 }; + } + + if (x === 0 && y === 0) { + return { type: "axis", rotation: "rotateZ", reversed: z < 0 }; + } + + return { type: "oblique" }; +} + +export function negateAngle(angle: string | number): string | number { + if (typeof angle === "number") { + return -angle; + } + + return angle.startsWith("-") ? angle.slice(1) : `-${angle}`; +}