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.

add scrub control to unit inputs

Sultan4593063a4ea5
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 a2d276197b..0caaee7a7b 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 { ChangeEvent, FocusEvent, KeyboardEvent } from "react";
+import { ChangeEvent, FocusEvent, KeyboardEvent, useRef } from "react";
import {
Box,
Text,
@@ -7,6 +7,7 @@ import {
TextField,
Tooltip,
IconButtonWithMenu,
+ numericScrubControl,
} from "@webstudio-is/design-system";
import { getFinalValue } from "../../shared/get-final-value";
import { useIsFromCurrentBreakpoint } from "../../shared/use-is-from-current-breakpoint";
@@ -53,6 +54,7 @@ export const TextControl = ({
setProperty,
styleConfig,
}: ControlProps) => {
+ const numericScrubRef = useRef<{ disconnectedCallback: () => void }>();
const isCurrentBreakpoint = useIsFromCurrentBreakpoint(styleConfig.property);
const value = getFinalValue({
@@ -63,9 +65,10 @@ export const TextControl = ({
if (value === undefined) return null;
+ // @todo setValue has a latency issue when updating a value fast(i.e keyboard arrows/scrub control)
const setValue = setProperty(styleConfig.property);
- const Icon = iconConfigs[styleConfig.property]?.normal || "noscript";
+ const Icon = iconConfigs[styleConfig.property]?.normal;
return (
<Grid
@@ -79,8 +82,21 @@ export const TextControl = ({
delayDuration={700 / 4}
disableHoverableContent={true}
>
- {/* @todo unit values should feature the ScrubControl */}
<IconButton
+ onPointerEnter={({ target }) => {
+ if (value.type !== "unit") return;
+ numericScrubRef.current =
+ numericScrubRef.current ||
+ numericScrubControl(target as HTMLInputElement, {
+ initialValue: value.value as number,
+ onValueChange: ({ value }) => setValue(String(value)),
+ });
+ }}
+ onPointerLeave={() => {
+ if (value.type !== "unit") return;
+ numericScrubRef.current?.disconnectedCallback();
+ numericScrubRef.current = undefined;
+ }}
variant="ghost"
size="1"
css={{
@@ -94,7 +110,7 @@ export const TextControl = ({
}),
}}
>
- <Icon />
+ {Icon && <Icon />}
</IconButton>
</Tooltip>
<TextField
diff --git a/packages/design-system/src/components/primitives/numeric-gesture-control.stories.tsx b/packages/design-system/src/components/primitives/numeric-gesture-control.stories.tsx
index 40c18b1705..edbd663ea7 100644
--- a/packages/design-system/src/components/primitives/numeric-gesture-control.stories.tsx
+++ b/packages/design-system/src/components/primitives/numeric-gesture-control.stories.tsx
@@ -22,8 +22,8 @@ const useNumericScrubControl = ({
direction: direction,
onValueChange: (event) => {
event.preventDefault();
- event.target.value = String(event.value);
- event.target.select();
+ (event.target as HTMLInputElement).value = String(event.value);
+ (event.target as HTMLInputElement).select();
},
});
return () => disconnectedCallback();
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 a3f983fed2..befb8ddcd4 100644
--- a/packages/design-system/src/components/primitives/numeric-gesture-control.ts
+++ b/packages/design-system/src/components/primitives/numeric-gesture-control.ts
@@ -23,8 +23,8 @@ export type NumericScrubOptions = {
maxValue?: NumericScrubValue;
initialValue?: NumericScrubValue;
direction?: NumericScrubDirection;
- onValueChange: (event: {
- target: HTMLInputElement;
+ onValueChange?: (event: {
+ target: HTMLElement;
value: NumericScrubValue;
preventDefault: () => void;
}) => void;
@@ -39,7 +39,7 @@ type NumericScrubState = {
};
export const numericScrubControl = (
- targetNode: HTMLInputElement,
+ targetNode: HTMLElement,
{
minValue = Number.MIN_SAFE_INTEGER,
maxValue = Number.MAX_SAFE_INTEGER,