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.

fix probe

Roman Pominov053ac6d300a6
diff --git a/packages/design-system/src/components/primitives/dnd/dom-utils.ts b/packages/design-system/src/components/primitives/dnd/dom-utils.ts
index 4c860eeb41..23791de7b3 100644
--- a/packages/design-system/src/components/primitives/dnd/dom-utils.ts
+++ b/packages/design-system/src/components/primitives/dnd/dom-utils.ts
@@ -2,12 +2,45 @@ import {
type Rect,
type ChildrenOrientation,
getRectsOrientation,
+ getTwoRectsOrientation,
} from "./geometry-utils";
+const getOrientaionUsingProbe = (
+ parent: Element,
+ relativeToChild?: Element
+): ChildrenOrientation => {
+ const probe = document.createElement("div");
+ if (relativeToChild) {
+ parent.insertBefore(probe, relativeToChild);
+ } else {
+ parent.appendChild(probe);
+ }
+ const probeRect = probe.getBoundingClientRect();
+ parent.removeChild(probe);
+
+ // If there's no child, see if one of the dimensions collapsed.
+ // If both or neither collapsed, fallback to vertical as the best guess.
+ if (relativeToChild === undefined) {
+ return {
+ type:
+ probeRect.width === 0 && probeRect.height > 0
+ ? "horizontal"
+ : "vertical",
+ reverse: false,
+ };
+ }
+
+ return getTwoRectsOrientation(
+ probeRect,
+ relativeToChild.getBoundingClientRect()
+ );
+};
+
// By looking at a specific child and it's neighbours,
// determines their orientation relative to each other
export const getLocalChildrenOrientation = (
parent: Element,
+ getChildren: (parent: Element) => Element[] | HTMLCollection,
childrentRects: Rect[],
childIndex: number
): ChildrenOrientation => {
@@ -15,24 +48,13 @@ export const getLocalChildrenOrientation = (
const current = childrentRects[childIndex] as Rect | undefined;
const next = childrentRects[childIndex + 1] as Rect | undefined;
+ // If there are no two rects to compare, use a probe
if (current === undefined || (next === undefined && previous === undefined)) {
- const probe = document.createElement("div");
- const { children } = parent;
- if (childIndex > children.length - 1) {
- parent.appendChild(probe);
- } else {
- parent.insertBefore(probe, children[childIndex]);
- }
- const probeRect = probe.getBoundingClientRect();
- parent.removeChild(probe);
-
- return {
- type:
- probeRect.width === 0 && probeRect.height > 0
- ? "horizontal"
- : "vertical",
- reverse: false,
- };
+ const children = getChildren(parent);
+ return getOrientaionUsingProbe(
+ parent,
+ childIndex < children.length ? children[childIndex] : undefined
+ );
}
return getRectsOrientation(previous, current, next);
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 fa9745765f..4e14e5d8ee 100644
--- a/packages/design-system/src/components/primitives/dnd/use-drop.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-drop.ts
@@ -145,6 +145,7 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {
const childrenOrientation = getLocalChildrenOrientation(
partialDropTarget.element,
+ latestProps.current.getValidChildren ?? ((parent) => parent.children),
childrenRects,
closestChildIndex
);