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.

code review, refactor unit-less logic, error state for props that do not support units

Sultan24ccb57674dc
diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx b/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx
index ecaf1f7d1d..9b2a1c4ea4 100644
--- a/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx
+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx
@@ -1,5 +1,10 @@
import { useState, useMemo } from "react";
-import { type Unit, type UnitValue, StyleValue } from "@webstudio-is/react-sdk";
+import {
+ type Unit,
+ type UnitValue,
+ StyleValue,
+ toValue,
+} from "@webstudio-is/react-sdk";
import * as SelectPrimitive from "@radix-ui/react-select";
import {
styled,
@@ -50,10 +55,7 @@ export const useUnitSelect = ({
() =>
units
.filter((unit) => {
- return isValid(
- property,
- value?.value + String(unit).replace("number", "")
- );
+ return isValid(property, toValue({ ...value, unit } as typeof value));
})
.map((unit) => unitRenderMap.get(unit) ?? unit),
[units]
diff --git a/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts b/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts
index 5c057fa76e..9a6cabd6ac 100644
--- a/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts
+++ b/apps/designer/app/designer/features/style-panel/shared/get-final-value.ts
@@ -1,5 +1,12 @@
-import type { Style, StyleValue, StyleProperty } from "@webstudio-is/react-sdk";
+import {
+ Style,
+ StyleValue,
+ StyleProperty,
+ toValue,
+ InvalidValue,
+} from "@webstudio-is/react-sdk";
import type { InheritedStyle } from "./get-inherited-style";
+import { isValid } from "./parse-css-value";
// @todo expose which instance we inherited the value from
export const getFinalValue = ({
@@ -17,5 +24,12 @@ export const getFinalValue = ({
if (currentValue?.value === "inherit" && inheritedValue !== undefined) {
return inheritedValue;
}
+ if (
+ currentValue &&
+ currentValue.type === "unit" &&
+ isValid(property, toValue(currentValue)) === false
+ ) {
+ return { ...(currentValue as unknown as InvalidValue), type: "invalid" };
+ }
return currentValue;
};
diff --git a/apps/designer/app/designer/features/style-panel/shared/parse-css-value.ts b/apps/designer/app/designer/features/style-panel/shared/parse-css-value.ts
index e5056fcc85..c4d1dbb5de 100644
--- a/apps/designer/app/designer/features/style-panel/shared/parse-css-value.ts
+++ b/apps/designer/app/designer/features/style-panel/shared/parse-css-value.ts
@@ -2,7 +2,7 @@ import hyphenate from "hyphenate-style-name";
import { units } from "@webstudio-is/react-sdk";
import type { StyleProperty, StyleValue, Unit } from "@webstudio-is/react-sdk";
-const unitRegex = new RegExp(`${units.join("|")}`);
+const unitRegex = new RegExp(`${units.concat("number").join("|")}`);
export const isValid = (property: string, value: string): boolean => {
// Only browsers with houdini api will provide validation for now
diff --git a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
index 0b2d3b3906..d199b7d220 100644
--- a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
+++ b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
@@ -101,13 +101,10 @@ export const useStyleData = ({
const toStyleValue = (property: StyleProperty, value: string) => {
if (currentStyle === undefined) return;
- const currentValue = currentStyle[property];
- const defaultUnit =
- currentValue?.type === "unit" ? currentValue?.unit : undefined;
if (property === "fontFamily") {
return { type: "fontFamily" as const, value: [value] };
}
- return parseCssValue(property, value, defaultUnit);
+ return parseCssValue(property, value, "number");
};
const setProperty: SetProperty = (property) => {
diff --git a/packages/design-system/src/components/combobox.tsx b/packages/design-system/src/components/combobox.tsx
index 2664688e45..fbf53839cf 100644
--- a/packages/design-system/src/components/combobox.tsx
+++ b/packages/design-system/src/components/combobox.tsx
@@ -24,7 +24,8 @@ import { Box } from "./box";
import { Grid } from "./grid";
const Listbox = styled("ul", panelStyles, {
- padding: "$1 0",
+ py: "$1",
+ px: 0,
margin: 0,
overflow: "auto",
// @todo need some non-hardcoded value
diff --git a/packages/react-sdk/src/css/units.ts b/packages/react-sdk/src/css/units.ts
index 1dede3ee72..a733273860 100644
--- a/packages/react-sdk/src/css/units.ts
+++ b/packages/react-sdk/src/css/units.ts
@@ -29,5 +29,4 @@ export const units = [
"vw",
"x",
"%",
- "number",
] as const;
diff --git a/packages/react-sdk/src/stitches/to-css.ts b/packages/react-sdk/src/stitches/to-css.ts
index 46556167c4..3823549234 100644
--- a/packages/react-sdk/src/stitches/to-css.ts
+++ b/packages/react-sdk/src/stitches/to-css.ts
@@ -2,10 +2,10 @@ import type { CSS } from "./stitches";
import type { StyleProperty, StyleValue, CssRule, Breakpoint } from "../css";
import { DEFAULT_FONT_FALLBACK, SYSTEM_FONTS } from "@webstudio-is/fonts";
-export const toValue = (value: StyleValue | undefined): string => {
+export const toValue = (value?: StyleValue): string => {
if (value === undefined) return "";
if (value.type === "unit") {
- return value.value + value.unit;
+ return value.value + (value.unit === "number" ? "" : value.unit);
}
if (value.type === "fontFamily") {
const family = value.value[0];