Skip to content

Commit

Permalink
refactor: disable fallback collisions temporarily on zone change
Browse files Browse the repository at this point in the history
This addresses an issue where the collision would jump after a zone change, noted as part of original #598 implementation and made more frequent in #767 after performance improvements.
  • Loading branch information
chrisvxd committed Jan 6, 2025
1 parent 566d94b commit be6e137
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/core/components/DragDropContext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ComponentDndData } from "../DraggableComponent";
import { isElement } from "@dnd-kit/dom/utilities";

import { PointerSensor } from "./PointerSensor";
import { collisionStore } from "../DraggableComponent/collision/dynamic/store";

const DEBUG = false;

Expand Down Expand Up @@ -95,6 +96,13 @@ const DragDropContextClient = ({

useZoneStore.setState({ zoneDepthIndex, areaDepthIndex });

// Disable fallback collisions temporarily after zone change, as
// these can cause unexpected collisions
collisionStore.setState({ fallbackEnabled: false });
setTimeout(() => {
collisionStore.setState({ fallbackEnabled: true });
}, 500);

setTimeout(() => {
// Force update after debounce
manager.collisionObserver.forceUpdate(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { trackMovementInterval } from "./track-movement-interval";
import { collisionDebug } from "../collision-debug";
import { closestCorners } from "@dnd-kit/collision";
import { DragAxis, Direction } from "../../../../types";
import { collisionStore } from "./store";

export type CollisionMap = Record<
string,
Expand Down Expand Up @@ -59,6 +60,8 @@ export const createDynamicCollisionDetector = (

const { center: dragCenter } = dragShape;

const { fallbackEnabled } = collisionStore.getState();

const interval = trackMovementInterval(position.current, dragAxis);

dragOperation.data = {
Expand Down Expand Up @@ -128,7 +131,7 @@ export const createDynamicCollisionDetector = (
return { ...collision, id: shouldFlushId ? "flush" : collision.id };
}

if (dragOperation.source?.id !== droppable.id) {
if (fallbackEnabled && dragOperation.source?.id !== droppable.id) {
// Only calculate fallbacks when the draggable sits within the droppable's axis projection
const xAxisIntersection =
dropShape.boundingRectangle.right > dragShape.boundingRectangle.left &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createStore } from "zustand/vanilla";

export const collisionStore = createStore<{
fallbackEnabled: boolean;
}>(() => ({
fallbackEnabled: false,
}));

0 comments on commit be6e137

Please sign in to comment.