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
simplify hook api & cleanup
Roman Pominov0a1457807289
diff --git a/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx b/packages/design-system/src/components/primitives/dnd/canvas.stories.tsxindex 88160e1940..28ea0e4ed7 100644--- a/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx+++ b/packages/design-system/src/components/primitives/dnd/canvas.stories.tsx@@ -151,7 +151,37 @@ export const Canvas = () => {},{ id: "4", style: {}, children: [], acceptsChildren: true },{ id: "5", style: {}, children: [], acceptsChildren: true },- { id: "6", style: {}, children: [], acceptsChildren: false },+ {+ id: "6",+ style: { background: "whitesmoke" },+ children: [],+ acceptsChildren: false,+ },+ {+ id: "7",+ style: { display: "flex" },+ children: [+ {+ id: "8",+ style: { margin: 0, background: "#ff7878" },+ children: [],+ acceptsChildren: true,+ },+ {+ id: "9",+ style: { margin: 0, background: "#a8d1ff" },+ children: [],+ acceptsChildren: true,+ },+ {+ id: "10",+ style: { margin: 0, background: "#94ef94" },+ children: [],+ acceptsChildren: true,+ },+ ],+ acceptsChildren: true,+ },]);const [currentDropTarget, setCurrentDropTarget] = useState<@@ -161,6 +191,13 @@ export const Canvas = () => {const rootRef = useRef<HTMLElement | null>(null);+ const getDefaultDropTarget = () => {+ if (rootRef.current === null) {+ throw new Error("should not happen");+ }+ return { data: ROOT_ID, element: rootRef.current };+ };+const dropHandlers = useDrop<string>({edgeDistanceThreshold: 10,@@ -168,17 +205,10 @@ export const Canvas = () => {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;+ if (dropTarget === undefined || rootElement === null) {+ return getDefaultDropTarget();}const { data: id, nearEdge } = dropTarget;@@ -202,13 +232,13 @@ export const Canvas = () => {const newItem = path.find((item) => item.acceptsChildren);if (newItem === undefined) {- return { data: ROOT_ID, element: rootElement };+ return getDefaultDropTarget();}const element = idToElement(rootElement, newItem.id);if (element === undefined) {- return { data: ROOT_ID, element: rootElement };+ return getDefaultDropTarget();}return { data: newItem.id, element };diff --git a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts b/packages/design-system/src/components/primitives/dnd/geometry-utils.tsindex b0a3f05a7b..0d947858d2 100644--- a/packages/design-system/src/components/primitives/dnd/geometry-utils.ts+++ b/packages/design-system/src/components/primitives/dnd/geometry-utils.ts@@ -2,6 +2,13 @@ export type Rect = Pick<DOMRect, "top" | "left" | "width" | "height">;export type ChildrenOrientation = "vertical" | "horizontal" | "mixed";+export type Placement = {+ x: number;+ y: number;+ length: number;+ direction: "horizontal" | "vertical";+};+// https://stackoverflow.com/a/18157551/478603const getDistanceToRect = (rect: Rect, { x, y }: { x: number; y: number }) => {const dx = Math.max(rect.left - x, 0, x - (rect.left + rect.width));@@ -44,13 +51,6 @@ export const isNearEdge = (rect.left + rect.width - x) <= edgeDistanceThreshold;-export type Placement = {- x: number;- y: number;- length: number;- direction: "horizontal" | "vertical";-};-export const getPlacementBetween = (a: Rect | undefined,b: Rect | undefined@@ -196,7 +196,7 @@ export const getRectsOrientation = (return "mixed";};-// Checks whether we should place item before or after the closest child.+// Determines whether we should place the item before or after the closest child.// Returns the number that should be added to the closest child index to get the final index.export const getIndexAdjustment = (pointer: { x: number; y: number },diff --git a/packages/design-system/src/components/primitives/dnd/use-drop.ts b/packages/design-system/src/components/primitives/dnd/use-drop.tsindex 766afc6122..509a12fbf6 100644--- a/packages/design-system/src/components/primitives/dnd/use-drop.ts+++ b/packages/design-system/src/components/primitives/dnd/use-drop.ts@@ -73,19 +73,16 @@ export type UseDropProps<Data> = {// To check that the element can qualify as a targetisDropTarget: (target: UsableElement) => Data | false;- // A target to use when no suitable element is found under the pointer,- // or swapDropTarget returns "DEFAULT"- getDefaultDropTarget: () => PartialDropTarget<Data>;-// Distance from an edge to set nearEdge to true in swapDropTargetedgeDistanceThreshold?: number;// Given the potential target that has passed the isDropTarget check,// and the position of the pointer on the target,// you can swap to another target- swapDropTarget?: (- dropTarget: PartialDropTarget<Data> & { nearEdge: boolean }- ) => PartialDropTarget<Data> | "DEFAULT";+ swapDropTarget: (+ // undefined is passed when no suitable element is found under the pointer+ dropTarget: (PartialDropTarget<Data> & { nearEdge: boolean }) | undefined+ ) => PartialDropTarget<Data>;onDropTargetChange: (dropTarget: DropTarget<Data>) => void;};@@ -234,31 +231,34 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {const {edgeDistanceThreshold = 3,isDropTarget,- swapDropTarget = (x) => x,- getDefaultDropTarget,+ swapDropTarget,} = latestProps.current;const { pointerCoordinates, root } = state.current;- if (pointerCoordinates === undefined || root === undefined) {- setDropTarget(getDefaultDropTarget());- return;- }// @todo: Cache this?// Not expensive by itself, but it may call isDropTarget multiple times.- let candidate = findClosestDropTarget({- root,- initialElement: toUseableElement(- document.elementFromPoint(pointerCoordinates.x, pointerCoordinates.y)- ),- isDropTarget,- });+ let candidate =+ root &&+ findClosestDropTarget({+ root,+ initialElement:+ pointerCoordinates &&+ toUseableElement(+ document.elementFromPoint(+ pointerCoordinates.x,+ pointerCoordinates.y+ )+ ),+ isDropTarget,+ });const isNewCandidate =candidate?.element !== state.current.lastCandidateElement;state.current.lastCandidateElement = candidate?.element;const candidateIsNearEdge =candidate &&+ pointerCoordinates &&isNearEdge(pointerCoordinates,edgeDistanceThreshold,@@ -273,28 +273,21 @@ export const useDrop = <Data>(props: UseDropProps<Data>): UseDropHandlers => {return;}- if (candidate === undefined) {- setDropTarget(getDefaultDropTarget());- return;- }-// eslint-disable-next-line no-constant-conditionwhile (true) {- const swappedTo = swapDropTarget({- ...candidate,- nearEdge: isNearEdge(- pointerCoordinates,- edgeDistanceThreshold,- candidate.element.getBoundingClientRect()- ),- });-- if (swappedTo === "DEFAULT") {- setDropTarget(getDefaultDropTarget());- return;- }+ const swappedTo = swapDropTarget(+ candidate &&+ pointerCoordinates && {+ ...candidate,+ nearEdge: isNearEdge(+ pointerCoordinates,+ edgeDistanceThreshold,+ candidate.element.getBoundingClientRect()+ ),+ }+ );- if (swappedTo.element === candidate.element) {+ if (swappedTo.element === candidate?.element) {setDropTarget(candidate);return;}