MCPcopy
hub / github.com/react-grid-layout/react-grid-layout / moveElementAwayFromCollision

Function moveElementAwayFromCollision

src/core/layout.ts:365–465  ·  view source on GitHub ↗
(
  layout: Layout,
  collidesWith: LayoutItem,
  itemToMove: LayoutItem,
  isUserAction: boolean | undefined,
  compactType: CompactType,
  cols: number
)

Source from the content-addressed store, hash-verified

363 * @returns Updated layout
364 */
365export function moveElementAwayFromCollision(
366 layout: Layout,
367 collidesWith: LayoutItem,
368 itemToMove: LayoutItem,
369 isUserAction: boolean | undefined,
370 compactType: CompactType,
371 cols: number
372): LayoutItem[] {
373 const compactH = compactType === "horizontal";
374 const compactV = compactType === "vertical";
375 const preventCollision = collidesWith.static;
376
377 // Try to move up/left first (only on primary collision from user action)
378 if (isUserAction) {
379 isUserAction = false; // Only try this once
380
381 // Create a fake item to test if there's room above/left
382 const fakeItem: LayoutItem = {
383 x: compactH ? Math.max(collidesWith.x - itemToMove.w, 0) : itemToMove.x,
384 y: compactV ? Math.max(collidesWith.y - itemToMove.h, 0) : itemToMove.y,
385 w: itemToMove.w,
386 h: itemToMove.h,
387 i: "-1"
388 };
389
390 const firstCollision = getFirstCollision(layout, fakeItem);
391 const collisionNorth =
392 firstCollision !== undefined &&
393 firstCollision.y + firstCollision.h > collidesWith.y;
394 const collisionWest =
395 firstCollision !== undefined &&
396 collidesWith.x + collidesWith.w > firstCollision.x;
397
398 // No collision above/left - we can move there
399 if (!firstCollision) {
400 return moveElement(
401 layout,
402 itemToMove,
403 compactH ? fakeItem.x : undefined,
404 compactV ? fakeItem.y : undefined,
405 isUserAction,
406 preventCollision,
407 compactType,
408 cols
409 );
410 }
411
412 // Handle specific collision cases
413 if (collisionNorth && compactV) {
414 return moveElement(
415 layout,
416 itemToMove,
417 undefined,
418 itemToMove.y + 1,
419 isUserAction,
420 preventCollision,
421 compactType,
422 cols

Callers 2

utils-test.jsFile · 0.90
moveElementFunction · 0.85

Calls 2

getFirstCollisionFunction · 0.85
moveElementFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…