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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
useDrop hook (wip)
Roman Pominov28215c5c107a
diff --git a/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx b/packages/design-system/src/components/primitives/dnd/canvas.stories.tsxindex 360c419d29..e442ca5da0 100644--- a/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx+++ b/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx@@ -1,7 +1,7 @@import { ComponentMeta } from "@storybook/react";import React, { useState, useRef } from "react";import { Box } from "../../box";-import { useDropTarget } from "./use-drop-target";+import { useDrop } from "./use-drop";import { useDrag } from "./use-drag";import { type Rect } from "./rect";import { usePlacement, PlacementIndicator } from "./placement";@@ -31,7 +31,7 @@ const Item = ({minHeight: 100,margin: 10,padding: 10,- background: "$mint12",+ background: "$mint5",border: "1px solid $mint9",}}style={data.style}@@ -59,7 +59,8 @@ const Items = ({);};-const elementToId = (element: HTMLElement) => element.dataset.id;+const elementToId = (element: Element) =>+ element instanceof HTMLElement && element.dataset.id;const idToElement = (root: HTMLElement,@@ -83,16 +84,16 @@ const mapItems = (data: ItemData[],fn: (item: ItemData) => ItemData): ItemData[] => {+ const recur = (+ data: ItemData[],+ fn: (item: ItemData) => ItemData+ ): ItemData[] =>+ data.map((item) => fn({ ...item, children: recur(item.children, fn) }));return fn({id: ROOT_ID,style: {},acceptsChildren: true,- children: data.map((item) => {- return fn({- ...item,- children: mapItems(item.children, fn),- });- }),+ children: recur(data, fn),}).children;};@@ -128,9 +129,24 @@ export const Canvas = () => {id: "0",style: {},children: [- { id: "1", style: { margin: 0 }, children: [], acceptsChildren: true },- { id: "2", style: { margin: 0 }, children: [], acceptsChildren: true },- { id: "3", style: { margin: 0 }, children: [], acceptsChildren: true },+ {+ id: "1",+ style: { margin: 0, background: "#ff7878" },+ children: [],+ acceptsChildren: true,+ },+ {+ id: "2",+ style: { margin: 0, background: "#a8d1ff" },+ children: [],+ acceptsChildren: true,+ },+ {+ id: "3",+ style: { margin: 0, background: "#94ef94" },+ children: [],+ acceptsChildren: true,+ },],acceptsChildren: true,},@@ -149,18 +165,27 @@ export const Canvas = () => {const dropTargetId = useRef<string>();const rootRef = useRef<HTMLElement | null>(null);- const dropTargetHandlers = useDropTarget<string>({+ const dropTargetHandlers = useDrop<string>({edgeDistanceThreshold: 10,- isDropTarget(element: HTMLElement) {++ isDropTarget(element) {return elementToId(element) ?? false;},++ getDefaultDropTarget() {+ if (rootRef.current === null) {+ throw new Error("should not happen");+ }+ return { data: ROOT_ID, element: rootRef.current };+ },+swapDropTarget(dropTarget) {const rootElement = rootRef.current;if (rootElement === null) {return dropTarget;}- const { data: id, area } = dropTarget;+ const { data: id, nearEdge } = dropTarget;if (id === ROOT_ID) {return dropTarget;@@ -168,7 +193,7 @@ export const Canvas = () => {const path = findItemPath(data, id) ?? [];- if (area !== "center") {+ if (nearEdge) {path.shift();}@@ -195,7 +220,7 @@ export const Canvas = () => {onDropTargetChange({ data, element }) {dropTargetId.current = data;- placementHandlers.handleTargetChange(element);+ placementHandlers.handleTargetChange(element as any);},});diff --git a/packages/design-system/src/components/primitives/dnd/placement.tsx b/packages/design-system/src/components/primitives/dnd/placement.tsxindex 69b69d7990..29856db034 100644--- a/packages/design-system/src/components/primitives/dnd/placement.tsx+++ b/packages/design-system/src/components/primitives/dnd/placement.tsx@@ -60,7 +60,7 @@ export const usePlacement = (props: UsePlacementProps): Handlers => {placementRext: undefined as Rect | undefined,});- // We want to rerurn a stable object to avoid re-renders when it's a dependency+ // We want to return a stable object to avoid re-renders when it's a dependencyreturn useMemo(() => {const getChildrenRects = (parent: HTMLElement, parentRect: DOMRect) => {const fromCache = state.current.childrenRectsCache.get(parent);@@ -104,19 +104,19 @@ export const usePlacement = (props: UsePlacementProps): Handlers => {childrenRects,pointerAdjusted);- const closesRect = childrenRects[closestRectIndex];+ const closestRect = childrenRects[closestRectIndex];const isOrientationVertical =childrenRects.length < 2 ||childrenRects[0].left === childrenRects[1].left;if (isOrientationVertical) {- const middleY = closesRect.top + closesRect.height / 2;+ const middleY = closestRect.top + closestRect.height / 2;return pointerAdjusted.y < middleY? closestRectIndex: closestRectIndex + 1;} else {- const middleX = closesRect.left + closesRect.width / 2;+ const middleX = closestRect.left + closestRect.width / 2;return pointerAdjusted.x < middleX? closestRectIndex: closestRectIndex + 1;diff --git a/packages/design-system/src/components/primitives/dnd/rect.ts b/packages/design-system/src/components/primitives/dnd/rect.tsindex b6dd8e17ee..2bb90f3679 100644--- a/packages/design-system/src/components/primitives/dnd/rect.ts+++ b/packages/design-system/src/components/primitives/dnd/rect.ts@@ -14,23 +14,42 @@ export const getClosestRectIndex = (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) => {- return (- a !== undefined &&- a.top === b.top &&- a.left === b.left &&- a.width === b.width &&- a.height === b.height- );-};+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/use-drag.ts b/packages/design-system/src/components/primitives/dnd/use-drag.tsindex 8dffecd8ef..16d23ea0e4 100644--- a/packages/design-system/src/components/primitives/dnd/use-drag.ts+++ b/packages/design-system/src/components/primitives/dnd/use-drag.ts@@ -142,7 +142,7 @@ export const useDrag = <DragItemData>({}}, [onPointerDown]);- // We want to rerurn a stable object to avoid re-renders when it's a dependency+ // We want to return a stable object to avoid re-renders when it's a dependencyreturn useMemo(() => {return {rootRef(element) {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.tsindex e191c4d76f..937646a226 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@@ -55,7 +55,7 @@ export const useDropTarget = <Data>(dropTarget: undefined as DropTarget<Data> | undefined,});- // We want to rerurn a stable object to avoid re-renders when it's a dependency+ // We want to return a stable object to avoid re-renders when it's a dependencyreturn useMemo(() => {const detectTarget = () => {const {diff --git a/packages/design-system/src/components/primitives/dnd/use-drop.test.ts b/packages/design-system/src/components/primitives/dnd/use-drop.test.tsnew file mode 100644index 0000000000..7ee27dcef5--- /dev/null+++ b/packages/design-system/src/components/primitives/dnd/use-drop.test.ts@@ -0,0 +1,165 @@+import { getPlacementBetween } from "./use-drop";++const size = { width: 100, height: 100 };++describe("getPlacementBetween", () => {+ // [a] | [b]+ test("horizontal rects", () => {+ const placement = getPlacementBetween(+ { top: 0, left: 0, ...size },+ { top: 0, left: 200, ...size }+ );+ expect(placement).toEqual({+ direction: "vertical",+ x: 150,+ y: 0,+ length: 100,+ });+ });++ // [b] | [a]+ test("horizontal rects (reversed)", () => {+ const placement = getPlacementBetween(+ { top: 0, left: 200, ...size },+ { top: 0, left: 0, ...size }+ );+ expect(placement).toEqual({+ direction: "vertical",+ x: 150,+ y: 0,+ length: 100,+ });+ });++ // [a]+ // ---+ // [b]+ test("vertical rects", () => {+ const placement = getPlacementBetween(+ { top: 0, left: 0, ...size },+ { top: 200, left: 0, ...size }+ );+ expect(placement).toEqual({+ direction: "horizontal",+ x: 0,+ y: 150,+ length: 100,+ });+ });++ // [a]+ // ---+ // [b]+ test("vertical rects (reversed)", () => {+ const placement = getPlacementBetween(+ { top: 200, left: 0, ...size },+ { top: 0, left: 0, ...size }+ );+ expect(placement).toEqual({+ direction: "horizontal",+ x: 0,+ y: 150,+ length: 100,+ });+ });++ // [a]+ // -----------+ // [b]+ test("diagonal, closer by Y", () => {+ const placement = getPlacementBetween(+ { top: 0, left: 600, ...size },+ { top: 200, left: 0, ...size }+ );+ expect(placement).toEqual({+ direction: "horizontal",+ x: 0,+ y: 150,+ length: 700,+ });+ });++ // [b]+ // -----------+ // [a]+ test("diagonal, closer by Y (reversed)", () => {+ const placement = getPlacementBetween(+ { top: 200, left: 0, ...size },+ { top: 0, left: 600, ...size }+ );+ expect(placement).toEqual({+ direction: "horizontal",+ x: 0,+ y: 150,+ length: 700,+ });+ });++ // [a] |+ // |+ // |+ // | [b]+ test("diagonal, closer by X", () => {+ const placement = getPlacementBetween(+ { top: 0, left: 0, ...size },+ { top: 600, left: 200, ...size }+ );+ expect(placement).toEqual({+ direction: "vertical",+ x: 150,+ y: 0,+ length: 700,+ });+ });++ // [b] |+ // |+ // |+ // | [a]+ test("diagonal, closer by X (reversed)", () => {+ const placement = getPlacementBetween(+ { top: 600, left: 200, ...size },+ { top: 0, left: 0, ...size }+ );+ expect(placement).toEqual({+ direction: "vertical",+ x: 150,+ y: 0,+ length: 700,+ });+ });++ test.each([+ [+ { top: 0, left: 0 },+ { top: 0, left: 0 },+ ],+ [+ { top: 0, left: 0 },+ { top: 50, left: 50 },+ ],+ [+ { top: 50, left: 50 },+ { top: 0, left: 0 },+ ],+ [+ { top: 0, left: 50 },+ { top: 50, left: 0 },+ ],+ [+ { top: 50, left: 0 },+ { top: 0, left: 50 },+ ],+ [+ { top: 0, left: 0, width: 200, height: 200 },+ { top: 50, left: 50 },+ ],+ [+ { top: 50, left: 50 },+ { top: 0, left: 0, width: 200, height: 200 },+ ],+ ])("overlap %o %o", (a, b) => {+ const placement = getPlacementBetween({ ...size, ...a }, { ...size, ...b });+ expect(placement).toBeUndefined();+ });+});diff --git a/packages/design-system/src/components/primitives/dnd/use-drop.ts b/packages/design-system/src/components/primitives/dnd/use-drop.tsindex 2f6176ed65..9f26a556ad 100644--- a/packages/design-system/src/components/primitives/dnd/use-drop.ts+++ b/packages/design-system/src/components/primitives/dnd/use-drop.ts@@ -1,10 +1,156 @@import { useRef, useMemo } from "react";-import { isEqualRect } from "./rect";+import {+ isEqualRect,+ isNearEdge,+ rectRelativeToRect,+ pointRelativeToRect,+ getClosestRectIndex,+ type Rect,+} from "./rect";-export type DropTarget<Data> = {+// @todo: use this in useDrag as well+type UsableElement = HTMLElement | SVGElement;+const toUseableElement = (+ element: Element | undefined | null+): UsableElement | undefined => {+ if (element instanceof HTMLElement || element instanceof SVGElement) {+ return element;+ }+};++// 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+const getLocalChildrenOrientation = (+ parent: UsableElement,+ childrentRects: Rect[],+ childIndex: number+): ChildrenOrientation => {+ const previous = childrentRects[childIndex - 1] as Rect | undefined;+ const current = childrentRects[childIndex] as Rect | undefined;+ 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 slope = height / width;+ const topRightCorner = { x: left + width, y: top };+ return slope * (diagonalX - topRightCorner.x) + topRightCorner.y;+ };++ const diagonalY = getDiagonalY(pointer.x);+ if (diagonalY === undefined) {+ return 0;+ }+ return pointer.y < diagonalY ? 0 : 1;+};++// Partial information about a drop target+// used during the selection of a new drop target+type PartialDropTarget<Data> = {data: Data;- target: Element;+ 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 type DropTarget<Data> = PartialDropTarget<Data> & {rect: DOMRect;+ indexWithinChildren: number;+ placement: Placement;};// We pass around data, to avoid extra data lookups.@@ -12,27 +158,39 @@ export type DropTarget<Data> = {// doesn't have to be looked up again in swapDropTarget.export type UseDropProps<Data> = {// To check that the element can qualify as a target- isDropTarget: (target: Element) => Data | false;+ isDropTarget: (target: UsableElement) => Data | false;++ getDefaultDropTarget: () => PartialDropTarget<Data>;++ // Distance from an edge for pointer to be considered on an edge.+ edgeDistanceThreshold?: number;// Given the potential target that has passed isDropTarget check,// and the position of the pointer on the target,// you can swap to another target- swapDropTarget?: (args: { target: Element; data: Data; onEdge: boolean }) => {- target: Element;- data: Data;- };+ swapDropTarget?: (+ dropTarget: PartialDropTarget<Data> & { nearEdge: boolean }+ ) => PartialDropTarget<Data> | "DEFAULT";onDropTargetChange: (dropTarget: DropTarget<Data>) => void;- edgeDistanceThreshold?: number;};export type UseDropHandlers = {- handleMove: (pointerCoordinate: { x: number; y: number }) => void;+ handleMove: (pointerCoordinates: { x: number; y: number }) => void;handleScroll: () => void;handleEnd: () => void;rootRef: (target: Element | null) => void;};+const getInitialState = <Data>() => {+ return {+ root: undefined as UsableElement | undefined,+ pointerCoordinates: undefined as { x: number; y: number } | undefined,+ dropTarget: undefined as DropTarget<Data> | undefined,+ childrenRectsCache: new WeakMap<UsableElement, Rect[]>(),+ };+};+export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {// We want to use fresh props every time we use them,// but we don't need to react to updates.@@ -40,87 +198,159 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {const latestProps = useRef<UseDropProps<Data>>(props);latestProps.current = props;- const state = useRef({- root: null as Element | null,- pointerCoordinate: undefined as { x: number; y: number } | undefined,- dropTarget: undefined as DropTarget<Data> | undefined,- });+ const state = useRef(getInitialState());- // We want to rerurn a stable object to avoid re-renders when it's a dependency+ // We want to return a stable object to avoid re-renders when it's a dependencyreturn useMemo(() => {+ const getChildrenRects = (parent: UsableElement, parentRect: Rect) => {+ const fromCache = state.current.childrenRectsCache.get(parent);+ if (fromCache !== undefined) {+ return fromCache;+ }++ const result = Array.from(parent.children).map((child) =>+ rectRelativeToRect(parentRect, child.getBoundingClientRect())+ );++ state.current.childrenRectsCache.set(parent, result);+ return result;+ };++ const getIndex = (+ parent: UsableElement,+ parentRect: Rect,+ pointer: { x: number; y: number }+ ) => {+ const pointerRelativeToParent = pointRelativeToRect(parentRect, pointer);+ const childrenRects = getChildrenRects(parent, parentRect);++ if (childrenRects.length === 0) {+ return 0;+ }++ const closestChildIndex = getClosestRectIndex(+ childrenRects,+ pointerRelativeToParent+ );++ const orientation = getLocalChildrenOrientation(+ parent,+ childrenRects,+ closestChildIndex+ );++ const adjustment = getIndexAdjustment(+ pointerRelativeToParent,+ childrenRects[closestChildIndex],+ orientation+ );++ return closestChildIndex + adjustment;+ };++ const handleDropTargetChange = (+ partialDropTarget: PartialDropTarget<Data>+ ) => {+ const { pointerCoordinates } = state.current;+ if (pointerCoordinates === undefined) {+ return;+ }++ const rect = partialDropTarget.element.getBoundingClientRect();++ const indexWithinChildren = getIndex(+ partialDropTarget.element,+ rect,+ pointerCoordinates+ );++ const current = state.current.dropTarget;+ if (+ current === undefined ||+ current.element !== partialDropTarget.element ||+ current.indexWithinChildren !== indexWithinChildren ||+ !isEqualRect(current.rect, rect)+ ) {+ const dropTarget: DropTarget<Data> = {+ ...partialDropTarget,+ rect,+ indexWithinChildren: indexWithinChildren,+ placement: {+ x: 0,+ y: 0,+ length: 0,+ direction: "horizontal",+ },+ };+ state.current.dropTarget = dropTarget;+ latestProps.current.onDropTargetChange(dropTarget);+ }+ };+const detectTarget = () => {const {edgeDistanceThreshold = 3,isDropTarget,- swapDropTarget = (args) => args,- onDropTargetChange,+ swapDropTarget = (x) => x,+ getDefaultDropTarget,} = latestProps.current;- const { pointerCoordinate, root } = state.current;- if (pointerCoordinate === undefined || root === null) {+ const { pointerCoordinates, root } = state.current;+ if (pointerCoordinates === undefined || root === undefined) {+ handleDropTargetChange(getDefaultDropTarget());return;}- const target = document.elementFromPoint(- pointerCoordinate.x,- pointerCoordinate.y+ const target = toUseableElement(+ document.elementFromPoint(pointerCoordinates.x, pointerCoordinates.y));if (!target) {+ handleDropTargetChange(getDefaultDropTarget());return;}// @todo: cache this? Not expensive by itself, but it may call isDropTarget a lot- const potentialTarget = findClosestDropTarget({+ let candidate = findClosestDropTarget({root,target,isDropTarget,});- if (potentialTarget === undefined) {+ if (candidate === undefined) {+ handleDropTargetChange(getDefaultDropTarget());return;}// @todo: if neither potentialTarget nor area has changed since the last time,// don't call swapDropTarget.- const dropTarget = {- ...potentialTarget,- rect: potentialTarget.target.getBoundingClientRect(),- };-// eslint-disable-next-line no-constant-conditionwhile (true) {- const swappedTarget = swapDropTarget({- target: dropTarget.target,- data: dropTarget.data,- onEdge: isOnEdge(- pointerCoordinate,+ const swappedTo = swapDropTarget({+ ...candidate,+ nearEdge: isNearEdge(+ pointerCoordinates,edgeDistanceThreshold,- dropTarget.rect+ candidate.element.getBoundingClientRect()),});- if (swappedTarget.target === dropTarget.target) {- break;+ if (swappedTo === "DEFAULT") {+ handleDropTargetChange(getDefaultDropTarget());+ return;}- dropTarget.target = swappedTarget.target;- dropTarget.data = swappedTarget.data;- dropTarget.rect = swappedTarget.target.getBoundingClientRect();- }+ if (swappedTo.element === candidate.element) {+ handleDropTargetChange(candidate);+ return;+ }- if (- state.current.dropTarget === undefined ||- state.current.dropTarget.target !== dropTarget.target ||- !isEqualRect(state.current.dropTarget.rect, dropTarget.rect)- ) {- state.current.dropTarget = dropTarget;- onDropTargetChange(dropTarget);+ candidate = swappedTo;}};return {- handleMove(pointerCoordinate: { x: number; y: number }) {- state.current.pointerCoordinate = pointerCoordinate;+ handleMove(pointerCoordinates) {+ state.current.pointerCoordinates = pointerCoordinates;detectTarget();},@@ -129,29 +359,16 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {},handleEnd() {- state.current.pointerCoordinate = undefined;- state.current.dropTarget = undefined;+ state.current = getInitialState();},- rootRef(rootElement: Element | null) {- state.current.root = rootElement;+ rootRef(rootElement) {+ state.current.root = toUseableElement(rootElement);},};}, []);};-const isOnEdge = (- { x, y }: { x: number; y: number },- edgeDistanceThreshold: number,- targetRect: DOMRect-) =>- Math.min(- y - targetRect.top,- targetRect.bottom - y,- x - targetRect.left,- targetRect.right - x- ) <= edgeDistanceThreshold;-// @todo: maybe rather than climbing the DOM tree,// we should use document.elementsFromPoint() array?// Might work better with absolute positioned elements.@@ -160,22 +377,24 @@ const findClosestDropTarget = <Data>({target,isDropTarget,}: {- root: Element;- target: Element;- isDropTarget: (target: Element) => Data | false;-}) => {- let currentTarget: Element | null = target;- while (currentTarget !== null) {+ root: UsableElement;+ target: UsableElement;+ isDropTarget: (target: UsableElement) => Data | false;+}): PartialDropTarget<Data> | undefined => {+ // The element we get from elementFromPoint() might not be inside the root+ if (!root.contains(target)) {+ return undefined;+ }++ let currentTarget: UsableElement | undefined = target;+ while (currentTarget !== undefined) {const data = isDropTarget(currentTarget);if (data !== false) {- return { data: data, target: currentTarget };+ return { data: data, element: currentTarget };}-- // @todo: what makes us think that document.elementFromPoint() is inside the root in the first place?if (currentTarget === root) {break;}-- currentTarget = currentTarget.parentElement;+ currentTarget = toUseableElement(currentTarget.parentElement);}};