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.

upper-cased unit rendering

Oleg Isonen507286076d65
diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx
index 46afe1a740..8b9c02fd5a 100644
--- a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx
+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx
@@ -8,15 +8,12 @@ import {
ComboboxListbox,
ComboboxListboxItem,
IconButton,
- Select,
numericScrubControl,
} from "@webstudio-is/design-system";
import { ChevronDownIcon } from "@webstudio-is/icons";
import {
type KeywordValue,
type StyleProperty,
- type Unit,
- type UnitValue,
type UnsetValue,
StyleValue,
} from "@webstudio-is/react-sdk";
@@ -25,19 +22,9 @@ import {
type KeyboardEvent,
useEffect,
useRef,
- useState,
} from "react";
+import { useUnitSelect } from "./unit-select";
-export const defaultUnits: Array<Unit | "-"> = [
- "px",
- "em",
- "rem",
- "ch",
- "vw",
- "vh",
- "%",
- "-",
-];
const defaultKeywords: [] = [];
const defaultValue: UnsetValue = { type: "unset", value: "" };
@@ -89,47 +76,6 @@ const useHandleOnChange = (
}, [value]);
};
-type UseUnitSelectType = {
- value?: UnitValue;
- onChange: (value: StyleValue) => void;
- units?: Array<Unit | "-">;
-};
-
-const useUnitSelect = ({
- onChange,
- value,
- units = defaultUnits,
- ...props
-}: UseUnitSelectType) => {
- const [isOpen, setIsOpen] = useState(false);
- if (value === undefined) return [isOpen, null];
- const element = (
- <Select
- {...props}
- value={value.unit === "number" ? "-" : value.unit}
- options={units}
- suffix={null}
- ghost
- open={isOpen}
- onOpenChange={setIsOpen}
- onChange={(item) => {
- // @todo Select should support generics
- const unit = item === "-" ? "number" : (item as Unit);
- onChange?.({
- ...value,
- unit,
- });
- }}
- onCloseAutoFocus={(event) => {
- // We don't want to focus the unit trigger when closing the select (no matter if unit was selected, clicked outside or esc was pressed)
- event.preventDefault();
- }}
- />
- );
-
- return [isOpen, element];
-};
-
const useScrub = (options: {
value: StyleValue;
onChange: (value: StyleValue) => void;
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
new file mode 100644
index 0000000000..d042d231eb
--- /dev/null
+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/unit-select.tsx
@@ -0,0 +1,66 @@
+import { Select } from "@webstudio-is/design-system";
+import { type Unit, type UnitValue, StyleValue } from "@webstudio-is/react-sdk";
+import { useState, useMemo } from "react";
+
+const unitRenderMap: Map<Unit, string> = new Map([
+ ["px", "PX"],
+ ["%", "%"],
+ ["em", "EM"],
+ ["rem", "REM"],
+ ["ch", "CH"],
+ ["vw", "VW"],
+ ["vh", "VH"],
+ ["number", "-"],
+]);
+
+const renderUnitMap: Map<string, Unit> = new Map();
+for (const [key, value] of unitRenderMap.entries()) {
+ renderUnitMap.set(value, key);
+}
+
+const defaultUnits = Array.from(unitRenderMap.keys());
+
+type UseUnitSelectType = {
+ value?: UnitValue;
+ onChange: (value: StyleValue) => void;
+ units?: Array<Unit>;
+};
+
+export const useUnitSelect = ({
+ onChange,
+ value,
+ units = defaultUnits,
+ ...props
+}: UseUnitSelectType) => {
+ const [isOpen, setIsOpen] = useState(false);
+ const renderUnits = useMemo(
+ () => units.map((unit) => unitRenderMap.get(unit) ?? unit),
+ [units]
+ );
+ if (value === undefined) return [isOpen, null];
+ const element = (
+ <Select
+ {...props}
+ value={unitRenderMap.get(value.unit)}
+ options={renderUnits}
+ suffix={null}
+ ghost
+ open={isOpen}
+ onOpenChange={setIsOpen}
+ onChange={(item) => {
+ const unit = renderUnitMap.get(item);
+ if (unit === undefined) return;
+ onChange?.({
+ ...value,
+ unit,
+ });
+ }}
+ onCloseAutoFocus={(event) => {
+ // We don't want to focus the unit trigger when closing the select (no matter if unit was selected, clicked outside or esc was pressed)
+ event.preventDefault();
+ }}
+ />
+ );
+
+ return [isOpen, element];
+};