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.

Canvas width arrow keys (#1437)

Oleg Isonene069870d3fb0
diff --git a/apps/builder/app/builder/features/breakpoints/width-input.tsx b/apps/builder/app/builder/features/breakpoints/width-input.tsx
index cecf3962aa..fdc007d6b4 100644
--- a/apps/builder/app/builder/features/breakpoints/width-input.tsx
+++ b/apps/builder/app/builder/features/breakpoints/width-input.tsx
@@ -9,6 +9,7 @@ import {
InputField,
useId,
useScrub,
+ handleNumericInputArrowKeys,
} from "@webstudio-is/design-system";
import { useCanvasWidth } from "~/builder/shared/nano-states";
import { breakpointsStore, isResizingCanvasStore } from "~/shared/nano-states";
@@ -37,6 +38,8 @@ const useEnhancedInput = ({
}) => {
const [intermediateValue, setIntermediateValue] = useState<number>();
+ const currentValue = intermediateValue ?? value;
+
const handleChange = (nextValue: number) => {
onChange(Math.max(nextValue, minWidth));
setIntermediateValue(undefined);
@@ -61,14 +64,19 @@ const useEnhancedInput = ({
},
onKeyDown(event: KeyboardEvent<HTMLInputElement>) {
if (event.key === "Enter") {
- handleChangeComplete(event.currentTarget.valueAsNumber);
+ return handleChangeComplete(event.currentTarget.valueAsNumber);
+ }
+ const nextValue = handleNumericInputArrowKeys(currentValue, event);
+ if (nextValue !== currentValue) {
+ event.preventDefault();
+ handleChange(nextValue);
}
},
onBlur() {
handleChangeComplete(inputRef.current?.valueAsNumber ?? 0);
},
type: "number" as const,
- value: intermediateValue ?? value,
+ value: currentValue,
};
};
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 e8b871828b..9a49f477a3 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
@@ -11,6 +11,7 @@ import {
NestedSelectButton,
NestedIconLabel,
InputField,
+ handleNumericInputArrowKeys,
} from "@webstudio-is/design-system";
import { ChevronDownIcon } from "@webstudio-is/icons";
import type {
@@ -41,16 +42,6 @@ import {
} from "~/shared/nano-states";
import { convertUnits } from "./convert-units";
-// We increment by 10 when shift is pressed, by 0.1 when alt/option is pressed and by 1 by default.
-const calcNumberChange = (
- value: number,
- { altKey, shiftKey, key }: { altKey: boolean; shiftKey: boolean; key: string }
-) => {
- const delta = shiftKey ? 10 : altKey ? 0.1 : 1;
- const multiplier = key === "ArrowUp" ? 1 : -1;
- return Number((value + delta * multiplier).toFixed(1));
-};
-
const useScrub = ({
value,
onChange,
@@ -190,7 +181,7 @@ const useHandleKeyDown =
onChange({
type: "unit",
- value: calcNumberChange(inputValue, event),
+ value: handleNumericInputArrowKeys(inputValue, event),
unit: value.unit,
});
// Prevent Downshift from opening menu on arrow up/down
diff --git a/packages/design-system/src/components/primitives/numeric-input-arrow-keys.ts b/packages/design-system/src/components/primitives/numeric-input-arrow-keys.ts
new file mode 100644
index 0000000000..3dfe95a165
--- /dev/null
+++ b/packages/design-system/src/components/primitives/numeric-input-arrow-keys.ts
@@ -0,0 +1,12 @@
+// We increment by 10 when shift is pressed, by 0.1 when alt/option is pressed and by 1 by default.
+export const handleNumericInputArrowKeys = (
+ value: number,
+ { altKey, shiftKey, key }: { altKey: boolean; shiftKey: boolean; key: string }
+) => {
+ if (key !== "ArrowUp" && key !== "ArrowDown") {
+ return value;
+ }
+ const delta = shiftKey ? 10 : altKey ? 0.1 : 1;
+ const multiplier = key === "ArrowUp" ? 1 : -1;
+ return Number((value + delta * multiplier).toFixed(1));
+};
diff --git a/packages/design-system/src/index.ts b/packages/design-system/src/index.ts
index f9e1965de3..e3a64e8c33 100644
--- a/packages/design-system/src/index.ts
+++ b/packages/design-system/src/index.ts
@@ -78,6 +78,7 @@ export * from "./components/primitives/is-truncated";
export * from "./components/primitives/arrow-focus";
export * from "./components/scroll-area";
export * from "./components/primitives/use-scrub";
+export * from "./components/primitives/numeric-input-arrow-keys";
// Deprecated