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.

reusable Point and Rect types

Roman Pominov8fa38d10c8fa
diff --git a/apps/designer/app/canvas/shared/use-drag-drop.ts b/apps/designer/app/canvas/shared/use-drag-drop.ts
index 599a739ea4..94132ee664 100644
--- a/apps/designer/app/canvas/shared/use-drag-drop.ts
+++ b/apps/designer/app/canvas/shared/use-drag-drop.ts
@@ -6,6 +6,7 @@ import {
import { findInstanceById, getInstancePath } from "~/shared/tree-utils";
import {
type DropTarget,
+ type Point,
useAutoScroll,
useDrag,
useDrop,
@@ -32,7 +33,7 @@ export type DragStartPayload = {
export type DragEndPayload = { origin: "panel" | "canvas" };
-export type DragMovePayload = { canvasCoordinates: { x: number; y: number } };
+export type DragMovePayload = { canvasCoordinates: Point };
const initialState = {
dropTarget: undefined as DropTarget<Instance> | undefined,
diff --git a/apps/designer/app/canvas/shared/use-pointer-outline.ts b/apps/designer/app/canvas/shared/use-pointer-outline.ts
index 038f7c1456..6b21db9b54 100644
--- a/apps/designer/app/canvas/shared/use-pointer-outline.ts
+++ b/apps/designer/app/canvas/shared/use-pointer-outline.ts
@@ -1,4 +1,5 @@
import { useEffect, useRef } from "react";
+import { type Point } from "@webstudio-is/design-system";
// Draw a point where we think the pointer is to visualize if calculations are based on the right position
// Only needed for debugging.
@@ -20,7 +21,7 @@ export const usePointerOutline = () => {
};
}, []);
- return (offset: { x: number; y: number }) => {
+ return (offset: Point) => {
if (ref.current === undefined) return;
ref.current.style.left = `${offset.x}px`;
ref.current.style.top = `${offset.y}px`;
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/components/components.tsx b/apps/designer/app/designer/features/sidebar-left/panels/components/components.tsx
index 27fbdee2a7..351a6d11c3 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/components/components.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/components/components.tsx
@@ -6,7 +6,7 @@ import {
components,
} from "@webstudio-is/react-sdk";
import { Flex } from "@webstudio-is/design-system";
-import { useDrag } from "@webstudio-is/design-system";
+import { useDrag, type Point } from "@webstudio-is/design-system";
import { PlusIcon } from "@webstudio-is/icons";
import { createInstance } from "~/shared/tree-utils";
import type { TabName } from "../../types";
@@ -42,7 +42,7 @@ const DragLayer = ({
point,
}: {
component: Instance["component"];
- point: { x: number; y: number };
+ point: Point;
}) => {
return createPortal(
<Flex
@@ -87,12 +87,12 @@ const elementToComponentName = (element: Element) => {
export const TabContent = ({ publish, onSetActiveTab }: TabContentProps) => {
const [dragComponent, setDragComponent] = useState<Instance["component"]>();
- const [point, setPoint] = useState<{ x: number; y: number }>({ x: 0, y: 0 });
+ const [point, setPoint] = useState<Point>({ x: 0, y: 0 });
const [canvasRect] = useCanvasRect();
const [zoom] = useZoom();
- const toCanvasCoordinates = ({ x, y }: { x: number; y: number }) => {
+ const toCanvasCoordinates = ({ x, y }: Point) => {
if (canvasRect === undefined) {
return { x: 0, y: 0 };
}
diff --git a/apps/designer/app/designer/features/workspace/canvas-tools/outline/label.tsx b/apps/designer/app/designer/features/workspace/canvas-tools/outline/label.tsx
index 52ed80a6db..ac8696fbf3 100644
--- a/apps/designer/app/designer/features/workspace/canvas-tools/outline/label.tsx
+++ b/apps/designer/app/designer/features/workspace/canvas-tools/outline/label.tsx
@@ -1,8 +1,7 @@
import { useCallback, useState } from "react";
-import { styled } from "@webstudio-is/design-system";
+import { styled, type Rect } from "@webstudio-is/design-system";
import { type Instance, components } from "@webstudio-is/react-sdk";
-type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;
type LabelPosition = "top" | "inside" | "bottom";
type LabelRefCallback = (element: HTMLElement | null) => void;
diff --git a/apps/designer/app/designer/features/workspace/canvas-tools/outline/outline.tsx b/apps/designer/app/designer/features/workspace/canvas-tools/outline/outline.tsx
index 090028848b..7f1e0b0524 100644
--- a/apps/designer/app/designer/features/workspace/canvas-tools/outline/outline.tsx
+++ b/apps/designer/app/designer/features/workspace/canvas-tools/outline/outline.tsx
@@ -1,7 +1,5 @@
import { useMemo } from "react";
-import { styled } from "@webstudio-is/design-system";
-
-type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;
+import { styled, type Rect } from "@webstudio-is/design-system";
const OutlineContainer = styled("div", {
position: "absolute",
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 9a0bf72e72..0fc660116d 100644
--- a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts
+++ b/packages/design-system/src/components/primitives/dnd/geometry-utils.ts
@@ -1,5 +1,7 @@
export type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;
+export type Point = { x: number; y: number };
+
export type ChildrenOrientation =
| { type: "horizontal" | "vertical"; reverse: boolean }
| { type: "mixed"; reverse?: boolean };
@@ -12,16 +14,13 @@ export type Placement = {
};
// https://stackoverflow.com/a/18157551/478603
-const getDistanceToRect = (rect: Rect, { x, y }: { x: number; y: number }) => {
+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);
};
-export const getClosestRectIndex = (
- rects: Rect[],
- point: { x: number; y: number }
-) => {
+export const getClosestRectIndex = (rects: Rect[], point: Point) => {
if (rects.length === 0) {
return -1;
}
@@ -42,7 +41,7 @@ export const isEqualRect = (a: Rect | undefined, b: Rect) =>
a.height === b.height;
export const isNearEdge = (
- { x, y }: { x: number; y: number },
+ { x, y }: Point,
edgeDistanceThreshold: number,
rect: Rect
) =>
@@ -266,7 +265,7 @@ export const getRectsOrientation = (
// Determines whether we should place the item before or after the closest child.
// Returns the number that should be added to the closest child index to get the final index.
export const getIndexAdjustment = (
- pointer: { x: number; y: number },
+ pointer: Point,
closestChildRect: Rect | undefined,
{ type: orientationType, reverse }: ChildrenOrientation
) => {
diff --git a/packages/design-system/src/components/primitives/dnd/index.ts b/packages/design-system/src/components/primitives/dnd/index.ts
index e567a425b6..c519d8370e 100644
--- a/packages/design-system/src/components/primitives/dnd/index.ts
+++ b/packages/design-system/src/components/primitives/dnd/index.ts
@@ -2,3 +2,4 @@ export * from "./use-drag";
export * from "./use-drop";
export * from "./use-auto-scroll";
export * from "./placement-indicator";
+export { type Rect, type Point } from "./geometry-utils";
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 e325884871..1713cb02d3 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
@@ -1,4 +1,5 @@
import { useRef, useMemo } from "react";
+import { type Point } from "./geometry-utils";
// Time between frames of scroll animation in milliseconds
const FRAME_PERIOD = 30;
@@ -51,7 +52,7 @@ export type UseAutoScrollProps = {
};
export type UseAutoScrollHandlers = {
- handleMove: (pointerCoordinate: { x: number; y: number }) => void;
+ 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 b7120609fa..4c38a71e7e 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drag.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drag.ts
@@ -1,5 +1,6 @@
import { useMove } from "./use-move";
import { useRef, useEffect, useMemo } from "react";
+import { type Point } from "./geometry-utils";
type State =
| {
@@ -23,7 +24,7 @@ export type UseDragProps<DragItemData> = {
shiftDistanceThreshold?: number;
isDragItem: (element: Element) => DragItemData | false;
onStart: (event: { target: HTMLElement; data: DragItemData }) => void;
- onMove: (event: { x: number; y: number }) => void;
+ onMove: (event: Point) => void;
onShiftChange?: (event: { shifts: number }) => void;
onEnd: () => void;
};
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 c6aeefc500..dc659ba27e 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drop.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drop.ts
@@ -8,6 +8,7 @@ import {
getPlacementNextTo,
getIndexAdjustment,
type Rect,
+ type Point,
type Placement,
} from "./geometry-utils";
import { getLocalChildrenOrientation, getChildrenRects } from "./dom-utils";
@@ -47,7 +48,7 @@ export type UseDropProps<Data> = {
};
export type UseDropHandlers = {
- handleMove: (pointerCoordinates: { x: number; y: number }) => void;
+ handleMove: (pointerCoordinates: Point) => void;
handleScroll: () => void;
handleEnd: () => void;
rootRef: (target: Element | null) => void;
@@ -55,7 +56,7 @@ export type UseDropHandlers = {
const getInitialState = <Data>() => {
return {
- pointerCoordinates: undefined as { x: number; y: number } | undefined,
+ pointerCoordinates: undefined as Point | undefined,
dropTarget: undefined as DropTarget<Data> | undefined,
childrenRectsCache: new WeakMap<Element, Rect[]>(),
lastCandidateElement: undefined as Element | undefined,