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.

Typing "-" in unitless value types should select the unitless (#1060)

Ivan Starkov4af2ab9e1b1c
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
index 2c4bbf0182..3886825326 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
@@ -35,6 +35,7 @@ import { useDebouncedCallback } from "use-debounce";
import type { StyleSource } from "../style-info";
import { toPascalCase } from "../keyword-utils";
import { theme } from "@webstudio-is/design-system";
+import { isValid } from "../parse-css-value";
// We increment by 10 when shift is pressed, by 0.1 when alt/option is pressed and by 1 by default.
const calcNumberChange = (
@@ -357,6 +358,10 @@ export const CssValueInput = ({
const [isUnitsOpen, unitSelectElement] = useUnitSelect({
property,
+ showUnitless:
+ value.type === "unit" || value.type === "intermediate"
+ ? isValid(property, `${value.value}`)
+ : false,
value:
value.type === "unit" || value.type === "intermediate"
? value.unit
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts
index e715e56971..4f9d29be96 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts
@@ -13,25 +13,38 @@ const unitsList = Object.values(units).flat();
export const parseIntermediateOrInvalidValue = (
property: StyleProperty,
- value: IntermediateStyleValue | InvalidValue
+ styleValue: IntermediateStyleValue | InvalidValue
): StyleValue => {
+ const value = styleValue.value.trim();
+
// Try value with existing or fallback unit
- const unit = "unit" in value ? value.unit ?? "px" : "px";
- let styleInput = parseCssValue(property, `${value.value}${unit}`);
+ const unit = "unit" in styleValue ? styleValue.unit ?? "px" : "px";
+ let styleInput = parseCssValue(property, `${value}${unit}`);
if (styleInput.type !== "invalid") {
return styleInput;
}
// Probably value is already valid, use it
- styleInput = parseCssValue(property, value.value);
+ styleInput = parseCssValue(property, value);
if (styleInput.type !== "invalid") {
return styleInput;
}
+ if (unit === "number") {
+ // Most css props supports 0 as unitless value, but not other numbers.
+ // Its possible that we had { value: 0, unit: "number" } and value has changed
+ // Lets try to parse it as px value
+ styleInput = parseCssValue(property, `${value}px`);
+
+ if (styleInput.type !== "invalid") {
+ return styleInput;
+ }
+ }
+
// Probably in kebab-case value will be valid
- styleInput = parseCssValue(property, toKebabCase(value.value));
+ styleInput = parseCssValue(property, toKebabCase(value));
if (styleInput.type !== "invalid") {
return styleInput;
@@ -41,14 +54,23 @@ export const parseIntermediateOrInvalidValue = (
// Try to extract/remove anything similar to unit value
const unitRegex = new RegExp(`(?:${unitsList.join("|")})`, "g");
- const matchedUnit = value.value.match(unitRegex)?.[0];
- const unitlessValue = value.value.replace(unitRegex, "");
+ let matchedUnit = value.match(unitRegex)?.[0];
+
+ let unitlessValue = value.replace(unitRegex, "");
+
+ // If value ends with "-" it is probably a unitless value i.e. unit = number
+ if (unitlessValue.endsWith("-")) {
+ unitlessValue = unitlessValue.slice(0, -1).trim();
+ // If we have matched unit, use it, otherwise try unitless value
+ matchedUnit = matchedUnit === undefined ? "" : matchedUnit;
+ }
// Try to evaluate math expression if possible
const mathResult = evaluateMath(unitlessValue);
if (mathResult != null) {
- const unit = matchedUnit ?? ("unit" in value ? value.unit ?? "px" : "px");
+ const unit =
+ matchedUnit ?? ("unit" in styleValue ? styleValue.unit ?? "px" : "px");
styleInput = parseCssValue(property, `${String(mathResult)}${unit}`);
if (styleInput.type !== "invalid") {
@@ -67,6 +89,6 @@ export const parseIntermediateOrInvalidValue = (
// or value is invalid
return {
type: "invalid",
- value: value.value,
+ value: value,
};
};
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts
index 129fdd97a5..f9848e984d 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts
@@ -209,3 +209,116 @@ describe("Returns invalid if can't parse", () => {
});
});
});
+
+describe("Value ending with `-` should be considered unitless", () => {
+ test("Unitless intermediate transformed to unitless", () => {
+ const result = parseIntermediateOrInvalidValue("lineHeight", {
+ type: "intermediate",
+ value: "10-",
+ });
+
+ expect(result).toEqual({
+ type: "unit",
+ value: 10,
+ unit: "number",
+ });
+ });
+
+ test("Unit intermediate transformed to unitless", () => {
+ const result = parseIntermediateOrInvalidValue("lineHeight", {
+ type: "intermediate",
+ value: "10-",
+ unit: "em",
+ });
+
+ expect(result).toEqual({
+ type: "unit",
+ value: 10,
+ unit: "number",
+ });
+ });
+
+ test("Unit intermediate with space transformed to unitless", () => {
+ const result = parseIntermediateOrInvalidValue("lineHeight", {
+ type: "intermediate",
+ value: "10 -",
+ unit: "em",
+ });
+
+ expect(result).toEqual({
+ type: "unit",
+ value: 10,
+ unit: "number",
+ });
+ });
+
+ test("Unit number intermediate transformed to unitless", () => {
+ const result = parseIntermediateOrInvalidValue("lineHeight", {
+ type: "intermediate",
+ value: "10",
+ unit: "number",
+ });
+
+ expect(result).toEqual({
+ type: "unit",
+ value: 10,
+ unit: "number",
+ });
+ });
+
+ test("Unitless expression transformed to unitless", () => {
+ const result = parseIntermediateOrInvalidValue("lineHeight", {
+ type: "intermediate",
+ value: "10 + 20 -",
+ unit: "px",
+ });
+
+ expect(result).toEqual({
+ type: "unit",
+ value: 30,
+ unit: "number",
+ });
+ });
+
+ test("Expression containing unit and unitless must be a unit", () => {
+ const result = parseIntermediateOrInvalidValue("lineHeight", {
+ type: "intermediate",
+ value: "10px + 20 -",
+ unit: "px",
+ });
+
+ expect(result).toEqual({
+ type: "unit",
+ value: 30,
+ unit: "px",
+ });
+ });
+
+ test("top with 0 should be unitless", () => {
+ const result = parseIntermediateOrInvalidValue("top", {
+ type: "intermediate",
+ value: "0-",
+ unit: "em",
+ });
+
+ expect(result).toEqual({
+ type: "unit",
+ value: 0,
+ unit: "number",
+ });
+ });
+
+ test("top with value 10 should have unit px", () => {
+ const result = parseIntermediateOrInvalidValue("top", {
+ type: "intermediate",
+ value: "10",
+ unit: "number",
+ });
+
+ expect(result).toEqual({
+ type: "unit",
+ value: 10,
+ unit: "px",
+ });
+ });
+});
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select.tsx b/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select.tsx
index 344568d6ab..4d44e587bf 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select.tsx
@@ -24,6 +24,7 @@ const visibleLengthUnits = ["px", "em", "rem", "ch", "vw", "vh"] as const;
type UseUnitSelectType = {
property: string;
value?: Unit;
+ showUnitless: boolean;
onChange: (value: Unit) => void;
onCloseAutoFocus: (event: Event) => void;
};
@@ -31,6 +32,8 @@ type UseUnitSelectType = {
export const useUnitSelect = ({
property,
value,
+ // edge-case, most css properties accept unitless value 0
+ showUnitless,
onChange,
onCloseAutoFocus,
}: UseUnitSelectType): [boolean, JSX.Element | null] => {
@@ -50,8 +53,13 @@ export const useUnitSelect = ({
options.push({ id: unit, label: unit.toLocaleUpperCase() });
}
}
+
+ if (showUnitless && !options.some((o) => o.id === "number")) {
+ options.push({ id: "number", label: "—" });
+ }
+
return options;
- }, [property]);
+ }, [property, showUnitless]);
if (
options.length === 0 ||
@@ -107,8 +115,9 @@ const UnitSelect = ({
<SelectPrimitive.SelectTrigger asChild>
<StyledTrigger variant="unit">
<SelectPrimitive.Value>
- {/* fallback to uppercased value for not listed units */}
- {matchedOption?.label ?? value.toLocaleUpperCase()}
+ {/* fallback to uppercased value for not listed units, show - for number unit */}
+ {matchedOption?.label ??
+ (value === "number" ? "—" : value.toLocaleUpperCase())}
</SelectPrimitive.Value>
</StyledTrigger>
</SelectPrimitive.SelectTrigger>
diff --git a/apps/builder/app/builder/features/style-panel/shared/parse-css-value.ts b/apps/builder/app/builder/features/style-panel/shared/parse-css-value.ts
index 03ffd1191b..ad2be3b288 100644
--- a/apps/builder/app/builder/features/style-panel/shared/parse-css-value.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/parse-css-value.ts
@@ -1,12 +1,7 @@
import * as csstree from "css-tree";
import hyphenate from "hyphenate-style-name";
-import type {
- StyleProperty,
- StyleValue,
- Unit,
- UnitGroup,
-} from "@webstudio-is/css-data";
-import { units, properties, keywordValues } from "@webstudio-is/css-data";
+import type { StyleProperty, StyleValue, Unit } from "@webstudio-is/css-data";
+import { units, keywordValues } from "@webstudio-is/css-data";
import warnOnce from "warn-once";
const cssTryParseValue = (input: string) => {
@@ -82,25 +77,20 @@ export const parseCssValue = (
) {
// Try extract units from 1st children
const first = ast.children.first;
- const unitGroups = properties[property as keyof typeof properties]
- .unitGroups as ReadonlyArray<UnitGroup>;
if (first?.type === "Number") {
- if (unitGroups.includes("number")) {
- return {
- type: "unit",
- unit: "number",
- value: Number(first.value),
- };
- }
- return invalidValue;
+ return {
+ type: "unit",
+ unit: "number",
+ value: Number(first.value),
+ };
}
if (first?.type === "Dimension") {
const unit = first.unit as typeof units[keyof typeof units][number];
- for (const unitGroup of unitGroups) {
- const possibleUnits = units[unitGroup] as ReadonlyArray<typeof unit>;
- if (possibleUnits.includes(unit)) {
+
+ for (const unitGroup of Object.values(units)) {
+ if (unitGroup.includes(unit as never)) {
return {
type: "unit",
unit: unit as Unit,
@@ -112,15 +102,13 @@ export const parseCssValue = (
}
if (first?.type === "Percentage") {
- if (unitGroups.includes("percentage")) {
- return {
- type: "unit",
- unit: "%",
- value: Number(first.value),
- };
- }
- return invalidValue;
+ return {
+ type: "unit",
+ unit: "%",
+ value: Number(first.value),
+ };
}
+
if (first?.type === "Identifier") {
const values = keywordValues[
property as keyof typeof keywordValues
@@ -131,7 +119,6 @@ export const parseCssValue = (
value: input,
};
}
- return invalidValue;
}
}