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.

handle unit select

Oleg Isonen62bf7461beaf
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 eb83a5654d..3d733f4caa 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
@@ -20,7 +20,7 @@ import {
StyleValue,
units as unsortedUnits,
} from "@webstudio-is/react-sdk";
-import { useEffect, useRef } from "react";
+import { useEffect, useRef, useState } from "react";
// @todo sorting doesn't work
export const defaultUnits: Array<Unit> = [...unsortedUnits].sort((unit) =>
@@ -64,25 +64,29 @@ const useOnChange = (
}, [input, onChange]);
};
-type UnitSelectType = {
- value: UnitValue;
+type UseUnitSelectType = {
+ value?: UnitValue;
onChange: (value: StyleValue) => void;
units?: Array<Unit>;
};
-const UnitSelect = ({
+const useUnitSelect = ({
onChange,
value,
units = defaultUnits,
...props
-}: UnitSelectType) => {
- return (
+}: UseUnitSelectType) => {
+ const [isUnitsOpen, setIsUnitsOpen] = useState(false);
+ if (value === undefined) return [isUnitsOpen, null];
+ const element = (
<Select
{...props}
value={value.unit}
options={units}
suffix={null}
ghost
+ open={isUnitsOpen}
+ onOpenChange={setIsUnitsOpen}
onChange={(unit) => {
onChange?.({
...value,
@@ -91,13 +95,14 @@ const UnitSelect = ({
}}
/>
);
+
+ return [isUnitsOpen, element];
};
type CssValueInputProps = {
property: StyleProperty;
value?: StyleValue;
keywords?: Array<KeywordValue>;
- units?: Array<Unit>;
onChange: (value: StyleValue) => void;
onChangeComplete: (value: StyleValue) => void;
onItemHighlight?: (value: StyleValue | null) => void;
@@ -109,7 +114,6 @@ type CssValueInputProps = {
* - When text is a number - unit mode
* - Enter or blur calls onChangeComplete
* - After submission, when value is an invalid CSS value - invalid mode
- * - When hovering over keywords and units list, onItemHighlight is called
*
* Unit mode:
* - Unit selection on unit button click
@@ -127,13 +131,13 @@ type CssValueInputProps = {
* - Arrow keys are used to navigate keyword items
* - Enter key or click is used to select item when list is open
* - Escape key is used to close list
+ * - When hovering over keywords list, onItemHighlight is called
*/
export const CssValueInput = ({
property,
value = defaultValue,
keywords = defaultKeywords,
- units,
onChange,
onChangeComplete,
onItemHighlight,
@@ -161,13 +165,18 @@ export const CssValueInput = ({
useOnChange(value, inputProps.value, onChange);
+ const [isUnitsOpen, unitSelectElement] = useUnitSelect({
+ value: value.type === "unit" ? value : undefined,
+ onChange: onChangeComplete,
+ });
+
const suffix =
value.type === "keyword" ? (
<IconButton {...getToggleButtonProps()}>
<ChevronDownIcon />
</IconButton>
) : value.type === "unit" ? (
- <UnitSelect value={value} onChange={onChange} units={units} />
+ unitSelectElement
) : null;
return (
@@ -186,6 +195,8 @@ export const CssValueInput = ({
inputProps.onKeyDown(event);
}}
onBlur={(event) => {
+ // When units select is open, onBlur is triggered,though we don't want a change event in this case.
+ if (isUnitsOpen) return;
onChangeComplete(value);
inputProps.onBlur(event);
}}
diff --git a/packages/design-system/src/components/select.tsx b/packages/design-system/src/components/select.tsx
index 41623396a9..1d1c4381d5 100644
--- a/packages/design-system/src/components/select.tsx
+++ b/packages/design-system/src/components/select.tsx
@@ -1,5 +1,4 @@
import * as SelectPrimitive from "@radix-ui/react-select";
-import noop from "lodash.noop";
import React, { ReactNode, Ref } from "react";
import { Grid } from "./grid";
import { Box } from "./box";
@@ -153,6 +152,8 @@ export type SelectProps<Option = SelectOption> = Omit<
defaultValue?: Option;
value?: Option;
onChange?: (option: Option) => void;
+ open?: boolean;
+ onOpenChange?: (open: boolean) => void;
placeholder?: string;
getLabel?: (option: Option) => string | undefined;
suffix?: JSX.Element | null;
@@ -164,7 +165,9 @@ const SelectBase = (
value,
defaultValue,
placeholder = "Select an option",
- onChange = noop,
+ onChange,
+ onOpenChange,
+ open,
getLabel = (option) => option,
name,
suffix = defaultSuffix,
@@ -178,6 +181,8 @@ const SelectBase = (
value={value}
defaultValue={defaultValue}
onValueChange={onChange}
+ open={open}
+ onOpenChange={onOpenChange}
>
<StyledTrigger ref={forwardedRef} withSuffix={Boolean(suffix)} {...props}>
<SelectPrimitive.Value>