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.

fix and simply numericScrubControl useage

Sultanc69566844b6c
diff --git a/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx b/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx
index acb6ece07c..d6697a3f3e 100644
--- a/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx
@@ -1,4 +1,4 @@
-import { FocusEvent, KeyboardEvent, useRef, useCallback } from "react";
+import { FocusEvent, KeyboardEvent, useRef, PointerEvent } from "react";
import {
Box,
Text,
@@ -41,25 +41,6 @@ export const TextControl = ({
const inputRef = useRef<HTMLInputElement | null>(null);
- const numericScrubRefCallback = useCallback(
- (node: HTMLButtonElement) => {
- if (!value) return;
- const id = Symbol.for("numericScrubControl");
- if (!node) return Object(node)[id]?.disconnectedCallback();
- if (value.type !== "unit") return;
- if (Object(node)[id]) return;
- Object(node)[id] = numericScrubControl(node, {
- initialValue: value.value,
- onValueChange: (event) => {
- if (inputRef.current) inputRef.current.value = String(event.value);
- handleChange("unit", event.value, true);
- },
- });
- },
- // eslint-disable-next-line react-hooks/exhaustive-deps
- []
- );
-
if (value === undefined) return null;
const setValue = setProperty(styleConfig.property);
@@ -97,7 +78,7 @@ export const TextControl = ({
}
};
const handleKeyDownArrowUpDown = (event: KeyboardEvent<HTMLInputElement>) => {
- let currentValue = parseFloat(event.target.value);
+ let currentValue = parseFloat(event.currentTarget.value);
let currentDelta = 1;
if (event.shiftKey) currentDelta = 10;
if (event.altKey) currentDelta = 0.1;
@@ -109,7 +90,7 @@ export const TextControl = ({
Math.abs(currentValue).toString().indexOf(".") + 2
)
: String(currentValue);
- event.target.value = currentValueAsString;
+ event.currentTarget.value = currentValueAsString;
setValue(currentValueAsString, { isEphemeral: true });
};
@@ -148,10 +129,20 @@ export const TextControl = ({
property={styleConfig.property}
label={styleConfig.label}
{...(value.type === "unit" && {
- onPointerUp: () => {
- setValue(String(inputRef.current?.value));
+ onPointerEnter: (event: PointerEvent<HTMLElement>) => {
+ Object(event.currentTarget)[Symbol.for("scrub")] ??=
+ numericScrubControl(event.currentTarget, {
+ initialValue: value.value,
+ onValueInput: (event) => {
+ if (inputRef.current)
+ inputRef.current.value = String(event.value);
+ handleChange("unit", event.value, true);
+ },
+ onValueChange: (event) => {
+ setValue(String(event.value));
+ },
+ });
},
- ref: numericScrubRefCallback,
})}
/>
}
diff --git a/packages/design-system/src/components/primitives/numeric-gesture-control.ts b/packages/design-system/src/components/primitives/numeric-gesture-control.ts
index befb8ddcd4..9dc6546595 100644
--- a/packages/design-system/src/components/primitives/numeric-gesture-control.ts
+++ b/packages/design-system/src/components/primitives/numeric-gesture-control.ts
@@ -18,16 +18,19 @@ export type NumericScrubDirection = "horizontal" | "vertical";
export type NumericScrubValue = number;
+export type NumericScrubCallback = (event: {
+ target: HTMLElement;
+ value: NumericScrubValue;
+ preventDefault: () => void;
+}) => void;
+
export type NumericScrubOptions = {
minValue?: NumericScrubValue;
maxValue?: NumericScrubValue;
initialValue?: NumericScrubValue;
direction?: NumericScrubDirection;
- onValueChange?: (event: {
- target: HTMLElement;
- value: NumericScrubValue;
- preventDefault: () => void;
- }) => void;
+ onValueInput?: NumericScrubCallback;
+ onValueChange?: NumericScrubCallback;
};
type NumericScrubState = {
@@ -45,6 +48,7 @@ export const numericScrubControl = (
maxValue = Number.MAX_SAFE_INTEGER,
initialValue = 0,
direction = "horizontal",
+ onValueInput = () => null,
onValueChange = () => null,
}: NumericScrubOptions
) => {
@@ -78,6 +82,11 @@ export const numericScrubControl = (
state.offset = 0;
handleCursor(targetNode.ownerDocument.documentElement, false);
exitPointerLock(state, event, targetNode);
+ onValueChange({
+ target: targetNode,
+ value: state.value,
+ preventDefault: () => event.preventDefault(),
+ });
break;
}
case "pointerdown": {
@@ -94,7 +103,7 @@ export const numericScrubControl = (
if (state.value < minValue) state.value = minValue;
else if (state.value > maxValue) state.value = maxValue;
state.offset += movement * state.velocity;
- onValueChange({
+ onValueInput({
target: targetNode,
value: state.value,
preventDefault: () => event.preventDefault(),