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.

placement indicator position detection

Oleg Isonen5d651e24e7c9
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 9a0a669146..4f8eb252d5 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
@@ -3,8 +3,7 @@ import { useEffect, useRef, useState } from "react";
import { Box } from "../../box";
import { useDropTarget, type Area } from "./use-drop-target";
import { useDrag } from "./use-drag";
-
-type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;
+import { getPlacement, PlacementIndicator, type Rect } from "./placement";
export const Playground = () => {
const [dropTargetRect, setDropTargetRect] = useState<Rect>();
@@ -20,14 +19,18 @@ export const Playground = () => {
isDropTarget(element: HTMLElement) {
return element.dataset.draggable === "true";
},
- onDropTargetChange({ rect, area }: { rect: Rect; area: Area }) {
+ onDropTargetChange({
+ rect,
+ area,
+ target,
+ }: {
+ rect: Rect;
+ area: Area;
+ target: HTMLElement;
+ }) {
setDropTargetRect(rect);
- setPlacementIndicatorRect({
- top: rect.top,
- left: rect.left,
- width: rect.width,
- height: 2,
- });
+ const placementRect = getPlacement({ target });
+ setPlacementIndicatorRect(placementRect);
console.log("area", area);
},
onHold({ target }: { target: HTMLElement }) {
@@ -61,7 +64,7 @@ export const Playground = () => {
});
return (
- <div {...dragProps} ref={rootRef}>
+ <Box {...dragProps} ref={rootRef} css={{ display: "flex" }}>
<Item background="$cyanA9" />
<Item background="$slateA9" />
<Item background="$blueA9" draggable={false}>
@@ -69,7 +72,7 @@ export const Playground = () => {
</Item>
<Outline rect={dropTargetRect} />
<PlacementIndicator rect={placementIndicatorRect} />
- </div>
+ </Box>
);
};
@@ -77,7 +80,7 @@ export default {} as ComponentMeta<typeof Playground>;
const Item = ({ background, draggable = true, children }: any) => (
<Box
- css={{ width: 100, height: 100, background, margin: 10 }}
+ css={{ height: 100, width: 100, background, margin: 10 }}
data-draggable={draggable}
>
{children}
@@ -104,16 +107,3 @@ const Outline = ({ rect }: { rect?: Rect }) => {
/>
);
};
-
-const PlacementIndicator = ({ rect }: { rect?: Rect }) => {
- if (rect === undefined) return null;
- const style = {
- top: rect.top,
- left: rect.left,
- width: rect.width,
- height: rect.height,
- };
- return (
- <Box style={style} css={{ position: "absolute", background: "green" }} />
- );
-};
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/placement.tsx b/apps/designer/app/shared/design-system/components/primitives/dnd/placement.tsx
new file mode 100644
index 0000000000..93adc355b5
--- /dev/null
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/placement.tsx
@@ -0,0 +1,37 @@
+import { Box } from "../../box";
+
+export type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;
+
+const probe = document.createElement("div");
+
+/**
+ * We place a div at the position where future drop will happen.
+ * We learn if the indicator will render vertically or horizontally as well as its width/heigh.
+ */
+export const getPlacement = ({ target, index = 0 }: any = {}) => {
+ const { children } = target;
+ if (index > children.length - 1) {
+ target.appendChild(probe);
+ } else {
+ target.insertBefore(probe, children[index]);
+ }
+ const rect = probe.getBoundingClientRect();
+ target.removeChild(probe);
+ return rect;
+};
+
+export const PlacementIndicator = ({ rect }: { rect?: Rect }) => {
+ if (rect === undefined) return null;
+ const style = {
+ top: rect.top,
+ left: rect.left,
+ width: rect.width,
+ height: rect.height,
+ };
+ return (
+ <Box
+ style={style}
+ css={{ position: "absolute", border: "1px solid green" }}
+ />
+ );
+};
diff --git a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.ts b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.ts
index 63e4c57e74..b524a0e39f 100644
--- a/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.ts
+++ b/apps/designer/app/shared/design-system/components/primitives/dnd/use-drop-target.ts
@@ -60,6 +60,7 @@ export const useDropTarget = ({
onDropTargetChange({
rect: targetRectRef.current,
area: nextArea,
+ target: nextTarget,
});
}
},