Skip to content

Commit f339bda

Browse files
committed
fix(verticalCompact): 'isUserAction' not properly reset after main collision
Fixes react-grid-layout#714, react-grid-layout#720, react-grid-layout#729 Thanks @alexanderzobnin
1 parent 653a625 commit f339bda

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

lib/utils.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ export function moveElementAwayFromCollision(
445445
// We only do this on the main collision as this can get funky in cascades and cause
446446
// unwanted swapping behavior.
447447
if (isUserAction) {
448+
// Reset isUserAction flag because we're not in the main collision anymore.
449+
isUserAction = false;
450+
448451
// Make a mock item so we don't modify the item here, only modify in moveElement.
449452
const fakeItem: LayoutItem = {
450453
x: compactH ? Math.max(collidesWith.x - itemToMove.w, 0) : itemToMove.x,
@@ -454,17 +457,8 @@ export function moveElementAwayFromCollision(
454457
i: "-1"
455458
};
456459

457-
// This makes it feel a bit more precise by waiting to swap for just a bit when moving up.
458-
const [axis, dimension] =
459-
compactType === "horizontal" ? ["x", "w"] : ["y", "h"];
460-
const shouldSkip =
461-
false &&
462-
// Our collision is below the item to move, and only encroaches by 25% of its dimension; ignore
463-
(collidesWith[axis] > itemToMove[axis] &&
464-
collidesWith[axis] - itemToMove[axis] > itemToMove[dimension] / 4);
465-
466460
// No collision? If so, we can go up there; otherwise, we'll end up moving down as normal
467-
if (!shouldSkip && !getFirstCollision(layout, fakeItem)) {
461+
if (!getFirstCollision(layout, fakeItem)) {
468462
log(
469463
`Doing reverse collision on ${itemToMove.i} up to [${fakeItem.x},${
470464
fakeItem.y

test/spec/utils-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,36 @@ describe("moveElement", () => {
265265
]
266266
);
267267
});
268+
269+
it("Moves one element to another should cause moving down panels below when compaction is vertical", () => {
270+
const layout = [
271+
{ x: 0, y: 0, w: 2, h: 1, i: "A" },
272+
{ x: 2, y: 0, w: 2, h: 1, i: "B" },
273+
{ x: 0, y: 1, w: 1, h: 1, i: "C" },
274+
{ x: 1, y: 1, w: 3, h: 1, i: "D" }
275+
];
276+
// move B left slightly so it collides with A; can cause C to jump above A
277+
// this test will check that that does not happen
278+
const itemB = layout[1];
279+
assert.deepEqual(
280+
moveElement(
281+
layout,
282+
itemB,
283+
1,
284+
0, // x, y
285+
true,
286+
false, // isUserAction, preventCollision
287+
"vertical",
288+
4 // compactType, cols
289+
),
290+
[
291+
{ x: 0, y: 1, w: 2, h: 1, i: "A", moved: true },
292+
{ x: 1, y: 0, w: 2, h: 1, i: "B", moved: true },
293+
{ x: 0, y: 2, w: 1, h: 1, i: "C", moved: true },
294+
{ x: 1, y: 2, w: 3, h: 1, i: "D", moved: true }
295+
]
296+
);
297+
});
268298
});
269299

270300
describe("compact vertical", () => {

0 commit comments

Comments
 (0)