Skip to content

Commit 84307c1

Browse files
committed
refactor: disable fallback collisions temporarily on zone change
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.
1 parent ad96451 commit 84307c1

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

packages/core/components/DragDropContext/index.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { ComponentDndData } from "../DraggableComponent";
2626
import { isElement } from "@dnd-kit/dom/utilities";
2727

2828
import { PointerSensor } from "./PointerSensor";
29+
import { collisionStore } from "../DraggableComponent/collision/dynamic/store";
2930

3031
const DEBUG = false;
3132

@@ -95,6 +96,13 @@ const DragDropContextClient = ({
9596

9697
useZoneStore.setState({ zoneDepthIndex, areaDepthIndex });
9798

99+
// Disable fallback collisions temporarily after zone change, as
100+
// these can cause unexpected collisions
101+
collisionStore.setState({ fallbackEnabled: false });
102+
setTimeout(() => {
103+
collisionStore.setState({ fallbackEnabled: true });
104+
}, 500);
105+
98106
setTimeout(() => {
99107
// Force update after debounce
100108
manager.collisionObserver.forceUpdate(true);

packages/core/components/DraggableComponent/collision/dynamic/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { trackMovementInterval } from "./track-movement-interval";
1212
import { collisionDebug } from "../collision-debug";
1313
import { closestCorners } from "@dnd-kit/collision";
1414
import { DragAxis, Direction } from "../../../../types";
15+
import { collisionStore } from "./store";
1516

1617
export type CollisionMap = Record<
1718
string,
@@ -59,6 +60,8 @@ export const createDynamicCollisionDetector = (
5960

6061
const { center: dragCenter } = dragShape;
6162

63+
const { fallbackEnabled } = collisionStore.getState();
64+
6265
const interval = trackMovementInterval(position.current, dragAxis);
6366

6467
dragOperation.data = {
@@ -128,7 +131,7 @@ export const createDynamicCollisionDetector = (
128131
return { ...collision, id: shouldFlushId ? "flush" : collision.id };
129132
}
130133

131-
if (dragOperation.source?.id !== droppable.id) {
134+
if (fallbackEnabled && dragOperation.source?.id !== droppable.id) {
132135
// Only calculate fallbacks when the draggable sits within the droppable's axis projection
133136
const xAxisIntersection =
134137
dropShape.boundingRectangle.right > dragShape.boundingRectangle.left &&
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createStore } from "zustand/vanilla";
2+
3+
export const collisionStore = createStore<{
4+
fallbackEnabled: boolean;
5+
}>(() => ({
6+
fallbackEnabled: false,
7+
}));

0 commit comments

Comments
 (0)