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.

Preview navigator drop target while dragging canvas (#1276)

Bogdan Chadkin70b9e7e5cff0
diff --git a/apps/builder/app/builder/features/sidebar-left/navigator/navigator.tsx b/apps/builder/app/builder/features/sidebar-left/navigator/navigator.tsx
index 9adcd19fde..a06ce85ce8 100644
--- a/apps/builder/app/builder/features/sidebar-left/navigator/navigator.tsx
+++ b/apps/builder/app/builder/features/sidebar-left/navigator/navigator.tsx
@@ -1,14 +1,16 @@
-import { useCallback } from "react";
+import { useCallback, useMemo } from "react";
import { useStore } from "@nanostores/react";
-import { Flex } from "@webstudio-is/design-system";
+import { Flex, type ItemDropTarget } from "@webstudio-is/design-system";
import {
rootInstanceStore,
selectedInstanceSelectorStore,
hoveredInstanceSelectorStore,
+ useDragAndDropState,
+ instancesStore,
} from "~/shared/nano-states";
import { InstanceTree } from "~/builder/shared/tree";
import { reparentInstance } from "~/shared/instance-utils";
-import type { InstanceSelector } from "~/shared/tree-utils";
+import { getInstanceSelector, InstanceSelector } from "~/shared/tree-utils";
import { Header, CloseButton } from "../header";
type NavigatorProps = {
@@ -18,7 +20,30 @@ type NavigatorProps = {
export const Navigator = ({ isClosable, onClose }: NavigatorProps) => {
const selectedInstanceSelector = useStore(selectedInstanceSelectorStore);
+ const instances = useStore(instancesStore);
const rootInstance = useStore(rootInstanceStore);
+ const [state, setState] = useDragAndDropState();
+
+ const dragItemSelector = useMemo(() => {
+ if (state.dragItem?.id === undefined) {
+ return;
+ }
+ return getInstanceSelector(instances, state.dragItem.id);
+ }, [state.dragItem, instances]);
+
+ const dropTarget = useMemo((): undefined | ItemDropTarget => {
+ if (state.dropTarget === undefined) {
+ return;
+ }
+ return {
+ placement: state.dropTarget.placement,
+ indexWithinChildren: state.dropTarget.position,
+ itemSelector: getInstanceSelector(
+ instances,
+ state.dropTarget.instance.id
+ ),
+ };
+ }, [state.dropTarget, instances]);
const handleDragEnd = useCallback(
(payload: {
@@ -46,8 +71,33 @@ export const Navigator = ({ isClosable, onClose }: NavigatorProps) => {
<InstanceTree
root={rootInstance}
selectedItemSelector={selectedInstanceSelector}
+ dragItemSelector={dragItemSelector}
+ dropTarget={dropTarget}
onSelect={selectedInstanceSelectorStore.set}
onHover={hoveredInstanceSelectorStore.set}
+ onDragItemChange={(dragItemSelector) => {
+ const instances = instancesStore.get();
+ const instance = instances.get(dragItemSelector[0]);
+ if (instance === undefined) {
+ return;
+ }
+ setState({ ...state, dragItem: instance });
+ }}
+ onDropTargetChange={(dropTarget) => {
+ const instances = instancesStore.get();
+ const instance = instances.get(dropTarget.itemSelector[0]);
+ if (instance === undefined) {
+ return;
+ }
+ setState({
+ ...state,
+ dropTarget: {
+ placement: dropTarget.placement,
+ position: dropTarget.indexWithinChildren,
+ instance,
+ },
+ });
+ }}
onDragEnd={handleDragEnd}
/>
</Flex>
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
index 093ed636b9..99b57909f6 100644
--- a/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/canvas-tools.tsx
@@ -8,15 +8,13 @@ import {
useDragAndDropState,
useSubscribeDragAndDropState,
} from "~/shared/nano-states";
-import {
- HoveredInstanceOutline,
- SelectedInstanceOutline,
- DropTargetOutline,
-} from "./outline";
+import { HoveredInstanceOutline, SelectedInstanceOutline } from "./outline";
import { useSubscribeTextToolbar, TextToolbar } from "./text-toolbar";
import { useSubscribeInstanceRect } from "./hooks/use-subscribe-instance-rect";
import { useSubscribeTextEditingInstanceId } from "./hooks/use-subscribe-editing-instance-id";
import { useSubscribeSwitchPage } from "~/shared/pages";
+import { Label } from "./outline/label";
+import { Outline } from "./outline/outline";
const toolsStyle = {
position: "absolute",
@@ -47,13 +45,20 @@ export const CanvasTools = ({ publish }: CanvasToolsProps) => {
if (
dragAndDropState.isDragging &&
- dragAndDropState.dropTarget !== undefined
+ dragAndDropState.dropTarget !== undefined &&
+ dragAndDropState.placementIndicator !== undefined
) {
+ const { dropTarget, placementIndicator } = dragAndDropState;
return (
<Box css={toolsStyle}>
- <DropTargetOutline dropTarget={dragAndDropState.dropTarget} />
- {dragAndDropState.placementIndicator !== undefined && (
- <PlacementIndicator placement={dragAndDropState.placementIndicator} />
+ <Outline rect={placementIndicator.parentRect}>
+ <Label
+ instance={dropTarget.instance}
+ instanceRect={placementIndicator.parentRect}
+ />
+ </Outline>
+ {placementIndicator !== undefined && (
+ <PlacementIndicator placement={placementIndicator} />
)}
</Box>
);
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/outline/drop-target-outline.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/outline/drop-target-outline.tsx
deleted file mode 100644
index 467e551214..0000000000
--- a/apps/builder/app/builder/features/workspace/canvas-tools/outline/drop-target-outline.tsx
+++ /dev/null
@@ -1,15 +0,0 @@
-import { Outline } from "./outline";
-import type { DropTargetChangePayload } from "~/canvas/shared/use-drag-drop";
-import { Label } from "./label";
-
-export const DropTargetOutline = ({
- dropTarget,
-}: {
- dropTarget: DropTargetChangePayload;
-}) => {
- return (
- <Outline rect={dropTarget.rect}>
- <Label instance={dropTarget.instance} instanceRect={dropTarget.rect} />
- </Outline>
- );
-};
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/outline/index.ts b/apps/builder/app/builder/features/workspace/canvas-tools/outline/index.ts
index 4b15c92ef0..1cf83e8390 100644
--- a/apps/builder/app/builder/features/workspace/canvas-tools/outline/index.ts
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/outline/index.ts
@@ -1,3 +1,2 @@
export { SelectedInstanceOutline } from "./selected-instance-outline";
export { HoveredInstanceOutline } from "./hovered-instance-outline";
-export { DropTargetOutline } from "./drop-target-outline";
diff --git a/apps/builder/app/canvas/shared/use-drag-drop.ts b/apps/builder/app/canvas/shared/use-drag-drop.ts
index b642dd7062..ae8e56e496 100644
--- a/apps/builder/app/canvas/shared/use-drag-drop.ts
+++ b/apps/builder/app/canvas/shared/use-drag-drop.ts
@@ -45,7 +45,6 @@ declare module "~/shared/pubsub" {
}
export type DropTargetChangePayload = {
- rect: DropTarget<null>["rect"];
placement: DropTarget<null>["placement"];
position: number;
instance: BaseInstance;
@@ -64,7 +63,7 @@ export type DragEndPayload = {
export type DragMovePayload = { canvasCoordinates: Point };
const initialState: {
- dropTarget: DropTarget<Instance> | undefined;
+ dropTarget: DropTargetChangePayload | undefined;
dragItem: BaseInstance | undefined;
} = {
dropTarget: undefined,
@@ -164,11 +163,9 @@ export const useDragAndDrop = () => {
},
onDropTargetChange(dropTarget) {
- state.current.dropTarget = dropTarget;
publish({
type: "dropTargetChange",
payload: {
- rect: dropTarget.rect,
placement: dropTarget.placement,
position: dropTarget.indexWithinChildren,
instance: toBaseInstance(dropTarget.data),
@@ -246,8 +243,8 @@ export const useDragAndDrop = () => {
if (dropTarget && dragItem && isCanceled === false) {
reparentInstance(dragItem.id, {
- parentId: dropTarget.data.id,
- position: dropTarget.indexWithinChildren,
+ parentId: dropTarget.instance.id,
+ position: dropTarget.position,
});
}
@@ -291,6 +288,22 @@ export const useDragAndDrop = () => {
autoScrollHandlers.handleMove(canvasCoordinates);
});
+ useSubscribe("dropTargetChange", (dropTarget) => {
+ state.current.dropTarget = dropTarget;
+ const element = getInstanceElementById(dropTarget.instance.id) ?? undefined;
+ if (element === undefined) {
+ return;
+ }
+ publish({
+ type: "placementIndicatorChange",
+ payload: computeIndicatorPlacement({
+ ...sharedDropOptions,
+ element,
+ placement: dropTarget.placement,
+ }),
+ });
+ });
+
useSubscribe("dragEnd", ({ origin, isCanceled }) => {
if (origin === "panel") {
dropHandlers.handleEnd({ isCanceled });
@@ -300,8 +313,8 @@ export const useDragAndDrop = () => {
if (dropTarget && dragItem && isCanceled === false) {
insertNewComponentInstance(dragItem.component, {
- parentId: dropTarget.data.id,
- position: dropTarget.indexWithinChildren,
+ parentId: dropTarget.instance.id,
+ position: dropTarget.position,
});
}
diff --git a/packages/design-system/src/components/primitives/dnd/__snapshots__/geometry-utils.test.ts.snap b/packages/design-system/src/components/primitives/dnd/__snapshots__/geometry-utils.test.ts.snap
index 5ad72e00b1..b696df4e7d 100644
--- a/packages/design-system/src/components/primitives/dnd/__snapshots__/geometry-utils.test.ts.snap
+++ b/packages/design-system/src/components/primitives/dnd/__snapshots__/geometry-utils.test.ts.snap
@@ -4,6 +4,12 @@ exports[`getPlacementInside childrenOrientation={ type: 'horizontal', reverse: f
{
"direction": "vertical",
"length": 90,
+ "parentRect": {
+ "height": 100,
+ "left": 0,
+ "top": 0,
+ "width": 100,
+ },
"type": "inside-parent",
"x": 5,
"y": 5,
@@ -14,6 +20,12 @@ exports[`getPlacementInside childrenOrientation={ type: 'horizontal', reverse: t
{
"direction": "vertical",
"length": 90,
+ "parentRect": {
+ "height": 100,
+ "left": 0,
+ "top": 0,
+ "width": 100,
+ },
"type": "inside-parent",
"x": 5,
"y": 5,
@@ -24,6 +36,12 @@ exports[`getPlacementInside childrenOrientation={ type: 'mixed' } 1`] = `
{
"direction": "horizontal",
"length": 90,
+ "parentRect": {
+ "height": 100,
+ "left": 0,
+ "top": 0,
+ "width": 100,
+ },
"type": "inside-parent",
"x": 5,
"y": 5,
@@ -34,6 +52,12 @@ exports[`getPlacementInside childrenOrientation={ type: 'vertical', reverse: fal
{
"direction": "horizontal",
"length": 90,
+ "parentRect": {
+ "height": 100,
+ "left": 0,
+ "top": 0,
+ "width": 100,
+ },
"type": "inside-parent",
"x": 5,
"y": 5,
@@ -44,6 +68,12 @@ exports[`getPlacementInside childrenOrientation={ type: 'vertical', reverse: tru
{
"direction": "horizontal",
"length": 90,
+ "parentRect": {
+ "height": 100,
+ "left": 0,
+ "top": 0,
+ "width": 100,
+ },
"type": "inside-parent",
"x": 5,
"y": 5,
@@ -54,6 +84,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: f
{
"direction": "vertical",
"length": 292,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 2,
"y": 4,
@@ -64,6 +100,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: f
{
"direction": "vertical",
"length": 310,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": -10,
"y": -10,
@@ -74,6 +116,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: f
{
"direction": "vertical",
"length": 300,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 0,
"y": 0,
@@ -84,6 +132,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: f
{
"direction": "vertical",
"length": 100,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 95,
"y": 100,
@@ -94,6 +148,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: f
{
"direction": "vertical",
"length": 292,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 298,
"y": 4,
@@ -104,6 +164,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: f
{
"direction": "vertical",
"length": 310,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 300,
"y": -10,
@@ -114,6 +180,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: f
{
"direction": "vertical",
"length": 300,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 300,
"y": 0,
@@ -124,6 +196,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: f
{
"direction": "vertical",
"length": 100,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 205,
"y": 100,
@@ -134,6 +212,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: t
{
"direction": "vertical",
"length": 292,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 298,
"y": 4,
@@ -144,6 +228,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: t
{
"direction": "vertical",
"length": 310,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 300,
"y": -10,
@@ -154,6 +244,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: t
{
"direction": "vertical",
"length": 300,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 300,
"y": 0,
@@ -164,6 +260,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: t
{
"direction": "vertical",
"length": 100,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 205,
"y": 100,
@@ -174,6 +276,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: t
{
"direction": "vertical",
"length": 292,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 2,
"y": 4,
@@ -184,6 +292,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: t
{
"direction": "vertical",
"length": 310,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": -10,
"y": -10,
@@ -194,6 +308,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: t
{
"direction": "vertical",
"length": 300,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 0,
"y": 0,
@@ -204,6 +324,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'horizontal', reverse: t
{
"direction": "vertical",
"length": 100,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 95,
"y": 100,
@@ -214,6 +340,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=back
{
"direction": "horizontal",
"length": 292,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 4,
"y": 2,
@@ -224,6 +356,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=back
{
"direction": "horizontal",
"length": 310,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": -10,
"y": -10,
@@ -234,6 +372,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=back
{
"direction": "horizontal",
"length": 300,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 0,
"y": 0,
@@ -244,6 +388,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=back
{
"direction": "horizontal",
"length": 100,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 100,
"y": 95,
@@ -254,6 +404,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=forw
{
"direction": "horizontal",
"length": 292,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 4,
"y": 298,
@@ -264,6 +420,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=forw
{
"direction": "horizontal",
"length": 310,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": -10,
"y": 300,
@@ -274,6 +436,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=forw
{
"direction": "horizontal",
"length": 300,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 0,
"y": 300,
@@ -284,6 +452,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'mixed' } direction=forw
{
"direction": "horizontal",
"length": 100,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 100,
"y": 205,
@@ -294,6 +468,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: fal
{
"direction": "horizontal",
"length": 292,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 4,
"y": 2,
@@ -304,6 +484,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: fal
{
"direction": "horizontal",
"length": 310,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": -10,
"y": -10,
@@ -314,6 +500,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: fal
{
"direction": "horizontal",
"length": 300,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 0,
"y": 0,
@@ -324,6 +516,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: fal
{
"direction": "horizontal",
"length": 100,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 100,
"y": 95,
@@ -334,6 +532,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: fal
{
"direction": "horizontal",
"length": 292,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 4,
"y": 298,
@@ -344,6 +548,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: fal
{
"direction": "horizontal",
"length": 310,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": -10,
"y": 300,
@@ -354,6 +564,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: fal
{
"direction": "horizontal",
"length": 300,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 0,
"y": 300,
@@ -364,6 +580,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: fal
{
"direction": "horizontal",
"length": 100,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 100,
"y": 205,
@@ -374,6 +596,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: tru
{
"direction": "horizontal",
"length": 292,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 4,
"y": 298,
@@ -384,6 +612,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: tru
{
"direction": "horizontal",
"length": 310,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": -10,
"y": 300,
@@ -394,6 +628,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: tru
{
"direction": "horizontal",
"length": 300,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 0,
"y": 300,
@@ -404,6 +644,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: tru
{
"direction": "horizontal",
"length": 100,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 100,
"y": 205,
@@ -414,6 +660,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: tru
{
"direction": "horizontal",
"length": 292,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 4,
"y": 2,
@@ -424,6 +676,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: tru
{
"direction": "horizontal",
"length": 310,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": -10,
"y": -10,
@@ -434,6 +692,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: tru
{
"direction": "horizontal",
"length": 300,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 0,
"y": 0,
@@ -444,6 +708,12 @@ exports[`getPlacementNextTo childrenOrientation={ type: 'vertical', reverse: tru
{
"direction": "horizontal",
"length": 100,
+ "parentRect": {
+ "height": 300,
+ "left": 0,
+ "top": 0,
+ "width": 300,
+ },
"type": "next-to-child",
"x": 100,
"y": 95,
diff --git a/packages/design-system/src/components/primitives/dnd/geometry-utils.test.ts b/packages/design-system/src/components/primitives/dnd/geometry-utils.test.ts
index 20dbacf89e..4dfd143651 100644
--- a/packages/design-system/src/components/primitives/dnd/geometry-utils.test.ts
+++ b/packages/design-system/src/components/primitives/dnd/geometry-utils.test.ts
@@ -153,13 +153,16 @@ describe("getArea", () => {
describe("getPlacementBetween", () => {
const size = { width: 100, height: 100 };
+ const parentRect = { top: 0, left: 0, width: 0, height: 0 };
test.each([
[
// [a] | [b]
+ parentRect,
{ top: 0, left: 0 },
{ top: 0, left: 200 },
{
+ parentRect,
type: "between-children",
direction: "vertical",
x: 150,
@@ -172,9 +175,11 @@ describe("getPlacementBetween", () => {
// [a]
// ---
// [b]
+ parentRect,
{ top: 0, left: 0 },
{ top: 200, left: 0 },
{
+ parentRect,
type: "between-children",
direction: "horizontal",
x: 0,
@@ -186,6 +191,7 @@ describe("getPlacementBetween", () => {
[
// [a]
// [b]
+ parentRect,
{ top: 0, left: 600 },
{ top: 200, left: 0 },
undefined,
@@ -195,27 +201,29 @@ describe("getPlacementBetween", () => {
// [a]
//
// [b]
+ parentRect,
{ top: 0, left: 0 },
{ top: 600, left: 200 },
undefined,
],
// overlaps ...
- [{ top: 0, left: 0 }, { top: 0, left: 0 }, undefined],
- [{ top: 0, left: 0 }, { top: 50, left: 50 }, undefined],
- [{ top: 0, left: 50 }, { top: 50, left: 0 }, undefined],
+ [parentRect, { top: 0, left: 0 }, { top: 0, left: 0 }, undefined],
+ [parentRect, { top: 0, left: 0 }, { top: 50, left: 50 }, undefined],
+ [parentRect, { top: 0, left: 50 }, { top: 50, left: 0 }, undefined],
[
+ parentRect,
{ top: 0, left: 0, width: 200, height: 200 },
{ top: 50, left: 50 },
undefined,
],
- ])("%o + %o", (a, b, placement) => {
- expect(getPlacementBetween({ ...size, ...a }, { ...size, ...b })).toEqual(
- placement
- );
- expect(getPlacementBetween({ ...size, ...b }, { ...size, ...a })).toEqual(
- placement
- );
+ ])("%o + %o", (parentRect, a, b, placement) => {
+ expect(
+ getPlacementBetween(parentRect, { ...size, ...a }, { ...size, ...b })
+ ).toEqual(placement);
+ expect(
+ getPlacementBetween(parentRect, { ...size, ...b }, { ...size, ...a })
+ ).toEqual(placement);
});
});
diff --git a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts b/packages/design-system/src/components/primitives/dnd/geometry-utils.ts
index 65497d1394..104cec063d 100644
--- a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts
+++ b/packages/design-system/src/components/primitives/dnd/geometry-utils.ts
@@ -12,6 +12,7 @@ export type ChildrenOrientation =
| { type: "mixed"; reverse?: boolean };
export type Placement = {
+ parentRect: Rect;
type: "between-children" | "next-to-child" | "inside-parent";
x: number;
y: number;
@@ -69,6 +70,7 @@ export const getArea = (
};
export const getPlacementBetween = (
+ parentRect: Rect,
a: Rect | undefined,
b: Rect | undefined
): Placement | undefined => {
@@ -96,6 +98,7 @@ export const getPlacementBetween = (
const minX = Math.min(a.left, b.left);
const maxX = Math.max(a.left + a.width, b.left + b.width);
return {
+ parentRect,
type: "between-children",
y: firstY.top + firstY.height + distanceY / 2,
x: minX,
@@ -107,6 +110,7 @@ export const getPlacementBetween = (
const minY = Math.min(a.top, b.top);
const maxY = Math.max(a.top + a.height, b.top + b.height);
return {
+ parentRect,
type: "between-children",
y: minY,
x: firstX.left + firstX.width + distanceX / 2,
@@ -144,6 +148,7 @@ export const getPlacementNextTo = (
if (side === "top") {
return {
+ parentRect,
type: "next-to-child",
x: rect.left,
y: rect.top - getMargin(rect.top),
@@ -154,6 +159,7 @@ export const getPlacementNextTo = (
if (side === "bottom") {
return {
+ parentRect,
type: "next-to-child",
x: rect.left,
y:
@@ -167,6 +173,7 @@ export const getPlacementNextTo = (
if (side === "left") {
return {
+ parentRect,
type: "next-to-child",
x: rect.left - getMargin(rect.left),
y: rect.top,
@@ -176,6 +183,7 @@ export const getPlacementNextTo = (
}
return {
+ parentRect,
type: "next-to-child",
x:
rect.left +
@@ -195,6 +203,7 @@ export const getPlacementInside = (
if (childrenOrientation.type === "horizontal") {
const safePadding = Math.min(parentRect.width / 2, padding);
return {
+ parentRect,
type: "inside-parent",
y: parentRect.top + safePadding,
x: parentRect.left + safePadding,
@@ -205,6 +214,7 @@ export const getPlacementInside = (
const safePadding = Math.min(parentRect.height / 2, padding);
return {
+ parentRect,
type: "inside-parent",
y: parentRect.top + safePadding,
x: parentRect.left + safePadding,
diff --git a/packages/design-system/src/components/primitives/dnd/placement-indicator.tsx b/packages/design-system/src/components/primitives/dnd/placement-indicator.tsx
index 18d0e5a1cc..bccfe27f28 100644
--- a/packages/design-system/src/components/primitives/dnd/placement-indicator.tsx
+++ b/packages/design-system/src/components/primitives/dnd/placement-indicator.tsx
@@ -80,6 +80,7 @@ export const computeIndicatorPlacement = (
children[neighbourChildIndex]?.getBoundingClientRect();
let placementIndicator = getPlacementBetween(
+ parentRect,
closestChildRect,
neighbourChildRect
);
diff --git a/packages/design-system/src/components/primitives/dnd/use-hold.ts b/packages/design-system/src/components/primitives/dnd/use-hold.ts
index 5097baf4d2..c41cde3ec5 100644
--- a/packages/design-system/src/components/primitives/dnd/use-hold.ts
+++ b/packages/design-system/src/components/primitives/dnd/use-hold.ts
@@ -1,6 +1,7 @@
-import { useMemo, useRef } from "react";
+import { useEffect, useMemo, useRef } from "react";
type UseHoldProps<Data> = {
+ data: Data;
isEqual: (a: Data, b: Data) => boolean;
holdTimeThreshold: number;
onHold: (data: Data) => void;
@@ -18,30 +19,30 @@ export const useHold = <Data>(props: UseHoldProps<Data>) => {
// So we can put them in a ref and make useMemo below very efficient.
const latestProps = useRef<UseHoldProps<Data>>(props);
- // We want to return a stable object to avoid re-renders when it's a dependency
- return useMemo(
- () => ({
- setData(data: Data) {
- const { currentData } = state.current;
- const { isEqual, holdTimeThreshold } = latestProps.current;
+ useEffect(() => {
+ const data = props.data;
+ const { currentData } = state.current;
+ const { isEqual, holdTimeThreshold } = latestProps.current;
- if (currentData !== undefined && isEqual(currentData, data)) {
- return;
- }
+ if (currentData !== undefined && isEqual(currentData, data)) {
+ return;
+ }
- clearTimeout(state.current.timeoutId);
+ clearTimeout(state.current.timeoutId);
- state.current.timeoutId = setTimeout(() => {
- state.current.timeoutId = undefined;
- latestProps.current.onHold(data);
- }, holdTimeThreshold);
- },
+ state.current.timeoutId = setTimeout(() => {
+ state.current.timeoutId = undefined;
+ latestProps.current.onHold(data);
+ }, holdTimeThreshold);
+ }, [props.data]);
+ // We want to return a stable object to avoid re-renders when it's a dependency
+ return useMemo(() => {
+ return {
reset() {
clearTimeout(state.current.timeoutId);
state.current = { currentData: undefined, timeoutId: undefined };
},
- }),
- []
- );
+ };
+ }, []);
};
diff --git a/packages/design-system/src/components/tree/horizontal-shift.test.ts b/packages/design-system/src/components/tree/horizontal-shift.test.ts
index 1f309326ce..4ec0d02def 100644
--- a/packages/design-system/src/components/tree/horizontal-shift.test.ts
+++ b/packages/design-system/src/components/tree/horizontal-shift.test.ts
@@ -61,6 +61,7 @@ const tree: Item = {
const makePlacementIndicator = (
type: Placement["type"] = "next-to-child"
): Placement => ({
+ parentRect: { width: 0, height: 0, left: 0, top: 0 },
type,
direction: "horizontal",
x: 0,
diff --git a/packages/design-system/src/components/tree/index.ts b/packages/design-system/src/components/tree/index.ts
index 173fbc95cf..02d27b02dd 100644
--- a/packages/design-system/src/components/tree/index.ts
+++ b/packages/design-system/src/components/tree/index.ts
@@ -1,3 +1,3 @@
-export type { ItemSelector } from "./item-utils";
+export type { ItemSelector, ItemDropTarget } from "./item-utils";
export * from "./tree";
export * from "./tree-node";
diff --git a/packages/design-system/src/components/tree/tree.stories.tsx b/packages/design-system/src/components/tree/tree.stories.tsx
index 70b53f1674..d2edff529c 100644
--- a/packages/design-system/src/components/tree/tree.stories.tsx
+++ b/packages/design-system/src/components/tree/tree.stories.tsx
@@ -4,7 +4,7 @@ import { Tree } from "./tree";
import { findItemById, Item, reparent } from "./test-tree-data";
import { Flex } from "../flex";
import { TreeItemLabel, TreeItemBody } from "./tree-node";
-import type { ItemSelector } from "./item-utils";
+import type { ItemDropTarget, ItemSelector } from "./item-utils";
export const StressTest = ({ animate }: { animate: boolean }) => {
const [root, setRoot] = useState<Item>((): Item => {
@@ -69,6 +69,10 @@ export const StressTest = ({ animate }: { animate: boolean }) => {
const [selectedItemSelector, setSelectedItemSelector] = useState<
undefined | ItemSelector
>();
+ const [dropTarget, setDropTarget] = useState<undefined | ItemDropTarget>();
+ const [dragItemSelector, setDragItemSelector] = useState<
+ undefined | ItemSelector
+ >();
return (
<Flex css={{ width: 300, height: 500, flexDirection: "column" }}>
@@ -81,7 +85,11 @@ export const StressTest = ({ animate }: { animate: boolean }) => {
animate={animate}
root={root}
selectedItemSelector={selectedItemSelector}
+ dragItemSelector={dragItemSelector}
+ dropTarget={dropTarget}
onSelect={setSelectedItemSelector}
+ onDropTargetChange={setDropTarget}
+ onDragItemChange={setDragItemSelector}
renderItem={(props) => (
<TreeItemBody {...props}>
<TreeItemLabel>{props.itemData.id}</TreeItemLabel>
diff --git a/packages/design-system/src/components/tree/tree.tsx b/packages/design-system/src/components/tree/tree.tsx
index cdeddaab86..03bce8e5c4 100644
--- a/packages/design-system/src/components/tree/tree.tsx
+++ b/packages/design-system/src/components/tree/tree.tsx
@@ -4,7 +4,6 @@ import { createPortal } from "react-dom";
import { ListPositionIndicator } from "../list-position-indicator";
import { TreeNode, INDENT, TreeItemRenderProps } from "./tree-node";
import {
- type Placement,
useHold,
useDrop,
useDrag,
@@ -26,6 +25,8 @@ import {
export type TreeProps<Data extends { id: string }> = {
root: Data;
selectedItemSelector: undefined | ItemSelector;
+ dragItemSelector: undefined | ItemSelector;
+ dropTarget: undefined | ItemDropTarget;
canLeaveParent: (itemId: ItemId) => boolean;
canAcceptChild: (itemId: ItemId) => boolean;
@@ -35,6 +36,8 @@ export type TreeProps<Data extends { id: string }> = {
onSelect?: (itemSelector: ItemSelector) => void;
onHover?: (itemSelector: undefined | ItemSelector) => void;
animate?: boolean;
+ onDropTargetChange: (dropTarget: ItemDropTarget) => void;
+ onDragItemChange: (itemSelector: ItemSelector) => void;
onDragEnd: (event: {
itemSelector: ItemSelector;
dropTarget: { itemSelector: ItemSelector; position: number | "end" };
@@ -57,6 +60,8 @@ const sharedDropOptions = {
export const Tree = <Data extends { id: string }>({
root,
selectedItemSelector,
+ dragItemSelector,
+ dropTarget,
canLeaveParent,
canAcceptChild,
getItemChildren,
@@ -64,6 +69,8 @@ export const Tree = <Data extends { id: string }>({
onSelect,
onHover,
animate,
+ onDropTargetChange,
+ onDragItemChange,
onDragEnd,
}: TreeProps<Data>) => {
const { getIsExpanded, setIsExpanded } = useExpandState({
@@ -73,13 +80,23 @@ export const Tree = <Data extends { id: string }>({
const rootRef = useRef<HTMLElement | null>(null);
- const [dragItemSelector, setDragItemSelector] = useState<
- undefined | ItemSelector
- >();
- const [dropTarget, setDropTarget] = useState<undefined | ItemDropTarget>();
- const [placementIndicator, setPlacementIndicator] = useState<
- undefined | Placement
- >();
+ const placementIndicator = useMemo(() => {
+ if (dropTarget === undefined) {
+ return;
+ }
+ const element = getElementByItemSelector(
+ rootRef.current ?? undefined,
+ dropTarget.itemSelector
+ );
+ if (element === undefined) {
+ return;
+ }
+ return computeIndicatorPlacement({
+ placement: dropTarget.placement,
+ element,
+ ...sharedDropOptions,
+ });
+ }, [dropTarget]);
const getDropTargetElement = useCallback(
(id: string): HTMLElement | null | undefined =>
@@ -104,10 +121,14 @@ export const Tree = <Data extends { id: string }>({
};
};
- const useHoldHandler = useHold<ItemDropTarget>({
- isEqual: (a, b) => areItemSelectorsEqual(a.itemSelector, b.itemSelector),
+ const useHoldHandler = useHold({
+ data: dropTarget,
+ isEqual: (a, b) => areItemSelectorsEqual(a?.itemSelector, b?.itemSelector),
holdTimeThreshold: 600,
onHold: (dropTarget) => {
+ if (dropTarget === undefined) {
+ return;
+ }
const [itemId] = dropTarget.itemSelector;
if (
getItemChildren(itemId).length > 0 ||
@@ -177,14 +198,7 @@ export const Tree = <Data extends { id: string }>({
indexWithinChildren: dropTarget.indexWithinChildren,
placement: dropTarget.placement,
};
- useHoldHandler.setData(itemDropTarget);
- setDropTarget(itemDropTarget);
- const placementIndicator = computeIndicatorPlacement({
- placement: dropTarget.placement,
- element: dropTarget.element,
- ...sharedDropOptions,
- });
- setPlacementIndicator(placementIndicator);
+ onDropTargetChange(itemDropTarget);
},
});
@@ -207,7 +221,7 @@ export const Tree = <Data extends { id: string }>({
},
onStart: ({ data: itemSelector }) => {
onSelect?.(itemSelector);
- setDragItemSelector(itemSelector);
+ onDragItemChange(itemSelector);
dropHandlers.handleStart();
autoScrollHandlers.setEnabled(true);
},
@@ -231,8 +245,6 @@ export const Tree = <Data extends { id: string }>({
autoScrollHandlers.setEnabled(false);
setHorizontalShift(0);
- setDragItemSelector(undefined);
- setDropTarget(undefined);
dropHandlers.handleEnd({ isCanceled });
useHoldHandler.reset();
},