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 some tests & minor fixes

Roman Pominov8a554fc1f093
diff --git a/packages/design-system/src/components/primitives/dnd/__snapshots__/geometry-utils.test.ts.snap b/packages/design-system/src/components/primitives/dnd/__snapshots__/geometry-utils.test.ts.snap
new file mode 100644
index 0000000000..2cdb252ab3
--- /dev/null
+++ b/packages/design-system/src/components/primitives/dnd/__snapshots__/geometry-utils.test.ts.snap
@@ -0,0 +1,451 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`getPlacementInside childrenOrientation={ type: 'horizontal', reverse: false } 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 90,
+ "type": "inside-parent",
+ "x": 5,
+ "y": 5,
+}
+`;
+
+exports[`getPlacementInside childrenOrientation={ type: 'horizontal', reverse: true } 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 90,
+ "type": "inside-parent",
+ "x": 5,
+ "y": 5,
+}
+`;
+
+exports[`getPlacementInside childrenOrientation={ type: 'mixed' } 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 90,
+ "type": "inside-parent",
+ "x": 5,
+ "y": 5,
+}
+`;
+
+exports[`getPlacementInside childrenOrientation={ type: 'vertical', reverse: false } 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 90,
+ "type": "inside-parent",
+ "x": 5,
+ "y": 5,
+}
+`;
+
+exports[`getPlacementInside childrenOrientation={ type: 'vertical', reverse: true } 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 90,
+ "type": "inside-parent",
+ "x": 5,
+ "y": 5,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: false } direction=backward child almost same as parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 292,
+ "type": "next-to-child",
+ "x": 2,
+ "y": 4,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: false } direction=backward child bigger than parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 310,
+ "type": "next-to-child",
+ "x": -10,
+ "y": -10,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: false } direction=backward child same as parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 300,
+ "type": "next-to-child",
+ "x": 0,
+ "y": 0,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: false } direction=backward child smaller than parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 100,
+ "type": "next-to-child",
+ "x": 95,
+ "y": 100,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: false } direction=forward child almost same as parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 292,
+ "type": "next-to-child",
+ "x": 298,
+ "y": 4,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: false } direction=forward child bigger than parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 310,
+ "type": "next-to-child",
+ "x": 300,
+ "y": -10,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: false } direction=forward child same as parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 300,
+ "type": "next-to-child",
+ "x": 300,
+ "y": 0,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: false } direction=forward child smaller than parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 100,
+ "type": "next-to-child",
+ "x": 205,
+ "y": 100,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: true } direction=backward child almost same as parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 292,
+ "type": "next-to-child",
+ "x": 298,
+ "y": 4,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: true } direction=backward child bigger than parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 310,
+ "type": "next-to-child",
+ "x": 300,
+ "y": -10,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: true } direction=backward child same as parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 300,
+ "type": "next-to-child",
+ "x": 300,
+ "y": 0,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: true } direction=backward child smaller than parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 100,
+ "type": "next-to-child",
+ "x": 205,
+ "y": 100,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: true } direction=forward child almost same as parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 292,
+ "type": "next-to-child",
+ "x": 2,
+ "y": 4,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: true } direction=forward child bigger than parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 310,
+ "type": "next-to-child",
+ "x": -10,
+ "y": -10,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: true } direction=forward child same as parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 300,
+ "type": "next-to-child",
+ "x": 0,
+ "y": 0,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: true } direction=forward child smaller than parent 1`] = `
+Object {
+ "direction": "vertical",
+ "length": 100,
+ "type": "next-to-child",
+ "x": 95,
+ "y": 100,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=backward child almost same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 292,
+ "type": "next-to-child",
+ "x": 4,
+ "y": 2,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=backward child bigger than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 310,
+ "type": "next-to-child",
+ "x": -10,
+ "y": -10,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=backward child same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 300,
+ "type": "next-to-child",
+ "x": 0,
+ "y": 0,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=backward child smaller than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 100,
+ "type": "next-to-child",
+ "x": 100,
+ "y": 95,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=forward child almost same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 292,
+ "type": "next-to-child",
+ "x": 4,
+ "y": 298,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=forward child bigger than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 310,
+ "type": "next-to-child",
+ "x": -10,
+ "y": 300,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=forward child same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 300,
+ "type": "next-to-child",
+ "x": 0,
+ "y": 300,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=forward child smaller than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 100,
+ "type": "next-to-child",
+ "x": 100,
+ "y": 205,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: false } direction=backward child almost same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 292,
+ "type": "next-to-child",
+ "x": 4,
+ "y": 2,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: false } direction=backward child bigger than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 310,
+ "type": "next-to-child",
+ "x": -10,
+ "y": -10,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: false } direction=backward child same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 300,
+ "type": "next-to-child",
+ "x": 0,
+ "y": 0,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: false } direction=backward child smaller than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 100,
+ "type": "next-to-child",
+ "x": 100,
+ "y": 95,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: false } direction=forward child almost same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 292,
+ "type": "next-to-child",
+ "x": 4,
+ "y": 298,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: false } direction=forward child bigger than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 310,
+ "type": "next-to-child",
+ "x": -10,
+ "y": 300,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: false } direction=forward child same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 300,
+ "type": "next-to-child",
+ "x": 0,
+ "y": 300,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: false } direction=forward child smaller than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 100,
+ "type": "next-to-child",
+ "x": 100,
+ "y": 205,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: true } direction=backward child almost same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 292,
+ "type": "next-to-child",
+ "x": 4,
+ "y": 298,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: true } direction=backward child bigger than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 310,
+ "type": "next-to-child",
+ "x": -10,
+ "y": 300,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: true } direction=backward child same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 300,
+ "type": "next-to-child",
+ "x": 0,
+ "y": 300,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: true } direction=backward child smaller than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 100,
+ "type": "next-to-child",
+ "x": 100,
+ "y": 205,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: true } direction=forward child almost same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 292,
+ "type": "next-to-child",
+ "x": 4,
+ "y": 2,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: true } direction=forward child bigger than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 310,
+ "type": "next-to-child",
+ "x": -10,
+ "y": -10,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: true } direction=forward child same as parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 300,
+ "type": "next-to-child",
+ "x": 0,
+ "y": 0,
+}
+`;
+
+exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: true } direction=forward child smaller than parent 1`] = `
+Object {
+ "direction": "horizontal",
+ "length": 100,
+ "type": "next-to-child",
+ "x": 100,
+ "y": 95,
+}
+`;
diff --git a/packages/design-system/src/components/primitives/dnd/geometry-utils.test.ts b/packages/design-system/src/components/primitives/dnd/geometry-utils.test.ts
index 11ceff2cae..c92f9fa873 100644
--- a/packages/design-system/src/components/primitives/dnd/geometry-utils.test.ts
+++ b/packages/design-system/src/components/primitives/dnd/geometry-utils.test.ts
@@ -1,8 +1,155 @@
-import { getPlacementBetween } from "./geometry-utils";
+import {
+ getPlacementBetween,
+ getDistanceToRect,
+ getArea,
+ getPlacementNextTo,
+ getPlacementInside,
+} from "./geometry-utils";
-const size = { width: 100, height: 100 };
+describe("getDistanceToRect", () => {
+ const rect = {
+ top: 100,
+ left: 100,
+ width: 100,
+ height: 100,
+ };
+
+ test("inside", () => {
+ expect(getDistanceToRect(rect, { x: rect.left + 1, y: rect.top + 1 })).toBe(
+ 0
+ );
+ });
+
+ test("near top edge", () => {
+ expect(getDistanceToRect(rect, { x: rect.left + 1, y: rect.top - 2 })).toBe(
+ 2
+ );
+ });
+
+ test("near bottom edge", () => {
+ expect(
+ getDistanceToRect(rect, {
+ x: rect.left + 1,
+ y: rect.top + rect.height + 2,
+ })
+ ).toBe(2);
+ });
+
+ test("near left edge", () => {
+ expect(getDistanceToRect(rect, { x: rect.left - 2, y: rect.top + 1 })).toBe(
+ 2
+ );
+ });
+
+ test("near right edge", () => {
+ expect(
+ getDistanceToRect(rect, {
+ x: rect.left + rect.width + 2,
+ y: rect.top + 1,
+ })
+ ).toBe(2);
+ });
+
+ test("near top left corner", () => {
+ expect(
+ getDistanceToRect(rect, {
+ x: rect.left - 2,
+ y: rect.top - 2,
+ })
+ ).toBe(Math.sqrt(8));
+ });
+
+ test("near top right corner", () => {
+ expect(
+ getDistanceToRect(rect, {
+ x: rect.left + rect.width + 2,
+ y: rect.top - 2,
+ })
+ ).toBe(Math.sqrt(8));
+ });
+
+ test("near bottom left corner", () => {
+ expect(
+ getDistanceToRect(rect, {
+ x: rect.left - 2,
+ y: rect.top + rect.height + 2,
+ })
+ ).toBe(Math.sqrt(8));
+ });
+
+ test("near bottom right corner", () => {
+ expect(
+ getDistanceToRect(rect, {
+ x: rect.left + rect.width + 2,
+ y: rect.top + rect.height + 2,
+ })
+ ).toBe(Math.sqrt(8));
+ });
+});
+
+describe("getArea", () => {
+ const rect = {
+ top: 100,
+ left: 100,
+ width: 100,
+ height: 100,
+ };
+ const threshold = 10;
+
+ test("top", () => {
+ expect(
+ getArea(
+ { x: rect.left + rect.width / 2, y: rect.top + 1 },
+ threshold,
+ rect
+ )
+ ).toBe("top");
+ });
+
+ test("bottom", () => {
+ expect(
+ getArea(
+ { x: rect.left + rect.width / 2, y: rect.top + rect.height - 1 },
+ threshold,
+ rect
+ )
+ ).toBe("bottom");
+ });
+
+ test("left", () => {
+ expect(
+ getArea(
+ { x: rect.left + 1, y: rect.top + rect.height / 2 },
+ threshold,
+ rect
+ )
+ ).toBe("left");
+ });
+
+ test("right", () => {
+ expect(
+ getArea(
+ { x: rect.left + rect.width - 1, y: rect.top + rect.height / 2 },
+ threshold,
+ rect
+ )
+ ).toBe("right");
+ });
+
+ test("center", () => {
+ expect(
+ getArea(
+ { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 },
+ threshold,
+ rect
+ )
+ ).toBe("center");
+ });
+});
describe("getPlacementBetween", () => {
+ const size = { width: 100, height: 100 };
+
test.each([
[
// [a] | [b]
@@ -67,3 +214,63 @@ describe("getPlacementBetween", () => {
);
});
});
+
+describe("getPlacementNextTo", () => {
+ describe.each([
+ { type: "horizontal", reverse: false },
+ { type: "horizontal", reverse: true },
+ { type: "vertical", reverse: false },
+ { type: "vertical", reverse: true },
+ { type: "mixed" },
+ ] as const)("childrenOrientation=%o", (childrenOrientation) => {
+ describe.each(["forward", "backward"] as const)(
+ "direction=%s",
+ (direction) => {
+ test.each([
+ [
+ "child smaller than parent",
+ { top: 100, left: 100, width: 100, height: 100 },
+ ],
+ [
+ "child almost same as parent",
+ { top: 4, left: 4, width: 292, height: 292 },
+ ],
+ [
+ "child same as parent",
+ { top: 0, left: 0, width: 300, height: 300 },
+ ],
+ [
+ "child bigger than parent",
+ { top: -10, left: -10, width: 310, height: 310 },
+ ],
+ ])("%s", (_, child) => {
+ expect(
+ getPlacementNextTo(
+ { top: 0, left: 0, width: 300, height: 300 },
+ child,
+ childrenOrientation,
+ direction
+ )
+ ).toMatchSnapshot();
+ });
+ }
+ );
+ });
+});
+
+describe("getPlacementInside", () => {
+ test.each([
+ { type: "horizontal", reverse: false },
+ { type: "horizontal", reverse: true },
+ { type: "vertical", reverse: false },
+ { type: "vertical", reverse: true },
+ { type: "mixed" },
+ ] as const)("childrenOrientation=%o", (childrenOrientation) => {
+ expect(
+ getPlacementInside(
+ { top: 0, left: 0, width: 100, height: 100 },
+ childrenOrientation
+ )
+ ).toMatchSnapshot();
+ });
+});
diff --git a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts b/packages/design-system/src/components/primitives/dnd/geometry-utils.ts
index 6d8cfdab67..347a192131 100644
--- a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts
+++ b/packages/design-system/src/components/primitives/dnd/geometry-utils.ts
@@ -22,7 +22,7 @@ export type Placement = {
export type Area = "top" | "bottom" | "left" | "right" | "center";
// https://stackoverflow.com/a/18157551/478603
-const getDistanceToRect = (rect: Rect, { x, y }: Point) => {
+export const getDistanceToRect = (rect: Rect, { x, y }: Point) => {
const dx = Math.max(rect.left - x, 0, x - (rect.left + rect.width));
const dy = Math.max(rect.top - y, 0, y - (rect.top + rect.height));
return Math.sqrt(dx * dx + dy * dy);
diff --git a/packages/design-system/src/components/primitives/dnd/index.ts b/packages/design-system/src/components/primitives/dnd/index.ts
index 8cb8570565..b9f03e2785 100644
--- a/packages/design-system/src/components/primitives/dnd/index.ts
+++ b/packages/design-system/src/components/primitives/dnd/index.ts
@@ -1,7 +1,7 @@
-export * from "./use-drag";
-export * from "./use-drop";
-export * from "./use-auto-scroll";
-export * from "./placement-indicator";
-export * from "./use-hold";
-export * from "./use-drag-cursor";
+export { useDrag } from "./use-drag";
+export { useDrop, type DropTarget } from "./use-drop";
+export { useAutoScroll } from "./use-auto-scroll";
+export { PlacementIndicator } from "./placement-indicator";
+export { useHold } from "./use-hold";
+export { useDragCursor } from "./use-drag-cursor";
export { type Rect, type Point, type Placement } from "./geometry-utils";
diff --git a/packages/design-system/src/components/primitives/dnd/use-auto-scroll.test.ts b/packages/design-system/src/components/primitives/dnd/use-auto-scroll.test.ts
new file mode 100644
index 0000000000..839f53ecec
--- /dev/null
+++ b/packages/design-system/src/components/primitives/dnd/use-auto-scroll.test.ts
@@ -0,0 +1,96 @@
+import { getSpeed } from "./use-auto-scroll";
+
+describe("getSpeed", () => {
+ const CONTAINER_START = 0;
+ const CONTAINER_END = 100;
+ const EDGE_DISTANCE_THRESHOLD = 10;
+ const MIN_SPEED = 0;
+ const MAX_SPEED = 10;
+
+ test("speed is never over mod(maxSpeed)", () => {
+ expect(
+ getSpeed(
+ CONTAINER_END + 100,
+ CONTAINER_START,
+ CONTAINER_END,
+ EDGE_DISTANCE_THRESHOLD,
+ MIN_SPEED,
+ MAX_SPEED
+ )
+ ).toBe(MAX_SPEED);
+ expect(
+ getSpeed(
+ CONTAINER_START - 100,
+ CONTAINER_START,
+ CONTAINER_END,
+ EDGE_DISTANCE_THRESHOLD,
+ MIN_SPEED,
+ MAX_SPEED
+ )
+ ).toBe(-MAX_SPEED);
+ });
+
+ test("when at the middle of container, speed is minSpeed", () => {
+ expect(
+ getSpeed(
+ CONTAINER_START + (CONTAINER_END - CONTAINER_START) / 2,
+ CONTAINER_START,
+ CONTAINER_END,
+ EDGE_DISTANCE_THRESHOLD,
+ MIN_SPEED,
+ MAX_SPEED
+ )
+ ).toBe(MIN_SPEED);
+ });
+
+ test("when within the edgeDistanceThreshold bounds, speed is minSpeed", () => {
+ expect(
+ getSpeed(
+ CONTAINER_END - EDGE_DISTANCE_THRESHOLD - 1,
+ CONTAINER_START,
+ CONTAINER_END,
+ EDGE_DISTANCE_THRESHOLD,
+ MIN_SPEED,
+ MAX_SPEED
+ )
+ ).toBe(MIN_SPEED);
+
+ expect(
+ getSpeed(
+ CONTAINER_START + EDGE_DISTANCE_THRESHOLD + 1,
+ CONTAINER_START,
+ CONTAINER_END,
+ EDGE_DISTANCE_THRESHOLD,
+ MIN_SPEED,
+ MAX_SPEED
+ )
+ ).toBe(MIN_SPEED);
+ });
+
+ test.each([0.2, 0.3, 0.9])(
+ "speed is proportional to how close the pointer is to the edge (proportion=%d)",
+ (proportion) => {
+ expect(
+ getSpeed(
+ CONTAINER_END - EDGE_DISTANCE_THRESHOLD * (1 - proportion),
+ CONTAINER_START,
+ CONTAINER_END,
+ EDGE_DISTANCE_THRESHOLD,
+ MIN_SPEED,
+ MAX_SPEED
+ )
+ ).toBe(MIN_SPEED + (MAX_SPEED - MIN_SPEED) * proportion);
+
+ expect(
+ getSpeed(
+ CONTAINER_START + EDGE_DISTANCE_THRESHOLD * (1 - proportion),
+ CONTAINER_START,
+ CONTAINER_END,
+ EDGE_DISTANCE_THRESHOLD,
+ MIN_SPEED,
+ MAX_SPEED
+ )
+ ).toBe((MIN_SPEED + (MAX_SPEED - MIN_SPEED) * proportion) * -1);
+ }
+ );
+});
diff --git a/packages/design-system/src/components/primitives/dnd/use-auto-scroll.ts b/packages/design-system/src/components/primitives/dnd/use-auto-scroll.ts
index 1713cb02d3..ca543c1de3 100644
--- a/packages/design-system/src/components/primitives/dnd/use-auto-scroll.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-auto-scroll.ts
@@ -4,7 +4,7 @@ import { type Point } from "./geometry-utils";
// Time between frames of scroll animation in milliseconds
const FRAME_PERIOD = 30;
-const getSpeed = (
+export const getSpeed = (
pointerPosition: number,
containerStart: number,
containerEnd: number,
@@ -39,7 +39,7 @@ const getSpeed = (
return 0;
};
-export type UseAutoScrollProps = {
+type UseAutoScrollProps = {
edgeDistanceThreshold?: number;
// If set to true the entire document will be scrolled.
@@ -51,7 +51,7 @@ export type UseAutoScrollProps = {
maxSpeed?: number;
};
-export type UseAutoScrollHandlers = {
+type UseAutoScrollHandlers = {
handleMove: (pointerCoordinate: Point) => void;
setEnabled: (enabled: boolean) => void;
targetRef: (element: HTMLElement | null) => void;
diff --git a/packages/design-system/src/components/primitives/dnd/use-drag.ts b/packages/design-system/src/components/primitives/dnd/use-drag.ts
index d15efdec28..e544e23e07 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drag.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drag.ts
@@ -19,7 +19,7 @@ const initialState: State<unknown> = {
pointerId: undefined,
};
-export type UseDragProps<DragItemData> = {
+type UseDragProps<DragItemData> = {
startDistanceThreashold?: number;
shiftDistanceThreshold?: number;
elementToData: (element: Element) => DragItemData | false;
@@ -29,7 +29,7 @@ export type UseDragProps<DragItemData> = {
onEnd: (event: { isCanceled: boolean }) => void;
};
-export type UseDragHandlers = {
+type UseDragHandlers = {
rootRef: (element: HTMLElement | null) => void;
cancelCurrentDrag: () => void;
};
@@ -129,9 +129,9 @@ export const useDrag = <DragItemData>(
};
const end = (isCanceled: boolean) => {
- // A drag is basically a very slow click.
- // But we don't want it to register as a click.
- if (isCanceled === false && state.current.status === "dragging") {
+ if (state.current.status === "dragging") {
+ // A drag is basically a very slow click.
+ // But we don't want it to register as a click.
const addedAt = Date.now();
window.addEventListener(
"click",
@@ -145,12 +145,12 @@ export const useDrag = <DragItemData>(
},
{ capture: true, once: true }
);
+
+ latestProps.current.onEnd({ isCanceled });
}
state.current = { ...(initialState as State<DragItemData>) };
- latestProps.current.onEnd({ isCanceled: isCanceled });
-
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
window.removeEventListener("pointercancel", handlePointerCancel);
diff --git a/packages/design-system/src/components/primitives/dnd/use-drop.ts b/packages/design-system/src/components/primitives/dnd/use-drop.ts
index f0b6345e87..fa9745765f 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drop.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drop.ts
@@ -16,7 +16,7 @@ import { getLocalChildrenOrientation, getChildrenRects } from "./dom-utils";
// Partial information about a drop target
// used during the selection of a new drop target
-export type PartialDropTarget<Data> = {
+type PartialDropTarget<Data> = {
data: Data;
element: Element;
};
@@ -30,7 +30,7 @@ export type DropTarget<Data> = PartialDropTarget<Data> & {
// We pass around data, to avoid extra data lookups.
// For example, data found in elementToData
// doesn't have to be looked up again in swapDropTarget.
-export type UseDropProps<Data> = {
+type UseDropProps<Data> = {
// To check that the element can qualify as a target
elementToData: (target: Element) => Data | false;
@@ -75,7 +75,7 @@ export type UseDropProps<Data> = {
// the pointer always will be at least 2px away from any edge of the root.
const PADDING_WHEN_EMULATING_POINTER_IN_BOUNDS = 2;
-export type UseDropHandlers = {
+type UseDropHandlers = {
handleMove: (pointerCoordinates: Point) => void;
handleScroll: () => void;
handleStart: () => void;
@@ -90,8 +90,9 @@ const getInitialState = <Data>() => {
pointerCoordinates: undefined as Point | undefined,
dropTarget: undefined as DropTarget<Data> | undefined,
childrenRectsCache: new WeakMap<Element, Rect[]>(),
- lastCandidateElement: undefined as Element | undefined,
- lastCandidateArea: undefined as Area | undefined,
+ lastInitialCandidate: undefined as
+ | (PartialDropTarget<Data> & { area: Area })
+ | undefined,
};
};
@@ -219,40 +220,44 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
const { pointerCoordinates } = state.current;
const root = rootRef.current;
+ const withArea = (
+ candidate: PartialDropTarget<Data> | null | undefined
+ ) => {
+ if (candidate == null || pointerCoordinates === undefined) {
+ return undefined;
+ }
+ return {
+ ...candidate,
+ area: getArea(
+ pointerCoordinates,
+ edgeDistanceThreshold,
+ candidate.element.getBoundingClientRect()
+ ),
+ };
+ };
+
// @todo: Cache this?
// Not expensive by itself, but it may call elementToData multiple times.
- let candidate =
+ let candidate = withArea(
root &&
- findClosestDropTarget({
- root,
- initialElement:
- pointerCoordinates &&
- document.elementFromPoint(
- pointerCoordinates.x,
- pointerCoordinates.y
- ),
- elementToData,
- });
+ findClosestDropTarget({
+ root,
+ initialElement:
+ pointerCoordinates &&
+ document.elementFromPoint(
+ pointerCoordinates.x,
+ pointerCoordinates.y
+ ),
+ elementToData,
+ })
+ );
// To avoid calling swapDropTarget unnecessarily on every pointermove
- const isNewCandidate =
- candidate?.element !== state.current.lastCandidateElement;
- state.current.lastCandidateElement = candidate?.element;
- const candidateArea =
- candidate && pointerCoordinates
- ? getArea(
- pointerCoordinates,
- edgeDistanceThreshold,
- candidate.element.getBoundingClientRect()
- )
- : undefined;
- const isNewArea = candidateArea !== state.current.lastCandidateArea;
- state.current.lastCandidateArea = candidateArea;
- if (
- isNewCandidate === false &&
- isNewArea === false &&
- state.current.dropTarget
- ) {
+ const isNewInitialCandidate =
+ candidate?.element !== state.current.lastInitialCandidate?.element ||
+ candidate?.area !== state.current.lastInitialCandidate?.area;
+ state.current.lastInitialCandidate = candidate;
+ if (isNewInitialCandidate === false && state.current.dropTarget) {
// Still need to call setDropTarget to update rect and/or placement.
// Because indexWithinChildren might have changed,
// or parent coordinates might have moved in case of a scroll
@@ -262,21 +267,10 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
let continueSwapping = true;
while (continueSwapping || candidate == null) {
- const swappedTo = swapDropTarget(
- candidate && pointerCoordinates
- ? {
- ...candidate,
- area: getArea(
- pointerCoordinates,
- edgeDistanceThreshold,
- candidate.element.getBoundingClientRect()
- ),
- }
- : undefined
- );
+ const swappedTo = swapDropTarget(candidate);
continueSwapping =
swappedTo.element !== candidate?.element && swappedTo.final !== true;
- candidate = swappedTo;
+ candidate = withArea(swappedTo);
}
setDropTarget(candidate);
@@ -324,7 +318,7 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
handleDomMutation() {
state.current.childrenRectsCache = new WeakMap();
- state.current.lastCandidateElement = undefined;
+ state.current.lastInitialCandidate = undefined;
detectTarget();
},
};
diff --git a/packages/design-system/src/components/primitives/dnd/use-hold.ts b/packages/design-system/src/components/primitives/dnd/use-hold.ts
index e1e3acb530..5097baf4d2 100644
--- a/packages/design-system/src/components/primitives/dnd/use-hold.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-hold.ts
@@ -1,6 +1,6 @@
import { useMemo, useRef } from "react";
-export type UseHoldProps<Data> = {
+type UseHoldProps<Data> = {
isEqual: (a: Data, b: Data) => boolean;
holdTimeThreshold: number;
onHold: (data: Data) => void;