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

Roman Pominove109513953cd
diff --git a/packages/design-system/src/components/primitives/dnd/use-drop.test.ts b/packages/design-system/src/components/primitives/dnd/geometry-utils.test.ts
similarity index 96%
rename from packages/design-system/src/components/primitives/dnd/use-drop.test.ts
rename to packages/design-system/src/components/primitives/dnd/geometry-utils.test.ts
index 47212115e9..ce142f43a0 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drop.test.ts
+++ b/packages/design-system/src/components/primitives/dnd/geometry-utils.test.ts
@@ -1,4 +1,4 @@
-import { getPlacementBetween } from "./use-drop";
+import { getPlacementBetween } from "./geometry-utils";
const size = { width: 100, height: 100 };
diff --git a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts b/packages/design-system/src/components/primitives/dnd/geometry-utils.ts
new file mode 100644
index 0000000000..b0a3f05a7b
--- /dev/null
+++ b/packages/design-system/src/components/primitives/dnd/geometry-utils.ts
@@ -0,0 +1,240 @@
+export type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;
+
+export type ChildrenOrientation = "vertical" | "horizontal" | "mixed";
+
+// https://stackoverflow.com/a/18157551/478603
+const getDistanceToRect = (rect: Rect, { x, y }: { x: number; y: number }) => {
+ 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 }
+) => {
+ if (rects.length === 0) {
+ return -1;
+ }
+ const sorted = rects
+ .map((rect, index) => ({
+ index,
+ distance: getDistanceToRect(rect, point),
+ }))
+ .sort((a, b) => a.distance - b.distance);
+ return sorted[0].index;
+};
+
+export const isEqualRect = (a: Rect | undefined, b: Rect) =>
+ a !== undefined &&
+ a.top === b.top &&
+ a.left === b.left &&
+ a.width === b.width &&
+ a.height === b.height;
+
+export const isNearEdge = (
+ { x, y }: { x: number; y: number },
+ edgeDistanceThreshold: number,
+ rect: Rect
+) =>
+ Math.min(
+ y - rect.top,
+ rect.top + rect.height - y,
+ x - rect.left,
+ rect.left + rect.width - x
+ ) <= edgeDistanceThreshold;
+
+export type Placement = {
+ x: number;
+ y: number;
+ length: number;
+ direction: "horizontal" | "vertical";
+};
+
+export const getPlacementBetween = (
+ a: Rect | undefined,
+ b: Rect | undefined
+): Placement | undefined => {
+ if (a === undefined || b === undefined) {
+ return undefined;
+ }
+
+ const [firstY, secondY] = a.top < b.top ? [a, b] : [b, a];
+ const [firstX, secondX] = a.left < b.left ? [a, b] : [b, a];
+ const distanceY = secondY.top - firstY.top - firstY.height;
+ const distanceX = secondX.left - firstX.left - firstX.width;
+
+ // if rects overlap we don't want to put placement between them
+ if (distanceX < 0 && distanceY < 0) {
+ return undefined;
+ }
+
+ if (distanceX < 0 || (distanceY >= 0 && distanceY < distanceX)) {
+ const minX = Math.min(a.left, b.left);
+ const maxX = Math.max(a.left + a.width, b.left + b.width);
+ return {
+ y: firstY.top + firstY.height + distanceY / 2,
+ x: minX,
+ length: maxX - minX,
+ direction: "horizontal",
+ };
+ }
+
+ const minY = Math.min(a.top, b.top);
+ const maxY = Math.max(a.top + a.height, b.top + b.height);
+ return {
+ y: minY,
+ x: firstX.left + firstX.width + distanceX / 2,
+ length: maxY - minY,
+ direction: "vertical",
+ };
+};
+
+export const getPlacementNextTo = (
+ parentRect: Rect,
+ rect: Rect | undefined,
+ side: "top" | "bottom" | "left" | "right"
+): Placement | undefined => {
+ if (rect === undefined) {
+ return undefined;
+ }
+
+ const margin = 5;
+
+ if (side === "top") {
+ return {
+ x: rect.left,
+ y: Math.min(rect.top, Math.max(0, rect.top - margin)),
+ length: rect.width,
+ direction: "horizontal",
+ };
+ }
+
+ if (side === "bottom") {
+ return {
+ x: rect.left,
+ y: Math.max(
+ rect.top + rect.height,
+ Math.min(parentRect.height, rect.top + margin)
+ ),
+ length: rect.width,
+ direction: "horizontal",
+ };
+ }
+
+ if (side === "left") {
+ return {
+ x: Math.min(rect.left, Math.max(0, rect.left - margin)),
+ y: rect.top,
+ length: rect.height,
+ direction: "vertical",
+ };
+ }
+
+ return {
+ x: Math.max(
+ rect.left + rect.width,
+ Math.min(parentRect.width, rect.left + margin)
+ ),
+ y: rect.top,
+ length: rect.height,
+ direction: "vertical",
+ };
+};
+
+export const getPlacementInside = (
+ parentRect: Rect,
+ childrenOrientation: ChildrenOrientation
+): Placement => {
+ const padding = 5;
+
+ if (childrenOrientation === "horizontal") {
+ const safePadding = Math.min(parentRect.width / 2, padding);
+ return {
+ y: parentRect.top + safePadding,
+ x: parentRect.left + safePadding,
+ length: parentRect.height - safePadding * 2,
+ direction: "vertical",
+ };
+ }
+
+ const safePadding = Math.min(parentRect.height / 2, padding);
+ return {
+ y: parentRect.top + safePadding,
+ x: parentRect.left + safePadding,
+ length: parentRect.width - safePadding * 2,
+ direction: "horizontal",
+ };
+};
+
+export const getRectsOrientation = (
+ first: Rect | undefined,
+ second: Rect,
+ third: Rect | undefined
+): ChildrenOrientation => {
+ // @todo: We need a slightly more sofisticated way to determine orientation.
+ // For example, detect this as "vertical":
+ // [ ]
+ // [ ]
+
+ // @todo: add "vertical-reversed", "horizontal-reversed", "mixed-reversed"
+
+ if (
+ (first === undefined || first.top === second.top) &&
+ (third === undefined || third.top === second.top)
+ ) {
+ return "horizontal";
+ }
+
+ if (
+ (first === undefined || first.left === second.left) &&
+ (third === undefined || third.left === second.left)
+ ) {
+ return "vertical";
+ }
+
+ return "mixed";
+};
+
+// Checks whether we should place 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 },
+ closestChildRect: Rect | undefined,
+ childrenOrientation: ChildrenOrientation
+) => {
+ if (closestChildRect === undefined) {
+ return 0;
+ }
+
+ const { top, left, width, height } = closestChildRect;
+
+ if (childrenOrientation === "vertical") {
+ const middleY = top + height / 2;
+ return pointer.y < middleY ? 0 : 1;
+ }
+
+ if (childrenOrientation === "horizontal") {
+ const middleX = left + width / 2;
+ return pointer.x < middleX ? 0 : 1;
+ }
+
+ // For the "mixed" orientation,
+ // we are looking at whether the pointer is above or below the diagonal
+
+ // diagonal equation
+ const getDiagonalY = (diagonalX: number) => {
+ if (width === 0) {
+ return undefined;
+ }
+ const slope = height / width;
+ const topRightCorner = { x: left + width, y: top };
+ return slope * (topRightCorner.x - diagonalX) + topRightCorner.y;
+ };
+
+ const diagonalY = getDiagonalY(pointer.x);
+ if (diagonalY === undefined) {
+ return 0;
+ }
+ return pointer.y < diagonalY ? 0 : 1;
+};
diff --git a/packages/design-system/src/components/primitives/dnd/placement.tsx b/packages/design-system/src/components/primitives/dnd/placement.tsx
index 29856db034..bdbd568e96 100644
--- a/packages/design-system/src/components/primitives/dnd/placement.tsx
+++ b/packages/design-system/src/components/primitives/dnd/placement.tsx
@@ -1,6 +1,6 @@
import { useRef, useMemo } from "react";
import { Box } from "../../box";
-import { type Rect, getClosestRectIndex, isEqualRect } from "./rect";
+import { type Rect, getClosestRectIndex, isEqualRect } from "./geometry-utils";
let probeCache: undefined | HTMLElement;
const getProbe = () => {
diff --git a/packages/design-system/src/components/primitives/dnd/rect.ts b/packages/design-system/src/components/primitives/dnd/rect.ts
deleted file mode 100644
index 2bb90f3679..0000000000
--- a/packages/design-system/src/components/primitives/dnd/rect.ts
+++ /dev/null
@@ -1,55 +0,0 @@
-export type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;
-
-// https://stackoverflow.com/a/18157551/478603
-const getDistanceToRect = (rect: Rect, { x, y }: { x: number; y: number }) => {
- 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 }
-) => {
- if (rects.length === 0) {
- return -1;
- }
- const sorted = rects
- .map((rect, index) => ({
- index,
- distance: getDistanceToRect(rect, point),
- }))
- .sort((a, b) => a.distance - b.distance);
- return sorted[0].index;
-};
-
-export const isEqualRect = (a: Rect | undefined, b: Rect) =>
- a !== undefined &&
- a.top === b.top &&
- a.left === b.left &&
- a.width === b.width &&
- a.height === b.height;
-
-export const isNearEdge = (
- { x, y }: { x: number; y: number },
- edgeDistanceThreshold: number,
- rect: Rect
-) =>
- Math.min(
- y - rect.top,
- rect.top + rect.height - y,
- x - rect.left,
- rect.left + rect.width - x
- ) <= edgeDistanceThreshold;
-
-export const rectRelativeToRect = (relativeTo: Rect, rect: Rect) => ({
- top: rect.top - relativeTo.top,
- left: rect.left - relativeTo.left,
- width: rect.width,
- height: rect.height,
-});
-
-export const pointRelativeToRect = (
- relativeTo: Rect,
- point: { x: number; y: number }
-) => ({ x: point.x - relativeTo.left, y: point.y - relativeTo.top });
diff --git a/packages/design-system/src/components/primitives/dnd/sortable-list.stories.tsx b/packages/design-system/src/components/primitives/dnd/sortable-list.stories.tsx
index fb3630aec0..2a7a180815 100644
--- a/packages/design-system/src/components/primitives/dnd/sortable-list.stories.tsx
+++ b/packages/design-system/src/components/primitives/dnd/sortable-list.stories.tsx
@@ -4,7 +4,7 @@ import { Box } from "../../box";
import { styled } from "../../../stitches.config";
import { useDropTarget } from "./use-drop-target";
import { useDrag } from "./use-drag";
-import { type Rect } from "./rect";
+import { type Rect } from "./geometry-utils";
import { usePlacement, PlacementIndicator } from "./placement";
import { useAutoScroll } from "./use-auto-scroll";
diff --git a/packages/design-system/src/components/primitives/dnd/use-drop-target.ts b/packages/design-system/src/components/primitives/dnd/use-drop-target.ts
index 937646a226..a4d4511a1e 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drop-target.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drop-target.ts
@@ -1,5 +1,5 @@
import { useRef, useMemo } from "react";
-import { isEqualRect } from "./rect";
+import { isEqualRect } from "./geometry-utils";
export type Area = "top" | "right" | "bottom" | "left" | "center";
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 8dedef0fb3..766afc6122 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drop.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drop.ts
@@ -2,11 +2,16 @@ import { useRef, useMemo } from "react";
import {
isEqualRect,
isNearEdge,
- rectRelativeToRect,
- pointRelativeToRect,
getClosestRectIndex,
+ getPlacementBetween,
+ getPlacementInside,
+ getPlacementNextTo,
+ getRectsOrientation,
+ getIndexAdjustment,
type Rect,
-} from "./rect";
+ type ChildrenOrientation,
+ type Placement,
+} from "./geometry-utils";
// @todo: use this in useDrag as well
type UsableElement = HTMLElement | SVGElement;
@@ -18,18 +23,8 @@ const toUseableElement = (
}
};
-// let probeCache: undefined | HTMLElement;
-// const getProbe = () => {
-// if (probeCache === undefined) {
-// probeCache = document.createElement("div");
-// }
-// return probeCache;
-// };
-
-// @todo: add "vertical-reversed", "horizontal-reversed", "mixed-reversed"
-type ChildrenOrientation = "vertical" | "horizontal" | "mixed";
-
-// @todo: describe this function
+// By looking at a specific child and it's neighbours,
+// determines their orientation relative to each other
const getLocalChildrenOrientation = (
parent: UsableElement,
childrentRects: Rect[],
@@ -40,186 +35,31 @@ const getLocalChildrenOrientation = (
const next = childrentRects[childIndex + 1] as Rect | undefined;
if (current === undefined || (next === undefined && previous === undefined)) {
- // @todo: use probe
- return "vertical";
- }
-
- if (
- (next === undefined || next.top === current.top) &&
- (previous === undefined || previous.top === current.top)
- ) {
- return "horizontal";
- }
-
- if (
- (next === undefined || next.left === current.left) &&
- (previous === undefined || previous.left === current.left)
- ) {
- return "vertical";
- }
-
- return "mixed";
-};
-
-const getIndexAdjustment = (
- pointer: { x: number; y: number },
- closestChildRect: Rect,
- childrenOrientation: ChildrenOrientation
-) => {
- const { top, left, width, height } = closestChildRect;
-
- if (childrenOrientation === "vertical") {
- const middleY = top + height / 2;
- return pointer.y < middleY ? 0 : 1;
- }
-
- if (childrenOrientation === "horizontal") {
- const middleX = left + width / 2;
- return pointer.x < middleX ? 0 : 1;
- }
-
- // For the "mixed" orientation,
- // we are looking at whether the pointer is above or below the diagonal
-
- // diagonal equation
- const getDiagonalY = (diagonalX: number) => {
- if (width === 0) {
- return undefined;
+ const probe = document.createElement("div");
+ const { children } = parent;
+ if (childIndex > children.length - 1) {
+ parent.appendChild(probe);
+ } else {
+ parent.insertBefore(probe, children[childIndex]);
}
- const slope = height / width;
- const topRightCorner = { x: left + width, y: top };
- return slope * (diagonalX - topRightCorner.x) + topRightCorner.y;
- };
+ const probeRect = probe.getBoundingClientRect();
+ parent.removeChild(probe);
- const diagonalY = getDiagonalY(pointer.x);
- if (diagonalY === undefined) {
- return 0;
+ return probeRect.width === 0 && probeRect.height !== 0
+ ? "horizontal"
+ : "vertical";
}
- return pointer.y < diagonalY ? 0 : 1;
+
+ return getRectsOrientation(previous, current, next);
};
// Partial information about a drop target
// used during the selection of a new drop target
-type PartialDropTarget<Data> = {
+export type PartialDropTarget<Data> = {
data: Data;
element: UsableElement;
};
-type Placement = {
- x: number;
- y: number;
- length: number;
- direction: "horizontal" | "vertical";
-};
-
-export const getPlacementBetween = (
- a: Rect,
- b: Rect
-): Placement | undefined => {
- const [firstY, secondY] = a.top < b.top ? [a, b] : [b, a];
- const [firstX, secondX] = a.left < b.left ? [a, b] : [b, a];
- const distanceY = secondY.top - firstY.top - firstY.height;
- const distanceX = secondX.left - firstX.left - firstX.width;
-
- // if rects overlap we don't want to put placement between them
- if (distanceX < 0 && distanceY < 0) {
- return undefined;
- }
-
- if (distanceX < 0 || (distanceY >= 0 && distanceY < distanceX)) {
- const minX = Math.min(a.left, b.left);
- const maxX = Math.max(a.left + a.width, b.left + b.width);
- return {
- y: firstY.top + firstY.height + distanceY / 2,
- x: minX,
- length: maxX - minX,
- direction: "horizontal",
- };
- }
-
- const minY = Math.min(a.top, b.top);
- const maxY = Math.max(a.top + a.height, b.top + b.height);
- return {
- y: minY,
- x: firstX.left + firstX.width + distanceX / 2,
- length: maxY - minY,
- direction: "vertical",
- };
-};
-
-export const getPlacementNextTo = (
- parentRect: Rect,
- rect: Rect,
- side: "top" | "bottom" | "left" | "right"
-): Placement => {
- const margin = 5;
-
- if (side === "top") {
- return {
- x: rect.left,
- y: Math.min(rect.top, Math.max(parentRect.top, rect.top - margin)),
- length: rect.width,
- direction: "horizontal",
- };
- }
-
- if (side === "bottom") {
- return {
- x: rect.left,
- y: Math.max(
- rect.top + rect.height,
- Math.min(parentRect.top + parentRect.height, rect.top + margin)
- ),
- length: rect.width,
- direction: "horizontal",
- };
- }
-
- if (side === "left") {
- return {
- x: Math.min(rect.left, Math.max(parentRect.left, rect.left - margin)),
- y: rect.top,
- length: rect.height,
- direction: "vertical",
- };
- }
-
- return {
- x: Math.max(
- rect.left + rect.width,
- Math.min(parentRect.left + parentRect.width, rect.left + margin)
- ),
- y: rect.top,
- length: rect.height,
- direction: "vertical",
- };
-};
-
-export const getPlacementInside = (
- parentRect: Rect,
- childrenOrientation: ChildrenOrientation
-): Placement => {
- const padding = 5;
-
- if (childrenOrientation === "horizontal") {
- const safePadding = Math.min(parentRect.width / 2, padding);
- return {
- y: parentRect.top + safePadding,
- x: parentRect.left + safePadding,
- length: parentRect.height - safePadding * 2,
- direction: "vertical",
- };
- }
-
- const safePadding = Math.min(parentRect.height / 2, padding);
- return {
- y: parentRect.top + safePadding,
- x: parentRect.left + safePadding,
- length: parentRect.width - safePadding * 2,
- direction: "horizontal",
- };
-};
-
export type DropTarget<Data> = PartialDropTarget<Data> & {
rect: DOMRect;
indexWithinChildren: number;
@@ -233,12 +73,14 @@ export type UseDropProps<Data> = {
// To check that the element can qualify as a target
isDropTarget: (target: UsableElement) => Data | false;
+ // A target to use when no suitable element is found under the pointer,
+ // or swapDropTarget returns "DEFAULT"
getDefaultDropTarget: () => PartialDropTarget<Data>;
- // Distance from an edge for pointer to be considered on an edge.
+ // Distance from an edge to set nearEdge to true in swapDropTarget
edgeDistanceThreshold?: number;
- // Given the potential target that has passed isDropTarget check,
+ // Given the potential target that has passed the isDropTarget check,
// and the position of the pointer on the target,
// you can swap to another target
swapDropTarget?: (
@@ -261,6 +103,8 @@ const getInitialState = <Data>() => {
pointerCoordinates: undefined as { x: number; y: number } | undefined,
dropTarget: undefined as DropTarget<Data> | undefined,
childrenRectsCache: new WeakMap<UsableElement, Rect[]>(),
+ lastCandidateElement: undefined as UsableElement | undefined,
+ lastCandidateIsNearEdge: undefined as boolean | undefined,
};
};
@@ -271,7 +115,7 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
const latestProps = useRef<UseDropProps<Data>>(props);
latestProps.current = props;
- const state = useRef(getInitialState());
+ const state = useRef(getInitialState<Data>());
// We want to return a stable object to avoid re-renders when it's a dependency
return useMemo(() => {
@@ -281,119 +125,106 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
return fromCache;
}
+ // We convert to relative coordinates to be able to store the result in cache.
+ // Otherwise we would have to clear cache on scroll.
+ const toRelativeCoordinates = (rect: Rect) => ({
+ left: rect.left - parentRect.left,
+ top: rect.top - parentRect.top,
+ width: rect.width,
+ height: rect.height,
+ });
+
const result = Array.from(parent.children).map((child) =>
- rectRelativeToRect(parentRect, child.getBoundingClientRect())
+ toRelativeCoordinates(child.getBoundingClientRect())
);
state.current.childrenRectsCache.set(parent, result);
return result;
};
- const getIndex = (
- parent: UsableElement,
- parentRect: Rect,
- pointer: { x: number; y: number }
+ const setDropTarget = (partialDropTarget: PartialDropTarget<Data>) => {
+ const { pointerCoordinates } = state.current;
+ if (pointerCoordinates === undefined) {
+ return;
+ }
- // @todo: don't return array here like that
- ): [number, ChildrenOrientation, number, number] => {
- const pointerRelativeToParent = pointRelativeToRect(parentRect, pointer);
- const childrenRects = getChildrenRects(parent, parentRect);
+ const parentRect = partialDropTarget.element.getBoundingClientRect();
- if (childrenRects.length === 0) {
- // @todo: still call getLocalChildrenOrientation
- return [0, "vertical", -1, 0];
- }
+ const pointerRelativeToParent = {
+ x: pointerCoordinates.x - parentRect.left,
+ y: pointerCoordinates.y - parentRect.top,
+ };
- const closestChildIndex = getClosestRectIndex(
- childrenRects,
- pointerRelativeToParent
+ const childrenRects = getChildrenRects(
+ partialDropTarget.element,
+ parentRect
);
- const orientation = getLocalChildrenOrientation(
- parent,
+ const closestChildIndex =
+ childrenRects.length === 0
+ ? 0
+ : getClosestRectIndex(childrenRects, pointerRelativeToParent);
+ const closestChildRect = childrenRects[closestChildIndex] as
+ | Rect
+ | undefined;
+
+ const childrenOrientation = getLocalChildrenOrientation(
+ partialDropTarget.element,
childrenRects,
closestChildIndex
);
- const adjustment = getIndexAdjustment(
+ const indexAdjustment = getIndexAdjustment(
pointerRelativeToParent,
- childrenRects[closestChildIndex],
- orientation
+ closestChildRect,
+ childrenOrientation
);
- return [
- closestChildIndex + adjustment,
- orientation,
- closestChildIndex,
- adjustment === 0 ? closestChildIndex - 1 : closestChildIndex + 1,
- ];
- };
-
- const handleDropTargetChange = (
- partialDropTarget: PartialDropTarget<Data>
- ) => {
- const { pointerCoordinates } = state.current;
- if (pointerCoordinates === undefined) {
- return;
- }
-
- const rect = partialDropTarget.element.getBoundingClientRect();
-
- const [indexWithinChildren, childrenOrientation, aIndex, bIndex] =
- getIndex(partialDropTarget.element, rect, pointerCoordinates);
+ const indexWithinChildren = closestChildIndex + indexAdjustment;
const current = state.current.dropTarget;
if (
current === undefined ||
current.element !== partialDropTarget.element ||
current.indexWithinChildren !== indexWithinChildren ||
- !isEqualRect(current.rect, rect)
+ !isEqualRect(current.rect, parentRect)
) {
- const childrenRects = getChildrenRects(partialDropTarget.element, rect);
- const aRect = childrenRects[aIndex] as Rect | undefined;
- const bRect = childrenRects[bIndex] as Rect | undefined;
-
- let placement: Placement | undefined;
-
- const toAbsoluteCoordinates = (
- placement: Placement | undefined
- ): Placement | undefined => {
- if (placement === undefined) {
- return;
- }
- return {
+ const side =
+ childrenOrientation === "horizontal"
+ ? indexAdjustment > 0
+ ? "right"
+ : "left"
+ : indexAdjustment > 0
+ ? "bottom"
+ : "top";
+
+ const toGlobalCoordinates = (placement: Placement | undefined) =>
+ placement && {
...placement,
- x: placement.x + rect.left,
- y: placement.y + rect.top,
+ x: placement.x + parentRect.left,
+ y: placement.y + parentRect.top,
};
- };
- if (aRect !== undefined && bRect !== undefined) {
- placement = toAbsoluteCoordinates(getPlacementBetween(aRect, bRect));
- }
+ const neighbourRect = childrenRects[
+ indexAdjustment === 0 ? closestChildIndex - 1 : closestChildIndex + 1
+ ] as Rect | undefined;
- if (placement === undefined && aRect !== undefined) {
- placement = toAbsoluteCoordinates(
- getPlacementNextTo(
- rectRelativeToRect(rect, rect),
- aRect,
- childrenOrientation === "horizontal"
- ? bIndex > aIndex
- ? "right"
- : "left"
- : bIndex > aIndex
- ? "bottom"
- : "top"
- )
- );
- }
+ const placement =
+ toGlobalCoordinates(
+ getPlacementBetween(closestChildRect, neighbourRect)
+ ) ||
+ toGlobalCoordinates(
+ getPlacementNextTo(parentRect, closestChildRect, side)
+ ) ||
+ getPlacementInside(parentRect, childrenOrientation);
const dropTarget: DropTarget<Data> = {
...partialDropTarget,
- rect,
- indexWithinChildren: indexWithinChildren,
- placement: placement || getPlacementInside(rect, childrenOrientation),
+ rect: parentRect,
+ indexWithinChildren,
+ placement,
};
+
state.current.dropTarget = dropTarget;
latestProps.current.onDropTargetChange(dropTarget);
}
@@ -409,31 +240,43 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
const { pointerCoordinates, root } = state.current;
if (pointerCoordinates === undefined || root === undefined) {
- handleDropTargetChange(getDefaultDropTarget());
- return;
- }
-
- const target = toUseableElement(
- document.elementFromPoint(pointerCoordinates.x, pointerCoordinates.y)
- );
- if (!target) {
- handleDropTargetChange(getDefaultDropTarget());
+ setDropTarget(getDefaultDropTarget());
return;
}
- // @todo: cache this? Not expensive by itself, but it may call isDropTarget a lot
+ // @todo: Cache this?
+ // Not expensive by itself, but it may call isDropTarget multiple times.
let candidate = findClosestDropTarget({
root,
- target,
+ initialElement: toUseableElement(
+ document.elementFromPoint(pointerCoordinates.x, pointerCoordinates.y)
+ ),
isDropTarget,
});
- if (candidate === undefined) {
- handleDropTargetChange(getDefaultDropTarget());
+
+ const isNewCandidate =
+ candidate?.element !== state.current.lastCandidateElement;
+ state.current.lastCandidateElement = candidate?.element;
+ const candidateIsNearEdge =
+ candidate &&
+ isNearEdge(
+ pointerCoordinates,
+ edgeDistanceThreshold,
+ candidate.element.getBoundingClientRect()
+ );
+ const isNewIsNearEdge =
+ candidateIsNearEdge !== state.current.lastCandidateIsNearEdge;
+ state.current.lastCandidateIsNearEdge = candidateIsNearEdge;
+
+ // to avoid calling swapDropTarget unnecessarily on every pointermove
+ if (!isNewCandidate && !isNewIsNearEdge) {
return;
}
- // @todo: if neither potentialTarget nor area has changed since the last time,
- // don't call swapDropTarget.
+ if (candidate === undefined) {
+ setDropTarget(getDefaultDropTarget());
+ return;
+ }
// eslint-disable-next-line no-constant-condition
while (true) {
@@ -447,12 +290,12 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
});
if (swappedTo === "DEFAULT") {
- handleDropTargetChange(getDefaultDropTarget());
+ setDropTarget(getDefaultDropTarget());
return;
}
if (swappedTo.element === candidate.element) {
- handleDropTargetChange(candidate);
+ setDropTarget(candidate);
return;
}
@@ -483,30 +326,30 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
// @todo: maybe rather than climbing the DOM tree,
// we should use document.elementsFromPoint() array?
-// Might work better with absolute positioned elements.
+// Might work better with absolutly positioned elements.
const findClosestDropTarget = <Data>({
root,
- target,
+ initialElement,
isDropTarget,
}: {
root: UsableElement;
- target: UsableElement;
+ initialElement: UsableElement | undefined;
isDropTarget: (target: UsableElement) => Data | false;
}): PartialDropTarget<Data> | undefined => {
// The element we get from elementFromPoint() might not be inside the root
- if (!root.contains(target)) {
+ if (!initialElement || !root.contains(initialElement)) {
return undefined;
}
- let currentTarget: UsableElement | undefined = target;
- while (currentTarget !== undefined) {
- const data = isDropTarget(currentTarget);
+ let currentElement: UsableElement | undefined = initialElement;
+ while (currentElement !== undefined) {
+ const data = isDropTarget(currentElement);
if (data !== false) {
- return { data: data, element: currentTarget };
+ return { data: data, element: currentElement };
}
- if (currentTarget === root) {
+ if (currentElement === root) {
break;
}
- currentTarget = toUseableElement(currentTarget.parentElement);
+ currentElement = toUseableElement(currentElement.parentElement);
}
};