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.

implements onHold

Oleg Isonenf25d34983ce0
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 5ce61817c1..8b0646f2ea 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
@@ -1,5 +1,5 @@
import { ComponentStory, ComponentMeta } from "@storybook/react";
-import { useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { Box } from "../../box";
import { useDropTarget, type Area } from "./use-drop-target";
import { useDrag } from "./use-drag";
@@ -9,6 +9,11 @@ type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;
export const Playground = () => {
const [dropTargetRect, setDropTargetRect] = useState<Rect>();
const [placementIndicatorRect, setPlacementIndicatorRect] = useState<Rect>();
+ const holdRef = useRef<HTMLElement>();
+
+ const handleHoldEnd = () => {
+ holdRef.current?.style.removeProperty("background");
+ };
const { rootRef, handleMove } = useDropTarget({
isDropTarget(element: HTMLElement) {
@@ -24,6 +29,11 @@ export const Playground = () => {
});
console.log("area", area);
},
+ onHold({ target }: { target: HTMLElement }) {
+ handleHoldEnd();
+ holdRef.current = target;
+ target.style.background = "red";
+ },
});
const dragProps = useDrag({
@@ -36,6 +46,7 @@ export const Playground = () => {
onEnd() {
setDropTargetRect(undefined);
setPlacementIndicatorRect(undefined);
+ handleHoldEnd();
},
});
@@ -91,8 +102,8 @@ const PlacementIndicator = ({ rect }: { rect?: Rect }) => {
left: rect.left,
width: rect.width,
height: rect.height,
- position: "absolute",
- background: "green",
- } as const;
- return <Box style={style} />;
+ };
+ return (
+ <Box style={style} css={{ position: "absolute", background: "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 db6c97c94b..9f46118dc1 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
@@ -2,15 +2,29 @@ import { useRef } from "react";
export type Area = "top" | "right" | "bottom" | "left" | "center";
+type UseDropTarget = any;
+
export const useDropTarget = ({
isDropTarget,
+ edgeDistanceThreshold = 3,
+ holdTimeThreshold = 500,
onDropTargetChange,
- edgeThreshold = 3,
-}: any) => {
+ onHold,
+}: UseDropTarget) => {
const rootRef = useRef<HTMLElement | null>(null);
const targetRef = useRef<HTMLElement>();
const targetRectRef = useRef<DOMRect>();
const areaRef = useRef<Area>("center");
+ const holdTimeoutRef = useRef<NodeJS.Timeout>();
+
+ const checkHold = (target: HTMLElement) => {
+ clearTimeout(holdTimeoutRef.current);
+ holdTimeoutRef.current = setTimeout(() => {
+ if (target === targetRef.current) {
+ onHold({ target });
+ }
+ }, holdTimeThreshold);
+ };
return {
handleMove(pointerCoordinate: Coordinate) {
@@ -30,11 +44,12 @@ export const useDropTarget = ({
if (hasTargetChanged) {
targetRectRef.current = nextTarget.getBoundingClientRect();
+ checkHold(nextTarget);
}
const nextArea = getArea(
pointerCoordinate,
- edgeThreshold,
+ edgeDistanceThreshold,
targetRectRef.current
);
@@ -64,16 +79,20 @@ const elementFromPoint = (coordinate: Coordinate): HTMLElement | undefined => {
const getArea = (
pointerCoordinate: Coordinate,
- edgeThreshold: number,
+ edgeDistanceThreshold: number,
targetRect?: DOMRect
) => {
let area: Area = "center";
if (targetRect === undefined) return area;
// We are at the edge and this means user wants to insert after that element into its parent
- if (pointerCoordinate.y - targetRect.top <= edgeThreshold) area = "top";
- if (targetRect.bottom - pointerCoordinate.y <= edgeThreshold) area = "bottom";
- if (pointerCoordinate.x - targetRect.left <= edgeThreshold) area = "left";
- if (targetRect.right - pointerCoordinate.x <= edgeThreshold) area = "right";
+ if (pointerCoordinate.y - targetRect.top <= edgeDistanceThreshold)
+ area = "top";
+ if (targetRect.bottom - pointerCoordinate.y <= edgeDistanceThreshold)
+ area = "bottom";
+ if (pointerCoordinate.x - targetRect.left <= edgeDistanceThreshold)
+ area = "left";
+ if (targetRect.right - pointerCoordinate.x <= edgeDistanceThreshold)
+ area = "right";
return area;
};