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 types

Oleg Isonencca8801fbe0c
diff --git a/app/shared/design-system/components/primitives/numericGestureControl.stories.jsx b/app/shared/design-system/components/primitives/numericGestureControl.stories.jsx
deleted file mode 100644
index 2914c288dc..0000000000
--- a/app/shared/design-system/components/primitives/numericGestureControl.stories.jsx
+++ /dev/null
@@ -1,39 +0,0 @@
-import {useRef, useEffect} from 'react';
-import {numericGestureControl} from './numericGestureControl.js';
-
-const useNumericGestureControl = ({ref, value, direction}) => {
- useEffect(() => {
- const {disconnectedCallback} = numericGestureControl(ref.current, {
- initialValue: ref.current.value = value,
- direction: direction,
- onValueChange: (event) => {
- event.preventDefault();
- event.target.value = event.value;
- event.target.select();
- }
- });
- return () => disconnectedCallback();
- }, [direction]);
-}
-
-const Input = ({value, direction}) => {
- const ref = useRef();
- useNumericGestureControl({ref, value, direction});
- return <input defaultValue={value} ref={ref} />;
-}
-
-export const NumericInput = Object.assign(Input.bind({}), {args: { value: 0, direction: 'horizontal' }});
-
-export default {
- title: 'numericGestureControl',
- component: Input,
- argTypes: {
- value: {
- control: { type: 'number' },
- },
- direction: {
- options: ['horizontal', 'vertical'],
- control: { type: 'radio' },
- },
- },
-};
diff --git a/app/shared/design-system/components/primitives/numericGestureControl.stories.tsx b/app/shared/design-system/components/primitives/numericGestureControl.stories.tsx
new file mode 100644
index 0000000000..99e37435f4
--- /dev/null
+++ b/app/shared/design-system/components/primitives/numericGestureControl.stories.tsx
@@ -0,0 +1,61 @@
+import { useRef, useEffect, RefObject } from "react";
+import {
+ numericGestureControl,
+ type Value,
+ type Direction,
+} from "./numericGestureControl";
+
+const useNumericGestureControl = ({
+ ref,
+ value,
+ direction,
+}: {
+ ref: RefObject<HTMLInputElement>;
+ value: Value;
+ direction: Direction;
+}) => {
+ useEffect(() => {
+ if (ref.current === null) return;
+ ref.current.value = String(value);
+ const { disconnectedCallback } = numericGestureControl(ref.current, {
+ initialValue: value,
+ direction: direction,
+ onValueChange: (event) => {
+ event.preventDefault();
+ event.target.value = String(event.value);
+ event.target.select();
+ },
+ });
+ return () => disconnectedCallback();
+ }, [direction, value]);
+};
+
+const Input = ({
+ value,
+ direction,
+}: {
+ value: Value;
+ direction: Direction;
+}) => {
+ const ref = useRef<HTMLInputElement | null>(null);
+ useNumericGestureControl({ ref, value, direction });
+ return <input defaultValue={value} ref={ref} />;
+};
+
+export const NumericInput = Object.assign(Input.bind({}), {
+ args: { value: 0, direction: "horizontal" },
+});
+
+export default {
+ title: "numericGestureControl",
+ component: Input,
+ argTypes: {
+ value: {
+ control: { type: "number" },
+ },
+ direction: {
+ options: ["horizontal", "vertical"],
+ control: { type: "radio" },
+ },
+ },
+};
diff --git a/app/shared/design-system/components/primitives/numericGestureControl.js b/app/shared/design-system/components/primitives/numericGestureControl.ts
similarity index 85%
rename from app/shared/design-system/components/primitives/numericGestureControl.js
rename to app/shared/design-system/components/primitives/numericGestureControl.ts
index 43bc613b9d..df7b14afb3 100644
--- a/app/shared/design-system/components/primitives/numericGestureControl.js
+++ b/app/shared/design-system/components/primitives/numericGestureControl.ts
@@ -13,15 +13,34 @@
* }
* });
*/
+
+export type Direction = "horizontal" | "vertical";
+
+export type Value = number;
+
+type Options = {
+ initialValue: Value;
+ direction: Direction;
+ onValueChange: (event: {
+ target: HTMLInputElement;
+ value: Value;
+ preventDefault: () => void;
+ }) => void;
+};
+
export const numericGestureControl = (
- targetNode,
- { initialValue = 0, direction = "horizontal", onValueChange = ({}) => null }
+ targetNode: HTMLInputElement,
+ {
+ initialValue = 0,
+ direction = "horizontal",
+ onValueChange = () => null,
+ }: Options
) => {
const { ownerDocument } = targetNode;
- const eventNames = ["pointerup", "pointerdown", "pointermove"];
+ const eventNames = ["pointerup", "pointerdown", "pointermove"] as const;
const state = {
value: initialValue,
- cursor: cursorNode.cloneNode(true),
+ cursor: cursorNode.cloneNode(true) as HTMLElement,
offset: 0,
velocity: direction === "horizontal" ? 1 : -1,
position: direction === "horizontal" ? "left" : "top",
@@ -31,7 +50,7 @@ export const numericGestureControl = (
direction === "horizontal" ? "0 1px" : "1px 0"
} 1.1px rgba(0,0,0,.4))`,
};
- const handleEvent = (event) => {
+ const handleEvent = (event: PointerEvent) => {
const { type, pressure, offsetX, offsetY, movementY, movementX } = event;
const offset = direction === "horizontal" ? offsetX : offsetY;
const movement = direction === "horizontal" ? movementX : -movementY;
@@ -77,7 +96,9 @@ export const numericGestureControl = (
else if (state.offset > globalThis.innerWidth) state.offset = 1;
state.value += movement;
state.offset += movement * state.velocity;
- state.cursor.style[state.position] = `${state.offset}px`;
+ state.cursor.style[
+ state.position as "top" | "left"
+ ] = `${state.offset}px`;
onValueChange({
target: targetNode,
value: state.value,
@@ -109,4 +130,4 @@ const cursorNode = new Range().createContextualFragment(`
<path d="M 15 4.5L 15 2L 11.5 5.5L 15 9L 15 6.5L 31 6.5L 31 9L 34.5 5.5L 31 2L 31 4.5Z" fill="#111" fill-rule="evenodd"></path>
</g>
</svg>
-`).firstElementChild;
+`).firstElementChild as HTMLElement;