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.

cleanup a bit

Oleg Isonen6c4b416fd46d
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx
index 2e59cc2e58..a905e6d918 100644
--- a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.stories.tsx
@@ -1,9 +1,7 @@
-import { getEnvelopeEndpointWithUrlEncodedAuth } from "@sentry/core";
import { ComponentStory, ComponentMeta } from "@storybook/react";
import { useState } from "react";
-import { DropTarget } from "react-dnd";
import { Box } from "../../box";
-import { useDrag, useDropTargetRect, Area } from "./handlers";
+import { useDrag, useDropTarget, Area } from "./handlers";
type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;
@@ -11,7 +9,7 @@ export const Playground = () => {
const [dropTargetRect, setDropTargetRect] = useState<Rect>();
const [placementIndicatorRect, setPlacementIndicatorRect] = useState<Rect>();
- const { ref, handleMove } = useDropTargetRect({
+ const { rootRef, handleMove } = useDropTarget({
isDropTarget(element: HTMLElement) {
return element.dataset.draggable === "true";
},
@@ -37,7 +35,7 @@ export const Playground = () => {
});
return (
- <div {...dragProps} ref={ref}>
+ <div {...dragProps} ref={rootRef}>
<Item background="$cyanA9" />
<Item background="$slateA9" />
<Item background="$blueA9" draggable={false}>
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.ts
index 1c7c42c84f..0c12962b19 100644
--- a/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.ts
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/handlers.ts
@@ -104,7 +104,22 @@ const elementFromPoint = (coordinate: Coordinate): HTMLElement | undefined => {
if (element instanceof HTMLElement) return element;
};
-export const useDropTargetRect = ({
+const getArea = (
+ pointerCoordinate: Coordinate,
+ edgeThreshold: number,
+ targetRect?: DOMRect
+) => {
+ let area: Area = "center";
+ if (targetRect === undefined) return area;
+ // We are at the edge and this means user wants to insert after that element into its parent
+ if (pointerCoordinate.y - targetRect.top <= edgeThreshold) area = "top";
+ if (targetRect.bottom - pointerCoordinate.y <= edgeThreshold) area = "bottom";
+ if (pointerCoordinate.x - targetRect.left <= edgeThreshold) area = "left";
+ if (targetRect.right - pointerCoordinate.x <= edgeThreshold) area = "right";
+ return area;
+};
+
+export const useDropTarget = ({
isDropTarget,
onDropTargetChange,
edgeThreshold = 3,
@@ -128,30 +143,22 @@ export const useDropTargetRect = ({
});
const hasTargetChanged = nextTarget !== targetRef.current;
-
- let nextArea: Area = "center";
+ targetRef.current = nextTarget;
if (hasTargetChanged) {
targetRectRef.current = nextTarget.getBoundingClientRect();
}
- // We are at the edge and this means user wants to insert after that element into its parent
- if (targetRectRef.current !== undefined) {
- if (pointerCoordinate.y - targetRectRef.current.top <= edgeThreshold)
- nextArea = "top";
- if (targetRectRef.current.bottom - pointerCoordinate.y <= edgeThreshold)
- nextArea = "bottom";
- if (pointerCoordinate.x - targetRectRef.current.left <= edgeThreshold)
- nextArea = "left";
- if (targetRectRef.current.right - pointerCoordinate.x <= edgeThreshold)
- nextArea = "right";
- }
+ const nextArea = getArea(
+ pointerCoordinate,
+ edgeThreshold,
+ targetRectRef.current
+ );
const hasAreaChanged = nextArea !== areaRef.current;
+ areaRef.current = nextArea;
if (hasTargetChanged || hasAreaChanged) {
- targetRef.current = nextTarget;
- areaRef.current = nextArea;
onDropTargetChange({
rect: targetRectRef.current,
area: nextArea,
@@ -159,7 +166,7 @@ export const useDropTargetRect = ({
}
},
- ref(rootElement: HTMLElement | null) {
+ rootRef(rootElement: HTMLElement | null) {
rootRef.current = rootElement;
},
};