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 onEnter and onBlur

Oleg Isonen914278603921
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 74c67ceeae..92afea3ef8 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
@@ -28,11 +28,42 @@ export const defaultUnits: Array<Unit> = [...unsortedUnits].sort((unit) =>
? -1
: 1
);
-
const defaultKeywords: [] = [];
-
const defaultValue: UnsetValue = { type: "unset", value: "" };
+const useOnChange = (
+ value: StyleValue,
+ input: string,
+ onChange: (value: StyleValue) => void
+) => {
+ // Used to decouple onChange effect from value ref change
+ const valueRef = useRef<StyleValue>(value);
+
+ useEffect(() => {
+ valueRef.current = value;
+ }, [value]);
+
+ useEffect(() => {
+ if (input === valueRef.current.value) return;
+
+ // We want to switch to unit mode if entire input is a number.
+ if (/^\d+$/.test(input)) {
+ onChange?.({
+ type: "unit",
+ // Use previously known unit or fallback to px.
+ unit: valueRef.current.type === "unit" ? valueRef.current.unit : "px",
+ value: Number(input),
+ });
+ return;
+ }
+
+ onChange?.({
+ type: "keyword",
+ value: input,
+ });
+ }, [input, onChange]);
+};
+
type UnitSelectType = {
value: UnitValue;
onChange: (value: StyleValue) => void;
@@ -78,6 +109,7 @@ type CssValueInputProps = {
* - When text is a number - unit mode
* - Enter or blur submits the value
* - 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
@@ -123,34 +155,8 @@ export const CssValueInput = ({
});
const inputProps = getInputProps();
- // Used to decouple onChange effect from value ref change
- const valueRef = useRef<StyleValue>(value);
-
- useEffect(() => {
- valueRef.current = value;
- }, [value]);
-
- useEffect(() => {
- const input = inputProps.value;
- if (input === valueRef.current.value) return;
-
- // We want to switch to unit mode if entire input is a number.
- if (/^\d+$/.test(input)) {
- onChange?.({
- type: "unit",
- // Use previously known unit or fallback to px.
- unit: valueRef.current.type === "unit" ? valueRef.current.unit : "px",
- value: Number(input),
- });
- return;
- }
-
- onChange?.({
- type: "keyword",
- value: input,
- });
- }, [inputProps.value, onChange]);
+ useOnChange(value, inputProps.value, onChange);
const suffix =
value.type === "keyword" ? (
@@ -170,6 +176,16 @@ export const CssValueInput = ({
name={property}
state={value.type === "invalid" ? "invalid" : undefined}
suffix={suffix}
+ onKeyDown={(event) => {
+ if (event.key === "Enter") {
+ onChangeComplete(value);
+ }
+ inputProps.onKeyDown(event);
+ }}
+ onBlur={(event) => {
+ onChangeComplete(value);
+ inputProps.onBlur(event);
+ }}
/>
</ComboboxPopperAnchor>
<ComboboxPopperContent align="start" sideOffset={5}>
diff --git a/packages/design-system/src/components/combobox.stories.tsx b/packages/design-system/src/components/combobox.stories.tsx
index 82d174a428..7807371382 100644
--- a/packages/design-system/src/components/combobox.stories.tsx
+++ b/packages/design-system/src/components/combobox.stories.tsx
@@ -1,9 +1,13 @@
import type { ComponentStory } from "@storybook/react";
import { MagnifyingGlassIcon } from "@webstudio-is/icons";
-import { useCombobox as useDownshiftCombobox } from "downshift";
import React, { useCallback } from "react";
import { TextField } from "./text-field";
-import { Combobox, ComboboxListboxItem, useCombobox } from "./combobox";
+import {
+ Combobox,
+ ComboboxListboxItem,
+ useCombobox,
+ comboboxStateChangeTypes,
+} from "./combobox";
import { Flex } from "./flex";
export default {
@@ -37,11 +41,10 @@ export const Complex: ComponentStory<typeof Combobox> = () => {
const { type, changes } = actionAndChanges;
switch (type) {
// on item selection.
- case useDownshiftCombobox.stateChangeTypes.ItemClick:
- case useDownshiftCombobox.stateChangeTypes.InputKeyDownEnter:
- case useDownshiftCombobox.stateChangeTypes.InputBlur:
- case useDownshiftCombobox.stateChangeTypes
- .ControlledPropUpdatedSelectedItem:
+ case comboboxStateChangeTypes.ItemClick:
+ case comboboxStateChangeTypes.InputKeyDownEnter:
+ case comboboxStateChangeTypes.InputBlur:
+ case comboboxStateChangeTypes.ControlledPropUpdatedSelectedItem:
return {
...changes,
// if we have a selected item.
@@ -52,7 +55,7 @@ export const Complex: ComponentStory<typeof Combobox> = () => {
};
// Remove "reset" action
- case useDownshiftCombobox.stateChangeTypes.InputKeyDownEscape: {
+ case comboboxStateChangeTypes.InputKeyDownEscape: {
return {
...state,
};