Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

refactor: migrate transform panels to new style engine (#4118)

Bogdan Chadkinc664b5b7ce06
diff --git a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-rotate.tsx b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-rotate.tsx
index 9f8586fa3e..557bc26810 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-rotate.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-rotate.tsx
@@ -1,67 +1,42 @@
import { Flex, Grid } from "@webstudio-is/design-system";
-import {
- updateRotateOrSkewPropertyValue,
- type TransformPanelProps,
-} from "./transform-utils";
import {
XAxisRotateIcon,
YAxisRotateIcon,
ZAxisRotateIcon,
} from "@webstudio-is/icons";
+import type { StyleValue } from "@webstudio-is/css-engine";
+import { propertySyntaxes } from "@webstudio-is/css-data";
import { CssValueInputContainer } from "../../shared/css-value-input";
-import {
- toValue,
- UnitValue,
- type FunctionValue,
- type StyleValue,
-} from "@webstudio-is/css-engine";
-import type { StyleUpdateOptions } from "../../shared/use-style-data";
-import { parseCssValue, propertySyntaxes } from "@webstudio-is/css-data";
-import { extractRotatePropertiesFromTransform } from "./transform-extractors";
import { PropertyInlineLabel } from "../../property-label";
+import { useComputedStyleDecl } from "../../shared/model";
+import { updateTransformFunction } from "./transform-utils";
-export const RotatePanelContent = (props: TransformPanelProps) => {
- const { propertyValue, setProperty, currentStyle } = props;
- const { rotateX, rotateY, rotateZ } =
- extractRotatePropertiesFromTransform(propertyValue);
-
- const handlePropertyUpdate = (
- index: number,
- prop: string,
- value: StyleValue,
- options?: StyleUpdateOptions
- ) => {
- let newValue: UnitValue = { type: "unit", value: 0, unit: "deg" };
-
- if (value.type === "unit") {
- newValue = value;
+export const RotatePanelContent = () => {
+ const styleDecl = useComputedStyleDecl("transform");
+ const tuple =
+ styleDecl.cascadedValue.type === "tuple"
+ ? styleDecl.cascadedValue
+ : undefined;
+ let rotateX: StyleValue = { type: "unit", value: 0, unit: "deg" };
+ let rotateY: StyleValue = { type: "unit", value: 0, unit: "deg" };
+ let rotateZ: StyleValue = { type: "unit", value: 0, unit: "deg" };
+ for (const item of tuple?.value ?? []) {
+ if (
+ item.type === "function" &&
+ item.args.type === "layers" &&
+ item.args.value[0].type === "unit"
+ ) {
+ if (item.name === "rotateX") {
+ rotateX = item.args.value[0];
+ }
+ if (item.name === "rotateY") {
+ rotateY = item.args.value[0];
+ }
+ if (item.name === "rotateZ") {
+ rotateZ = item.args.value[0];
+ }
}
-
- if (value.type === "tuple" && value.value[0].type === "unit") {
- newValue = value.value[0];
- }
-
- const newFunctionValue: FunctionValue = {
- type: "function",
- name: prop,
- args: { type: "layers", value: [newValue] },
- };
-
- const newPropertyValue = updateRotateOrSkewPropertyValue({
- panel: "rotate",
- index,
- currentStyle,
- value: newFunctionValue,
- propertyValue,
- });
-
- const rotate = parseCssValue("transform", toValue(newPropertyValue));
- if (rotate.type === "invalid") {
- return;
- }
-
- setProperty("transform")(rotate, options);
- };
+ }
return (
<Flex direction="column" gap={2}>
@@ -75,18 +50,13 @@ export const RotatePanelContent = (props: TransformPanelProps) => {
description={propertySyntaxes.rotateX}
/>
<CssValueInputContainer
- key="rotateX"
styleSource="local"
property="rotate"
- value={
- rotateX?.type === "function" && rotateX.args.type === "layers"
- ? rotateX.args.value[0]
- : { type: "unit", value: 0, unit: "deg" }
- }
+ value={rotateX}
keywords={[]}
- setValue={(value, options) => {
- handlePropertyUpdate(0, "rotateX", value, options);
- }}
+ setValue={(value, options) =>
+ updateTransformFunction(styleDecl, "rotateX", value, options)
+ }
deleteProperty={() => {}}
/>
</Grid>
@@ -100,18 +70,13 @@ export const RotatePanelContent = (props: TransformPanelProps) => {
description={propertySyntaxes.rotateY}
/>
<CssValueInputContainer
- key="rotateY"
styleSource="local"
property="rotate"
- value={
- rotateY?.type === "function" && rotateY.args.type === "layers"
- ? rotateY.args.value[0]
- : { type: "unit", value: 0, unit: "deg" }
- }
+ value={rotateY}
keywords={[]}
- setValue={(value, options) => {
- handlePropertyUpdate(1, "rotateY", value, options);
- }}
+ setValue={(value, options) =>
+ updateTransformFunction(styleDecl, "rotateY", value, options)
+ }
deleteProperty={() => {}}
/>
</Grid>
@@ -125,18 +90,13 @@ export const RotatePanelContent = (props: TransformPanelProps) => {
description={propertySyntaxes.rotateZ}
/>
<CssValueInputContainer
- key="rotateZ"
styleSource="local"
property="rotate"
- value={
- rotateZ?.type === "function" && rotateZ.args.type === "layers"
- ? rotateZ.args.value[0]
- : { type: "unit", value: 0, unit: "deg" }
- }
+ value={rotateZ}
keywords={[]}
- setValue={(value, options) => {
- handlePropertyUpdate(2, "rotateZ", value, options);
- }}
+ setValue={(value, options) =>
+ updateTransformFunction(styleDecl, "rotateZ", value, options)
+ }
deleteProperty={() => {}}
/>
</Grid>
diff --git a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-scale.tsx b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-scale.tsx
index 25704054b2..cdc2bb0df8 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-scale.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-scale.tsx
@@ -1,3 +1,4 @@
+import { useState } from "react";
import {
Box,
EnhancedTooltip,
@@ -6,15 +7,7 @@ import {
SmallToggleButton,
theme,
} from "@webstudio-is/design-system";
-import {
- StyleValue,
- toValue,
- UnitValue,
- type StyleProperty,
- type Unit,
-} from "@webstudio-is/css-engine";
-import { CssValueInput } from "../../shared/css-value-input";
-import { type TransformPanelProps } from "./transform-utils";
+import type { StyleValue, StyleProperty } from "@webstudio-is/css-engine";
import {
BorderRadiusIcon,
XAxisIcon,
@@ -23,150 +16,72 @@ import {
Link2Icon,
Link2UnlinkedIcon,
} from "@webstudio-is/icons";
-import { parseCssValue, propertySyntaxes } from "@webstudio-is/css-data";
-import { useCallback, useState } from "react";
-import type { StyleUpdateOptions } from "../../shared/use-style-data";
+import { propertySyntaxes } from "@webstudio-is/css-data";
+import { CssValueInput } from "../../shared/css-value-input";
+import {
+ deleteProperty,
+ setProperty,
+ type StyleUpdateOptions,
+} from "../../shared/use-style-data";
import type { IntermediateStyleValue } from "../../shared/css-value-input/css-value-input";
import { PropertyInlineLabel } from "../../property-label";
+import { useComputedStyleDecl } from "../../shared/model";
const property: StyleProperty = "scale";
-// When a scale shorthand proeprty is used, we can't set keywords.
-// i.e scale: none 50% 100% is invalid.
-// So we use a fake property which don't have any initial keywords.
-// scale proeprties initial value is none. So, the individual scaleX, scaleY, scaleZ proeprties
-// will show none in the input field if used scale directly.
-const fakeProperty: StyleProperty = "opacity";
-
-// We need to have a custom conversion function for unit values.
-// Unlike individual proeprties, here all three goes under a single scale proeprty
-// So, whne we try to save for x, the other two y and z should be updated as well.
-// And we need to make sure they are saved as unit values and not intermediate values.
-const convertToUnitValue = (
- value: StyleValue | IntermediateStyleValue
-): UnitValue | undefined => {
- if (value.type === "unit") {
- return value;
- }
-
- if (value.type === "intermediate") {
- let propValue;
- if (value.unit === "number") {
- propValue = value.value;
- } else if (value.unit !== undefined) {
- propValue = value.value.replace(value.unit, "");
- }
- return {
- type: "unit",
- value: Number(propValue),
- unit: value.unit as Unit,
- };
- }
-};
-
-export const ScalePanelContent = (props: TransformPanelProps) => {
- const { propertyValue, setProperty, deleteProperty } = props;
- const [scaleX, scaleY, scaleZ] = propertyValue.value;
+export const ScalePanelContent = () => {
+ const styleDecl = useComputedStyleDecl(property);
+ const tuple =
+ styleDecl.cascadedValue.type === "tuple"
+ ? styleDecl.cascadedValue
+ : undefined;
+ const [scaleX, scaleY, scaleZ] = tuple?.value ?? [];
const [isScalingLocked, setScalingLock] = useState(true);
const [intermediateScalingX, setIntermediateScalingX] = useState<
StyleValue | IntermediateStyleValue
- >(scaleX);
+ >();
const [intermediateScalingY, setIntermediateScalingY] = useState<
StyleValue | IntermediateStyleValue
- >(scaleY);
+ >();
const [intermediateScalingZ, setIntermediateScalingZ] = useState<
StyleValue | IntermediateStyleValue
- >(scaleZ);
+ >();
+
+ const setAxis = (
+ axis: number,
+ newValue: StyleValue,
+ options?: StyleUpdateOptions
+ ) => {
+ if (tuple === undefined) {
+ return;
+ }
+
+ if (newValue.type === "tuple") {
+ [newValue] = newValue.value;
+ }
+ if (newValue.type !== "unit") {
+ newValue = { type: "unit", value: 100, unit: "%" };
+ }
- const handleToggleScaling = () => {
+ const newTuple = structuredClone(tuple);
+ newTuple.value[axis] = newValue;
+ if (isScalingLocked) {
+ if (axis === 0 || axis === 1) {
+ newTuple.value[0] = newValue;
+ newTuple.value[1] = newValue;
+ }
+ }
+ setProperty(property)(newTuple, options);
+ };
+
+ const toggleScaling = () => {
const lockScaling = isScalingLocked === true ? false : true;
setScalingLock(lockScaling);
if (lockScaling === true) {
- setIntermediateScalingY(intermediateScalingX);
- updateScaleValues(
- {
- scaleX: intermediateScalingX,
- scaleY: intermediateScalingX,
- scaleZ: intermediateScalingZ,
- },
- { isEphemeral: false }
- );
+ setAxis(1, scaleX);
}
};
- const updateScaleValues = useCallback(
- (
- params: {
- scaleX: StyleValue | IntermediateStyleValue;
- scaleY: StyleValue | IntermediateStyleValue;
- scaleZ: StyleValue | IntermediateStyleValue;
- },
- options: StyleUpdateOptions
- ) => {
- const x = convertToUnitValue(params.scaleX);
- const y = convertToUnitValue(params.scaleY);
- const z = convertToUnitValue(params.scaleZ);
-
- const result = parseCssValue(
- "scale",
- `${toValue(x)} ${toValue(y)} ${toValue(z)}`
- );
-
- if (result.type === "invalid" || result.type !== "tuple") {
- return;
- }
-
- setProperty(property)(result, options);
- },
- [setProperty]
- );
-
- const handleOnChange = useCallback(
- (
- prop: "scaleX" | "scaleY" | "scaleZ",
- value: StyleValue | IntermediateStyleValue,
- options: StyleUpdateOptions
- ) => {
- let x = intermediateScalingX;
- let y = intermediateScalingY;
- let z = intermediateScalingZ;
-
- if (prop === "scaleX") {
- setIntermediateScalingX(value);
- x = value;
- if (isScalingLocked === true) {
- setIntermediateScalingY(value);
- y = value;
- }
- }
-
- if (prop === "scaleY") {
- setIntermediateScalingY(value);
- y = value;
- if (isScalingLocked === true) {
- setIntermediateScalingX(value);
- x = value;
- }
- }
-
- if (prop === "scaleZ") {
- setIntermediateScalingZ(value);
- z = value;
- }
-
- if (value.type !== "intermediate") {
- updateScaleValues({ scaleX: x, scaleY: y, scaleZ: z }, options);
- }
- },
- [
- intermediateScalingX,
- intermediateScalingY,
- intermediateScalingZ,
- isScalingLocked,
- updateScaleValues,
- ]
- );
-
return (
<Flex>
<Flex direction="column" gap={2}>
@@ -180,35 +95,33 @@ export const ScalePanelContent = (props: TransformPanelProps) => {
description={propertySyntaxes.scaleX}
/>
<CssValueInput
- key="scaleX"
styleSource="local"
- property={fakeProperty}
+ property={property}
keywords={[]}
value={scaleX}
intermediateValue={intermediateScalingX}
onChange={(value) => {
- if (value === undefined) {
- deleteProperty(property, { isEphemeral: true });
- return;
+ setIntermediateScalingX(value);
+ if (isScalingLocked) {
+ setIntermediateScalingY(value);
}
-
- handleOnChange("scaleX", value, { isEphemeral: true });
- }}
- onChangeComplete={(value) => {
if (value === undefined) {
deleteProperty(property, { isEphemeral: true });
- return;
+ } else if (value.type !== "intermediate") {
+ setAxis(0, value, { isEphemeral: true });
}
-
- handleOnChange("scaleX", value.value, { isEphemeral: false });
+ }}
+ onChangeComplete={({ value }) => {
+ setIntermediateScalingX(undefined);
+ setIntermediateScalingY(undefined);
+ setAxis(0, value);
}}
onHighlight={(value) => {
if (value === undefined) {
deleteProperty(property, { isEphemeral: true });
- return;
+ } else {
+ setAxis(0, value, { isEphemeral: true });
}
-
- handleOnChange("scaleX", value, { isEphemeral: true });
}}
onAbort={() => deleteProperty(property, { isEphemeral: true })}
/>
@@ -223,35 +136,33 @@ export const ScalePanelContent = (props: TransformPanelProps) => {
description={propertySyntaxes.scaleY}
/>
<CssValueInput
- key="scaleY"
styleSource="local"
- property={fakeProperty}
+ property={property}
keywords={[]}
value={scaleY}
intermediateValue={intermediateScalingY}
onChange={(value) => {
- if (value === undefined) {
- deleteProperty(property, { isEphemeral: true });
- return;
+ setIntermediateScalingY(value);
+ if (isScalingLocked) {
+ setIntermediateScalingX(value);
}
-
- handleOnChange("scaleY", value, { isEphemeral: true });
- }}
- onChangeComplete={(value) => {
if (value === undefined) {
deleteProperty(property, { isEphemeral: true });
- return;
+ } else if (value.type !== "intermediate") {
+ setAxis(1, value, { isEphemeral: true });
}
-
- handleOnChange("scaleY", value.value, { isEphemeral: false });
+ }}
+ onChangeComplete={({ value }) => {
+ setIntermediateScalingY(undefined);
+ setIntermediateScalingX(undefined);
+ setAxis(1, value);
}}
onHighlight={(value) => {
if (value === undefined) {
deleteProperty(property, { isEphemeral: true });
- return;
+ } else {
+ setAxis(1, value, { isEphemeral: true });
}
-
- handleOnChange("scaleY", value, { isEphemeral: true });
}}
onAbort={() => deleteProperty(property, { isEphemeral: true })}
/>
@@ -266,34 +177,29 @@ export const ScalePanelContent = (props: TransformPanelProps) => {
description={propertySyntaxes.scaleZ}
/>
<CssValueInput
- key="scaleZ"
styleSource="local"
- property={fakeProperty}
+ property={property}
keywords={[]}
value={scaleZ}
intermediateValue={intermediateScalingZ}
onChange={(value) => {
+ setIntermediateScalingZ(value);
if (value === undefined) {
deleteProperty(property, { isEphemeral: true });
- return;
+ } else if (value.type !== "intermediate") {
+ setAxis(2, value, { isEphemeral: true });
}
- handleOnChange("scaleZ", value, { isEphemeral: true });
}}
- onChangeComplete={(value) => {
- if (value === undefined) {
- deleteProperty(property, { isEphemeral: true });
- return;
- }
-
- handleOnChange("scaleZ", value.value, { isEphemeral: false });
+ onChangeComplete={({ value }) => {
+ setIntermediateScalingZ(undefined);
+ setAxis(2, value, { isEphemeral: false });
}}
onHighlight={(value) => {
if (value === undefined) {
deleteProperty(property, { isEphemeral: true });
- return;
+ } else {
+ setAxis(2, value, { isEphemeral: true });
}
-
- handleOnChange("scaleZ", value, { isEphemeral: true });
}}
onAbort={() => deleteProperty(property, { isEphemeral: true })}
/>
@@ -316,7 +222,7 @@ export const ScalePanelContent = (props: TransformPanelProps) => {
<SmallToggleButton
css={{ rotate: "90deg" }}
pressed={isScalingLocked}
- onPressedChange={handleToggleScaling}
+ onPressedChange={toggleScaling}
variant="normal"
icon={isScalingLocked ? <Link2Icon /> : <Link2UnlinkedIcon />}
/>
diff --git a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-skew.tsx b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-skew.tsx
index 270f9947e5..567193880e 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-skew.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-skew.tsx
@@ -1,67 +1,39 @@
import { Flex, Grid } from "@webstudio-is/design-system";
-import {
- updateRotateOrSkewPropertyValue,
- type TransformPanelProps,
-} from "./transform-utils";
-import { extractSkewPropertiesFromTransform } from "./transform-extractors";
import { XAxisIcon, YAxisIcon } from "@webstudio-is/icons";
+import type { StyleValue } from "@webstudio-is/css-engine";
+import { propertySyntaxes } from "@webstudio-is/css-data";
import { CssValueInputContainer } from "../../shared/css-value-input";
-import type { StyleUpdateOptions } from "../../shared/use-style-data";
-import {
- StyleValue,
- toValue,
- UnitValue,
- type FunctionValue,
-} from "@webstudio-is/css-engine";
-import { parseCssValue, propertySyntaxes } from "@webstudio-is/css-data";
import { PropertyInlineLabel } from "../../property-label";
+import { useComputedStyleDecl } from "../../shared/model";
+import { updateTransformFunction } from "./transform-utils";
// We use fakeProperty to pass for the CssValueInputContainer.
// https://developer.mozilla.org/en-US/docs/Web/CSS/rotate#formal_syntax
// angle
const fakeProperty = "rotate";
-export const SkewPanelContent = (props: TransformPanelProps) => {
- const { propertyValue, setProperty, currentStyle } = props;
- const { skewX, skewY } = extractSkewPropertiesFromTransform(propertyValue);
-
- const handlePropertyUpdate = (
- index: number,
- prop: string,
- value: StyleValue,
- options?: StyleUpdateOptions
- ) => {
- let newValue: UnitValue = { type: "unit", value: 0, unit: "deg" };
-
- if (value.type === "unit") {
- newValue = value;
- }
-
- if (value.type === "tuple" && value.value[0].type === "unit") {
- newValue = value.value[0];
+export const SkewPanelContent = () => {
+ const styleDecl = useComputedStyleDecl("transform");
+ const tuple =
+ styleDecl.cascadedValue.type === "tuple"
+ ? styleDecl.cascadedValue
+ : undefined;
+ let skewX: StyleValue = { type: "unit", value: 0, unit: "deg" };
+ let skewY: StyleValue = { type: "unit", value: 0, unit: "deg" };
+ for (const item of tuple?.value ?? []) {
+ if (
+ item.type === "function" &&
+ item.args.type === "layers" &&
+ item.args.value[0].type === "unit"
+ ) {
+ if (item.name === "skewx") {
+ skewX = item.args.value[0];
+ }
+ if (item.name === "skewY") {
+ skewY = item.args.value[0];
+ }
}
-
- const newFunctionValue: FunctionValue = {
- type: "function",
- name: prop,
- args: { type: "layers", value: [newValue] },
- };
-
- const newPropertyValue = updateRotateOrSkewPropertyValue({
- panel: "skew",
- index,
- currentStyle,
- value: newFunctionValue,
- propertyValue,
- });
-
- const skew = parseCssValue("transform", toValue(newPropertyValue));
- if (skew.type === "invalid") {
- return;
- }
-
- setProperty("transform")(skew, options);
- };
+ }
return (
<Flex direction="column" gap={2}>
@@ -75,18 +47,13 @@ export const SkewPanelContent = (props: TransformPanelProps) => {
description={propertySyntaxes.skewX}
/>
<CssValueInputContainer
- key="skewX"
styleSource="local"
property={fakeProperty}
- value={
- skewX?.type === "function" && skewX.args.type === "layers"
- ? skewX.args.value[0]
- : { type: "unit", value: 0, unit: "deg" }
- }
+ value={skewX}
keywords={[]}
- setValue={(value, options) => {
- handlePropertyUpdate(0, "skewX", value, options);
- }}
+ setValue={(value, options) =>
+ updateTransformFunction(styleDecl, "skewX", value, options)
+ }
deleteProperty={() => {}}
/>
</Grid>
@@ -100,18 +67,13 @@ export const SkewPanelContent = (props: TransformPanelProps) => {
description={propertySyntaxes.skewY}
/>
<CssValueInputContainer
- key="skewY"
styleSource="local"
property={fakeProperty}
- value={
- skewY?.type === "function" && skewY.args.type === "layers"
- ? skewY.args.value[0]
- : { type: "unit", value: 0, unit: "deg" }
- }
+ value={skewY}
keywords={[]}
- setValue={(value, options) => {
- handlePropertyUpdate(1, "skewY", value, options);
- }}
+ setValue={(value, options) =>
+ updateTransformFunction(styleDecl, "skewY", value, options)
+ }
deleteProperty={() => {}}
/>
</Grid>
diff --git a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-translate.tsx b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-translate.tsx
index 04425666e0..851be6b2cd 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-translate.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-translate.tsx
@@ -1,58 +1,46 @@
import { Flex, Grid } from "@webstudio-is/design-system";
-import {
- StyleValue,
- toValue,
- UnitValue,
- type StyleProperty,
-} from "@webstudio-is/css-engine";
+import type { StyleProperty, StyleValue } from "@webstudio-is/css-engine";
+import { XAxisIcon, YAxisIcon, ZAxisIcon } from "@webstudio-is/icons";
+import { propertySyntaxes } from "@webstudio-is/css-data";
import { CssValueInputContainer } from "../../shared/css-value-input";
import {
- updateTransformTuplePropertyValue,
- type TransformPanelProps,
-} from "./transform-utils";
-import type { StyleUpdateOptions } from "../../shared/use-style-data";
-import { XAxisIcon, YAxisIcon, ZAxisIcon } from "@webstudio-is/icons";
-import { parseCssValue, propertySyntaxes } from "@webstudio-is/css-data";
+ setProperty,
+ type StyleUpdateOptions,
+} from "../../shared/use-style-data";
import { PropertyInlineLabel } from "../../property-label";
+import { useComputedStyleDecl } from "../../shared/model";
const property: StyleProperty = "translate";
-export const TranslatePanelContent = (props: TransformPanelProps) => {
- const { propertyValue, setProperty } = props;
- const [translateX, translateY, translateZ] = propertyValue.value;
+export const TranslatePanelContent = () => {
+ const styleDecl = useComputedStyleDecl(property);
+ const tuple =
+ styleDecl.cascadedValue.type === "tuple"
+ ? styleDecl.cascadedValue
+ : undefined;
+ const [translateX, translateY, translateZ] = tuple?.value ?? [];
- const handlePropertyUpdate = (
- index: number,
+ const setAxis = (
+ axis: number,
newValue: StyleValue,
options?: StyleUpdateOptions
) => {
- if (newValue === undefined) {
+ if (tuple === undefined) {
return;
}
// For individual translate properties, we are passing the property as translate.
// This is sending back either tuple or a unit value when manually edited and when scrub is used respectively.
- let value: UnitValue = { type: "unit", value: 0, unit: "px" };
- if (newValue.type === "unit") {
- value = newValue;
- }
-
- if (newValue.type === "tuple" && newValue.value[0].type === "unit") {
- value = newValue.value[0];
+ if (newValue.type === "tuple") {
+ [newValue] = newValue.value;
}
-
- const translateValue = updateTransformTuplePropertyValue(
- index,
- value,
- propertyValue
- );
-
- const translate = parseCssValue(property, toValue(translateValue));
- if (translate.type === "invalid") {
- return;
+ if (newValue.type !== "unit") {
+ newValue = { type: "unit", value: 0, unit: "px" };
}
- setProperty(property)(translate, options);
+ const newTuple = structuredClone(tuple);
+ newTuple.value[axis] = newValue;
+ setProperty(property)(newTuple, options);
};
return (
@@ -68,14 +56,11 @@ export const TranslatePanelContent = (props: TransformPanelProps) => {
/>
<CssValueInputContainer
- key="translateX"
styleSource="local"
property={property}
value={translateX}
keywords={[]}
- setValue={(newValue, options) => {
- handlePropertyUpdate(0, newValue, options);
- }}
+ setValue={(newValue, options) => setAxis(0, newValue, options)}
deleteProperty={() => {}}
/>
</Grid>
@@ -89,14 +74,11 @@ export const TranslatePanelContent = (props: TransformPanelProps) => {
description={propertySyntaxes.translateY}
/>
<CssValueInputContainer
- key="translateX"
styleSource="local"
property={property}
value={translateY}
keywords={[]}
- setValue={(newValue, options) => {
- handlePropertyUpdate(1, newValue, options);
- }}
+ setValue={(newValue, options) => setAxis(1, newValue, options)}
deleteProperty={() => {}}
/>
</Grid>
@@ -110,14 +92,11 @@ export const TranslatePanelContent = (props: TransformPanelProps) => {
description={propertySyntaxes.translateZ}
/>
<CssValueInputContainer
- key="translateX"
styleSource="local"
property={property}
value={translateZ}
keywords={[]}
- setValue={(newValue, options) => {
- handlePropertyUpdate(2, newValue, options);
- }}
+ setValue={(newValue, options) => setAxis(2, newValue, options)}
deleteProperty={() => {}}
/>
</Grid>
diff --git a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-utils.test.ts b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-utils.test.ts
index dac527913e..45f0ac0f98 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-utils.test.ts
+++ b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-utils.test.ts
@@ -4,12 +4,9 @@ import {
handleDeleteTransformProperty,
handleHideTransformProperty,
isTransformPanelPropertyUsed,
- updateRotateOrSkewPropertyValue,
- updateTransformTuplePropertyValue,
} from "./transform-utils";
import type { StyleInfo } from "../../shared/style-info";
import {
- FunctionValue,
toValue,
TupleValue,
type StyleProperty,
@@ -211,55 +208,4 @@ describe("Transform utils CRUD operations", () => {
expect(rotate.rotateY?.hidden).toBe(true);
expect(rotate.rotateZ?.hidden).toBe(true);
});
-
- test("update translate property value", () => {
- const translate = parseCssValue("translate", "0px 0px 0px");
- const newValue = updateTransformTuplePropertyValue(
- 1,
- { type: "unit", value: 50, unit: "px" },
- translate as TupleValue
- );
-
- expect(toValue(newValue)).toBe("0px 50px 0px");
- });
-
- test("update rotate values in transform property", () => {
- const { currentStyle, setProperty } = initializeStyleInfo();
- const transform = parseCssValue(
- "transform",
- "rotateX(10deg) rotateY(10deg) rotateZ(10deg) skewX(10deg) skewY(10deg)"
- );
- setProperty("transform")(transform);
-
- const updatedRotateYValue: FunctionValue = {
- type: "function",
- name: "rotateY",
- args: {
- type: "layers",
- value: [{ type: "unit", value: 50, unit: "deg" }],
- },
- };
- const { rotateX, rotateY, rotateZ } =
- extractRotatePropertiesFromTransform(transform);
- const newValue = updateRotateOrSkewPropertyValue({
- index: 1,
- panel: "rotate",
- currentStyle,
- value: updatedRotateYValue,
- propertyValue: {
- type: "tuple",
- value: [
- rotateX as FunctionValue,
- rotateY as FunctionValue,
- rotateZ as FunctionValue,
- ],
- },
- });
-
- const result = extractRotatePropertiesFromTransform(newValue);
- expect(result.rotateY).toEqual(updatedRotateYValue);
- expect(toValue(newValue)).toBe(
- "rotateX(10deg) rotateY(50deg) rotateZ(10deg) skewX(10deg) skewY(10deg)"
- );
- });
});
diff --git a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-utils.ts b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-utils.ts
index bd850c70f2..fa39ce0453 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transforms/transform-utils.ts
+++ b/apps/builder/app/builder/features/style-panel/sections/transforms/transform-utils.ts
@@ -4,22 +4,36 @@ import {
StyleValue,
toValue,
type TupleValue,
- type TupleValueItem,
} from "@webstudio-is/css-engine";
-import type { DeleteProperty, SetProperty } from "../../shared/use-style-data";
+import {
+ setProperty,
+ type DeleteProperty,
+ type SetProperty,
+ type StyleUpdateOptions,
+} from "../../shared/use-style-data";
import type { StyleInfo } from "../../shared/style-info";
-import type { TransformPanel, transformPanelDropdown } from "./transforms";
import {
extractRotatePropertiesFromTransform,
extractSkewPropertiesFromTransform,
} from "./transform-extractors";
+import type { ComputedStyleDecl } from "~/shared/style-object-model";
-export type TransformPanelProps = {
- currentStyle: StyleInfo;
- propertyValue: TupleValue;
- setProperty: SetProperty;
- deleteProperty: DeleteProperty;
-};
+export const transformPanels = [
+ "translate",
+ "scale",
+ "rotate",
+ "skew",
+] as const;
+
+export const transformPanelDropdown = [
+ ...transformPanels,
+ "backfaceVisibility",
+ "transformOrigin",
+ "perspective",
+ "perspectiveOrigin",
+] as const;
+
+export type TransformPanel = (typeof transformPanels)[number];
const defaultTranslate = "0px 0px 0px";
const defaultScale = "100% 100% 100%";
@@ -350,45 +364,6 @@ export const handleHideTransformProperty = (params: {
}
};
-export const updateTransformTuplePropertyValue = (
- index: number,
- newValue: TupleValueItem,
- value: TupleValue
-): TupleValue => {
- const newArray: TupleValueItem[] = [...value.value];
- newArray.splice(index, 1, newValue);
- return {
- ...value,
- value: newArray,
- };
-};
-
-export const updateRotateOrSkewPropertyValue = (props: {
- index: number;
- panel: "rotate" | "skew";
- currentStyle: StyleInfo;
- value: FunctionValue;
- propertyValue: TupleValue;
-}): TupleValue => {
- const { index, value, propertyValue, panel } = props;
- const newPropertyValue = updateTransformTuplePropertyValue(
- index,
- value,
- propertyValue
- );
-
- const existingTransforms = props.currentStyle["transform"]?.value;
- if (existingTransforms?.type === "tuple") {
- const filteredValues = removeRotateOrSkewValues(panel, existingTransforms);
- return {
- ...existingTransforms,
- value: [...newPropertyValue.value, ...filteredValues],
- };
- }
-
- return newPropertyValue;
-};
-
const keywordToValue: Record<string, number> = {
left: 0,
right: 100,
@@ -412,3 +387,49 @@ export const calculatePositionFromOrigin = (value: StyleValue | undefined) => {
return 0;
};
+
+const transformFunctions = ["rotateX", "rotateY", "rotateZ", "skewX", "skewY"];
+
+export const updateTransformFunction = (
+ styleDecl: ComputedStyleDecl,
+ name: string,
+ newValue: StyleValue,
+ options?: StyleUpdateOptions
+) => {
+ const tuple =
+ styleDecl.cascadedValue.type === "tuple"
+ ? styleDecl.cascadedValue
+ : undefined;
+ if (tuple === undefined) {
+ return;
+ }
+
+ if (newValue.type === "tuple") {
+ [newValue] = newValue.value;
+ }
+ if (newValue.type !== "unit") {
+ newValue = { type: "unit", value: 0, unit: "deg" };
+ }
+
+ const matched = new Map<string, FunctionValue>();
+ for (const item of tuple.value) {
+ if (item.type === "function") {
+ matched.set(item.name, item);
+ }
+ }
+ matched.set(name, {
+ type: "function",
+ name,
+ args: { type: "layers", value: [newValue] },
+ });
+ const newTuple = structuredClone(tuple);
+ // recreate tuple with strictly ordered functions
+ newTuple.value = [];
+ for (const name of transformFunctions) {
+ const functionValue = matched.get(name);
+ if (functionValue) {
+ newTuple.value.push(functionValue);
+ }
+ }
+ setProperty("transform")(newTuple, options);
+};
diff --git a/apps/builder/app/builder/features/style-panel/sections/transforms/transforms.tsx b/apps/builder/app/builder/features/style-panel/sections/transforms/transforms.tsx
index 959318bde5..d37ae169a9 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transforms/transforms.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/transforms/transforms.tsx
@@ -30,7 +30,9 @@ import {
handleDeleteTransformProperty,
handleHideTransformProperty,
getHumanizedTextFromTransformLayer,
- type TransformPanelProps,
+ transformPanels,
+ transformPanelDropdown,
+ type TransformPanel,
} from "./transform-utils";
import { FloatingPanel } from "~/builder/shared/floating-panel";
import { TranslatePanelContent } from "./transform-translate";
@@ -46,24 +48,8 @@ import { PropertySectionLabel } from "../../property-label";
import { propertyDescriptions } from "@webstudio-is/css-data";
import { useComputedStyles } from "../../shared/model";
-export const transformPanels = [
- "translate",
- "scale",
- "rotate",
- "skew",
-] as const;
-
-export const transformPanelDropdown = [
- ...transformPanels,
- "backfaceVisibility",
- "transformOrigin",
- "perspective",
- "perspectiveOrigin",
-] as const;
-
-export type TransformPanel = (typeof transformPanels)[number];
-
const label = "Transforms";
+
export const properties = [
"translate",
"scale",
@@ -194,24 +180,15 @@ const TransformSection = (
return;
}
- const contentPanelProps: TransformPanelProps = {
- currentStyle,
- setProperty,
- deleteProperty,
- propertyValue: properties.value,
- };
-
return (
<FloatingPanel
title={humanizeString(panel)}
content={
<Flex direction="column" css={{ p: theme.spacing[9] }}>
- {panel === "translate" && (
- <TranslatePanelContent {...contentPanelProps} />
- )}
- {panel === "scale" && <ScalePanelContent {...contentPanelProps} />}
- {panel === "rotate" && <RotatePanelContent {...contentPanelProps} />}
- {panel === "skew" && <SkewPanelContent {...contentPanelProps} />}
+ {panel === "translate" && <TranslatePanelContent />}
+ {panel === "scale" && <ScalePanelContent />}
+ {panel === "rotate" && <RotatePanelContent />}
+ {panel === "skew" && <SkewPanelContent />}
</Flex>
}
>
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select-options.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select-options.ts
index 996a5ea9e0..98461b74e6 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select-options.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select-options.ts
@@ -1,10 +1,5 @@
import type { CssValueInputValue } from "./css-value-input";
-import {
- keywordValues,
- properties,
- units,
- isValidDeclaration,
-} from "@webstudio-is/css-data";
+import { properties, units, isValidDeclaration } from "@webstudio-is/css-data";
import type { UnitOption } from "./unit-select";
// To make sorting stable
@@ -120,39 +115,5 @@ export const buildOptions = (
indexSortValue(preferedSorting.indexOf(optionB.id))
);
- // This value can't have units, skip select
- // (show keywords menu instead)
- if (options.length === 0) {
- return [];
- }
-
- const propertyKeywordsSet = new Set(
- keywordValues[property as keyof typeof keywordValues]
- );
-
- // Opinionated set of keywords to show
- const webstudioKeywords = ["auto", "normal", "none"].filter((keyword) =>
- propertyKeywordsSet.has(keyword as never)
- );
-
- for (const keyword of webstudioKeywords) {
- options.push({
- id: keyword,
- label: keyword,
- type: "keyword",
- });
- }
-
- if (
- value.type === "keyword" &&
- options.some((option) => option.id === value.value) === false
- ) {
- options.push({
- id: value.value,
- label: value.value,
- type: "keyword",
- });
- }
-
return options;
};
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select.test.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select.test.ts
index c198130b52..a5394ebd6e 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select.test.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select.test.ts
@@ -58,11 +58,6 @@ describe("Unit menu options", () => {
"label": "ch",
"type": "unit",
},
- {
- "id": "auto",
- "label": "auto",
- "type": "keyword",
- },
]
`);
});
@@ -131,11 +126,6 @@ describe("Unit menu options", () => {
"label": "ch",
"type": "unit",
},
- {
- "id": "auto",
- "label": "auto",
- "type": "keyword",
- },
]
`);
});
@@ -149,14 +139,4 @@ describe("Unit menu options", () => {
).some((option) => option.id === "ch")
).toBe(true);
});
-
- test("Should add keyword to options", () => {
- expect(
- buildOptions(
- "width",
- { value: "inherit", type: "keyword" },
- nestedSelectButtonUnitless
- ).some((option) => option.id === "inherit")
- ).toBe(true);
- });
});