Skip to content

Commit c664b5b

Browse files
authored
refactor: migrate transform panels to new style engine (#4118)
Ref #1536 Switched translate, scale, rotate and skew panels to new style engine. Simplified style updates. <img width="286" alt="image" src="https://github.com/user-attachments/assets/01e6b7cf-d3d9-430b-a1bd-efcd0db2b125"> <img width="257" alt="image" src="https://github.com/user-attachments/assets/ede684ad-a029-4a03-9c1b-05878d4ba1f8"> <img width="264" alt="image" src="https://github.com/user-attachments/assets/6270a781-b311-4759-a379-d9fe3627efd4"> <img width="253" alt="image" src="https://github.com/user-attachments/assets/040d5463-38e0-4bf0-bf9b-c823a284a48f">
1 parent f653a11 commit c664b5b

File tree

9 files changed

+264
-572
lines changed

9 files changed

+264
-572
lines changed

apps/builder/app/builder/features/style-panel/sections/transforms/transform-rotate.tsx

Lines changed: 41 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,42 @@
11
import { Flex, Grid } from "@webstudio-is/design-system";
2-
import {
3-
updateRotateOrSkewPropertyValue,
4-
type TransformPanelProps,
5-
} from "./transform-utils";
62
import {
73
XAxisRotateIcon,
84
YAxisRotateIcon,
95
ZAxisRotateIcon,
106
} from "@webstudio-is/icons";
7+
import type { StyleValue } from "@webstudio-is/css-engine";
8+
import { propertySyntaxes } from "@webstudio-is/css-data";
119
import { CssValueInputContainer } from "../../shared/css-value-input";
12-
import {
13-
toValue,
14-
UnitValue,
15-
type FunctionValue,
16-
type StyleValue,
17-
} from "@webstudio-is/css-engine";
18-
import type { StyleUpdateOptions } from "../../shared/use-style-data";
19-
import { parseCssValue, propertySyntaxes } from "@webstudio-is/css-data";
20-
import { extractRotatePropertiesFromTransform } from "./transform-extractors";
2110
import { PropertyInlineLabel } from "../../property-label";
11+
import { useComputedStyleDecl } from "../../shared/model";
12+
import { updateTransformFunction } from "./transform-utils";
2213

23-
export const RotatePanelContent = (props: TransformPanelProps) => {
24-
const { propertyValue, setProperty, currentStyle } = props;
25-
const { rotateX, rotateY, rotateZ } =
26-
extractRotatePropertiesFromTransform(propertyValue);
27-
28-
const handlePropertyUpdate = (
29-
index: number,
30-
prop: string,
31-
value: StyleValue,
32-
options?: StyleUpdateOptions
33-
) => {
34-
let newValue: UnitValue = { type: "unit", value: 0, unit: "deg" };
35-
36-
if (value.type === "unit") {
37-
newValue = value;
14+
export const RotatePanelContent = () => {
15+
const styleDecl = useComputedStyleDecl("transform");
16+
const tuple =
17+
styleDecl.cascadedValue.type === "tuple"
18+
? styleDecl.cascadedValue
19+
: undefined;
20+
let rotateX: StyleValue = { type: "unit", value: 0, unit: "deg" };
21+
let rotateY: StyleValue = { type: "unit", value: 0, unit: "deg" };
22+
let rotateZ: StyleValue = { type: "unit", value: 0, unit: "deg" };
23+
for (const item of tuple?.value ?? []) {
24+
if (
25+
item.type === "function" &&
26+
item.args.type === "layers" &&
27+
item.args.value[0].type === "unit"
28+
) {
29+
if (item.name === "rotateX") {
30+
rotateX = item.args.value[0];
31+
}
32+
if (item.name === "rotateY") {
33+
rotateY = item.args.value[0];
34+
}
35+
if (item.name === "rotateZ") {
36+
rotateZ = item.args.value[0];
37+
}
3838
}
39-
40-
if (value.type === "tuple" && value.value[0].type === "unit") {
41-
newValue = value.value[0];
42-
}
43-
44-
const newFunctionValue: FunctionValue = {
45-
type: "function",
46-
name: prop,
47-
args: { type: "layers", value: [newValue] },
48-
};
49-
50-
const newPropertyValue = updateRotateOrSkewPropertyValue({
51-
panel: "rotate",
52-
index,
53-
currentStyle,
54-
value: newFunctionValue,
55-
propertyValue,
56-
});
57-
58-
const rotate = parseCssValue("transform", toValue(newPropertyValue));
59-
if (rotate.type === "invalid") {
60-
return;
61-
}
62-
63-
setProperty("transform")(rotate, options);
64-
};
39+
}
6540

6641
return (
6742
<Flex direction="column" gap={2}>
@@ -75,18 +50,13 @@ export const RotatePanelContent = (props: TransformPanelProps) => {
7550
description={propertySyntaxes.rotateX}
7651
/>
7752
<CssValueInputContainer
78-
key="rotateX"
7953
styleSource="local"
8054
property="rotate"
81-
value={
82-
rotateX?.type === "function" && rotateX.args.type === "layers"
83-
? rotateX.args.value[0]
84-
: { type: "unit", value: 0, unit: "deg" }
85-
}
55+
value={rotateX}
8656
keywords={[]}
87-
setValue={(value, options) => {
88-
handlePropertyUpdate(0, "rotateX", value, options);
89-
}}
57+
setValue={(value, options) =>
58+
updateTransformFunction(styleDecl, "rotateX", value, options)
59+
}
9060
deleteProperty={() => {}}
9161
/>
9262
</Grid>
@@ -100,18 +70,13 @@ export const RotatePanelContent = (props: TransformPanelProps) => {
10070
description={propertySyntaxes.rotateY}
10171
/>
10272
<CssValueInputContainer
103-
key="rotateY"
10473
styleSource="local"
10574
property="rotate"
106-
value={
107-
rotateY?.type === "function" && rotateY.args.type === "layers"
108-
? rotateY.args.value[0]
109-
: { type: "unit", value: 0, unit: "deg" }
110-
}
75+
value={rotateY}
11176
keywords={[]}
112-
setValue={(value, options) => {
113-
handlePropertyUpdate(1, "rotateY", value, options);
114-
}}
77+
setValue={(value, options) =>
78+
updateTransformFunction(styleDecl, "rotateY", value, options)
79+
}
11580
deleteProperty={() => {}}
11681
/>
11782
</Grid>
@@ -125,18 +90,13 @@ export const RotatePanelContent = (props: TransformPanelProps) => {
12590
description={propertySyntaxes.rotateZ}
12691
/>
12792
<CssValueInputContainer
128-
key="rotateZ"
12993
styleSource="local"
13094
property="rotate"
131-
value={
132-
rotateZ?.type === "function" && rotateZ.args.type === "layers"
133-
? rotateZ.args.value[0]
134-
: { type: "unit", value: 0, unit: "deg" }
135-
}
95+
value={rotateZ}
13696
keywords={[]}
137-
setValue={(value, options) => {
138-
handlePropertyUpdate(2, "rotateZ", value, options);
139-
}}
97+
setValue={(value, options) =>
98+
updateTransformFunction(styleDecl, "rotateZ", value, options)
99+
}
140100
deleteProperty={() => {}}
141101
/>
142102
</Grid>

0 commit comments

Comments
 (0)