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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
experimental: add scaling `x` and `y` inputs to be changed together using locked behaviour (#3812)
Jaya Krishna Namburuc97f053de8f5
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.tsxindex e85455f16f..160e1b89f9 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,109 +1,321 @@-import { Flex, Grid, Label } from "@webstudio-is/design-system";+import {+ Box,+ EnhancedTooltip,+ Flex,+ Grid,+ Label,+ SmallToggleButton,+ theme,+} from "@webstudio-is/design-system";import {StyleValue,toValue,+ UnitValue,type StyleProperty,+ type Unit,} from "@webstudio-is/css-engine";-import { CssValueInputContainer } from "../../shared/css-value-input";-import type { StyleUpdateOptions } from "../../shared/use-style-data";+import { CssValueInput } from "../../shared/css-value-input";+import { type TransformPanelProps } from "./transform-utils";import {- updateTransformTuplePropertyValue,- type TransformPanelProps,-} from "./transform-utils";-import { XAxisIcon, YAxisIcon, ZAxisIcon } from "@webstudio-is/icons";+ BorderRadiusIcon,+ XAxisIcon,+ YAxisIcon,+ ZAxisIcon,+ Link2Icon,+ Link2UnlinkedIcon,+} from "@webstudio-is/icons";import { parseCssValue } from "@webstudio-is/css-data";+import { useCallback, useState } from "react";+import type { StyleUpdateOptions } from "../../shared/use-style-data";+import type { IntermediateStyleValue } from "../../shared/css-value-input/css-value-input";-// We use fakeProperty to pass for the CssValueInputContainer.-// As we know during parsing, the syntax for scale is wrong in the css-data package.-// https://github.com/mdn/data/pull/746-// https://developer.mozilla.org/en-US/docs/Web/CSS/opacity#syntax-// number | percentage-const fakeProperty = "opacity";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 } = props;+ const { propertyValue, setProperty, deleteProperty } = props;const [scaleX, scaleY, scaleZ] = propertyValue.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 handlePropertyUpdate = (- index: number,- value: StyleValue,- options?: StyleUpdateOptions- ) => {- if (value.type !== "unit") {- return;+ const handleToggleScaling = () => {+ const lockScaling = isScalingLocked === true ? false : true;+ setScalingLock(lockScaling);+ if (lockScaling === true) {+ setIntermediateScalingY(intermediateScalingX);+ updateScaleValues(+ {+ scaleX: intermediateScalingX,+ scaleY: intermediateScalingX,+ scaleZ: intermediateScalingZ,+ },+ { isEphemeral: false }+ );}+ };- const newValue = updateTransformTuplePropertyValue(- index,- value,- propertyValue- );+ 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 scale = parseCssValue(property, toValue(newValue));- if (scale.type === "invalid") {- return;- }+ const result = parseCssValue(+ "scale",+ `${toValue(x)} ${toValue(y)} ${toValue(z)}`+ );- setProperty(property)(scale, options);- };+ 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 direction="column" gap={2}>- <Grid- gap={1}- css={{ alignItems: "center", gridTemplateColumns: "auto 1fr 1fr" }}- >- <XAxisIcon />- <Label> Scale X</Label>- <CssValueInputContainer- key="scaleX"- styleSource="local"- property={fakeProperty}- value={scaleX}- keywords={[]}- setValue={(newValue, options) => {- handlePropertyUpdate(0, newValue, options);- }}- deleteProperty={() => {}}- />- </Grid>- <Grid- gap={1}- css={{ alignItems: "center", gridTemplateColumns: "auto 1fr 1fr" }}- >- <YAxisIcon />- <Label> Scale Y</Label>- <CssValueInputContainer- key="scaleY"- styleSource="local"- property={fakeProperty}- value={scaleY}- keywords={[]}- setValue={(newValue, options) => {- handlePropertyUpdate(1, newValue, options);- }}- deleteProperty={() => {}}- />- </Grid>- <Grid- gap={1}- css={{ alignItems: "center", gridTemplateColumns: "auto 1fr 1fr" }}+ <Flex>+ <Flex direction="column" gap={2}>+ <Grid+ gap={1}+ css={{ alignItems: "center", gridTemplateColumns: "auto 1fr 1fr" }}+ >+ <XAxisIcon />+ <Label> Scale X</Label>+ <CssValueInput+ key="scaleX"+ styleSource="local"+ property={fakeProperty}+ keywords={[]}+ value={scaleX}+ intermediateValue={intermediateScalingX}+ onChange={(value) => {+ if (value === undefined) {+ deleteProperty(property, { isEphemeral: true });+ return;+ }++ handleOnChange("scaleX", value, { isEphemeral: true });+ }}+ onChangeComplete={(value) => {+ if (value === undefined) {+ deleteProperty(property, { isEphemeral: true });+ return;+ }++ handleOnChange("scaleX", value.value, { isEphemeral: false });+ }}+ onHighlight={(value) => {+ if (value === undefined) {+ deleteProperty(property, { isEphemeral: true });+ return;+ }++ handleOnChange("scaleX", value, { isEphemeral: true });+ }}+ onAbort={() => deleteProperty(property, { isEphemeral: true })}+ />+ </Grid>+ <Grid+ gap={1}+ css={{ alignItems: "center", gridTemplateColumns: "auto 1fr 1fr" }}+ >+ <YAxisIcon />+ <Label> Scale Y</Label>+ <CssValueInput+ key="scaleY"+ styleSource="local"+ property={fakeProperty}+ keywords={[]}+ value={scaleY}+ intermediateValue={intermediateScalingY}+ onChange={(value) => {+ if (value === undefined) {+ deleteProperty(property, { isEphemeral: true });+ return;+ }++ handleOnChange("scaleY", value, { isEphemeral: true });+ }}+ onChangeComplete={(value) => {+ if (value === undefined) {+ deleteProperty(property, { isEphemeral: true });+ return;+ }++ handleOnChange("scaleY", value.value, { isEphemeral: false });+ }}+ onHighlight={(value) => {+ if (value === undefined) {+ deleteProperty(property, { isEphemeral: true });+ return;+ }++ handleOnChange("scaleY", value, { isEphemeral: true });+ }}+ onAbort={() => deleteProperty(property, { isEphemeral: true })}+ />+ </Grid>+ <Grid+ gap={1}+ css={{ alignItems: "center", gridTemplateColumns: "auto 1fr 1fr" }}+ >+ <ZAxisIcon />+ <Label> Scale Z</Label>+ <CssValueInput+ key="scaleZ"+ styleSource="local"+ property={fakeProperty}+ keywords={[]}+ value={scaleZ}+ intermediateValue={intermediateScalingZ}+ onChange={(value) => {+ if (value === undefined) {+ deleteProperty(property, { isEphemeral: true });+ return;+ }+ handleOnChange("scaleZ", value, { isEphemeral: true });+ }}+ onChangeComplete={(value) => {+ if (value === undefined) {+ deleteProperty(property, { isEphemeral: true });+ return;+ }++ handleOnChange("scaleZ", value.value, { isEphemeral: false });+ }}+ onHighlight={(value) => {+ if (value === undefined) {+ deleteProperty(property, { isEphemeral: true });+ return;+ }++ handleOnChange("scaleZ", value, { isEphemeral: true });+ }}+ onAbort={() => deleteProperty(property, { isEphemeral: true })}+ />+ </Grid>+ </Flex>+ <Flex+ direction="column"+ css={{ paddingLeft: theme.spacing[5], marginTop: theme.spacing[3] }}>- <ZAxisIcon />- <Label> Scale Z</Label>- <CssValueInputContainer- key="scaleZ"- styleSource="local"- property={fakeProperty}- value={scaleZ}- keywords={[]}- setValue={(newValue, options) => {- handlePropertyUpdate(2, newValue, options);- }}- deleteProperty={() => {}}- />- </Grid>+ <Box css={{ rotate: "90deg" }}>+ <BorderRadiusIcon size="12" />+ </Box>+ <EnhancedTooltip+ content={+ isScalingLocked === true+ ? "Unlink scale-x and scale-y values"+ : "Link scale-x and scale-y values"+ }+ >+ <SmallToggleButton+ css={{ rotate: "90deg" }}+ pressed={isScalingLocked}+ onPressedChange={handleToggleScaling}+ variant="normal"+ icon={isScalingLocked ? <Link2Icon /> : <Link2UnlinkedIcon />}+ />+ </EnhancedTooltip>+ <Box css={{ rotate: "180deg" }}>+ <BorderRadiusIcon size="12" />+ </Box>+ </Flex></Flex>);};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.tsindex d7dd8ece64..26fd7b74d8 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@@ -78,18 +78,18 @@ describe("Transform utils CRUD operations", () => {value: [{type: "unit",- unit: "number",- value: 1,+ unit: "%",+ value: 100,},{type: "unit",- unit: "number",- value: 1,+ unit: "%",+ value: 100,},{type: "unit",- unit: "number",- value: 1,+ unit: "%",+ value: 100,},],});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.tsindex 77475d267b..e0495af6cf 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@@ -14,10 +14,11 @@ export type TransformPanelProps = {currentStyle: StyleInfo;propertyValue: TupleValue;setProperty: SetProperty;+ deleteProperty: DeleteProperty;};const defaultTranslate = "0px 0px 0px";-const defaultScale = "1 1 1";+const defaultScale = "100% 100% 100%";const defaultRotate = "rotateX(0deg) rotateY(0deg) rotateZ(0deg)";const defaultSkew = "skewX(0deg) skewY(0deg)";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.tsxindex a5c9279334..e900ac4023 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@@ -192,6 +192,7 @@ const TransformSection = (const contentPanelProps: TransformPanelProps = {currentStyle,setProperty,+ deleteProperty,propertyValue: properties.value,};