MCPcopy Index your code
hub / github.com/react-grid-layout/react-grid-layout / resolveCompactionCollision

Function resolveCompactionCollision

src/core/compactors.ts:38–76  ·  view source on GitHub ↗
(
  layout: Layout,
  item: LayoutItem,
  moveToCoord: number,
  axis: "x" | "y",
  hasStatics?: boolean
)

Source from the content-addressed store, hash-verified

36 * @param hasStatics - Whether layout contains static items (disables early break optimization)
37 */
38export function resolveCompactionCollision(
39 layout: Layout,
40 item: LayoutItem,
41 moveToCoord: number,
42 axis: "x" | "y",
43 hasStatics?: boolean
44): void {
45 const sizeProp = axis === "x" ? "w" : "h";
46
47 // Temporarily increment position to check for collisions
48 (item as Mutable<LayoutItem>)[axis] += 1;
49
50 const itemIndex = layout.findIndex(l => l.i === item.i);
51
52 // Calculate hasStatics once if not provided (for backwards compat)
53 const layoutHasStatics = hasStatics ?? getStatics(layout).length > 0;
54
55 for (let i = itemIndex + 1; i < layout.length; i++) {
56 const otherItem = layout[i];
57 if (otherItem === undefined) continue;
58 if (otherItem.static) continue;
59 // Optimization: break early if past this element, but only if no statics
60 // are present. Static items can be scattered throughout the layout,
61 // so we can't assume sort order guarantees no more collisions.
62 if (!layoutHasStatics && otherItem.y > item.y + item.h) break;
63
64 if (collides(item, otherItem)) {
65 resolveCompactionCollision(
66 layout,
67 otherItem,
68 moveToCoord + item[sizeProp],
69 axis,
70 layoutHasStatics
71 );
72 }
73 }
74
75 (item as Mutable<LayoutItem>)[axis] = moveToCoord;
76}
77
78/**
79 * Compact a single item vertically (move up).

Callers 2

compactItemVerticalFunction · 0.70
compactItemHorizontalFunction · 0.70

Calls 2

getStaticsFunction · 0.70
collidesFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…