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.

implement scrub amendments to spec

Sultande658fd6c3bc
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 10eca62c7d..7389886290 100644
--- a/packages/design-system/src/components/primitives/numeric-gesture-control.ts
+++ b/packages/design-system/src/components/primitives/numeric-gesture-control.ts
@@ -39,6 +39,7 @@ type NumericScrubState = {
offset: number;
velocity: number;
direction: string;
+ timeout: unknown;
};
export const numericScrubControl = (
@@ -52,26 +53,14 @@ export const numericScrubControl = (
onValueChange,
}: NumericScrubOptions
) => {
- const eventNames = ["pointerup", "pointerdown", "pointermove"] as const;
+ const eventNames = ["pointerup", "pointerdown"] as const;
const state: NumericScrubState = {
value: initialValue,
cursor: undefined,
offset: 0,
velocity: direction === "horizontal" ? 1 : -1,
direction: direction,
- };
- const handleCursor = (
- targetNode: HTMLElement | HTMLInputElement,
- shouldSetCursor: boolean
- ) => {
- if (shouldSetCursor) {
- targetNode.style.setProperty(
- "cursor",
- direction === "horizontal" ? "ew-resize" : "ns-resize"
- );
- } else {
- targetNode.style.removeProperty("cursor");
- }
+ timeout: 0,
};
const handleEvent = (event: PointerEvent) => {
const { type, clientX, clientY, movementY, movementX } = event;
@@ -79,22 +68,28 @@ export const numericScrubControl = (
const movement = direction === "horizontal" ? movementX : -movementY;
switch (type) {
case "pointerup": {
+ let shouldComponentUpdate = Boolean(state.cursor);
state.offset = 0;
- handleCursor(targetNode.ownerDocument.documentElement, false);
+ targetNode.removeEventListener("pointermove", handleEvent);
+ clearTimeout(state.timeout as number);
exitPointerLock(state, event, targetNode);
- onValueChange?.({
- target: targetNode,
- value: state.value,
- preventDefault: () => event.preventDefault(),
- });
+ if (shouldComponentUpdate)
+ onValueChange?.({
+ target: targetNode,
+ value: state.value,
+ preventDefault: () => event.preventDefault(),
+ });
break;
}
case "pointerdown": {
// light touches don't register corresponding pointerup
- if (event.pressure === 0) break;
+ if (event.pressure === 0 || event.button !== 0) break;
state.offset = offset;
- handleCursor(targetNode.ownerDocument.documentElement, true);
- requestPointerLock(state, event, targetNode);
+ state.timeout = setTimeout(
+ () => requestPointerLock(state, event, targetNode),
+ 150
+ );
+ targetNode.addEventListener("pointermove", handleEvent);
break;
}
case "pointermove": {
@@ -111,17 +106,24 @@ export const numericScrubControl = (
preventDefault: () => event.preventDefault(),
});
}
+ if (state.cursor) {
+ state.cursor.style.top = `${
+ parseFloat(state.cursor.style.top) + event.movementY
+ }px`;
+ state.cursor.style[
+ state.direction === "horizontal" ? "left" : "top"
+ ] = `${state.offset}px`;
+ }
break;
}
}
};
- handleCursor(targetNode, true);
+
eventNames.forEach((eventName) =>
targetNode.addEventListener(eventName, handleEvent)
);
return {
disconnectedCallback: () => {
- handleCursor(targetNode, false);
eventNames.forEach((eventName) =>
targetNode.removeEventListener(eventName, handleEvent)
);
@@ -164,14 +166,11 @@ const requestPointerLock = (
if (state.cursor) {
targetNode.ownerDocument.documentElement.append(state.cursor);
}
- targetNode.onpointermove = () => {
- if (state.cursor) {
- state.cursor.style[
- state.direction === "horizontal" ? "left" : "top"
- ] = `${state.offset}px`;
- }
- };
} else {
+ targetNode.ownerDocument.documentElement.style.setProperty(
+ "cursor",
+ state.direction === "horizontal" ? "ew-resize" : "ns-resize"
+ );
targetNode.setPointerCapture(event.pointerId);
}
};
@@ -189,6 +188,7 @@ const exitPointerLock = (
targetNode.onpointermove = null;
targetNode.ownerDocument.exitPointerLock();
} else {
+ targetNode.ownerDocument.documentElement.style.removeProperty("cursor");
targetNode.releasePointerCapture(event.pointerId);
}
};