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

Function calcGridItemPosition

src/core/calculate.ts:84–164  ·  view source on GitHub ↗
(
  positionParams: PositionParams,
  x: number,
  y: number,
  w: number,
  h: number,
  dragPosition?: { top: number; left: number } | null,
  resizePosition?: {
    top: number;
    left: number;
    height: number;
    width: number;
  } | null
)

Source from the content-addressed store, hash-verified

82 * @returns Position in pixels
83 */
84export function calcGridItemPosition(
85 positionParams: PositionParams,
86 x: number,
87 y: number,
88 w: number,
89 h: number,
90 dragPosition?: { top: number; left: number } | null,
91 resizePosition?: {
92 top: number;
93 left: number;
94 height: number;
95 width: number;
96 } | null
97): Position {
98 const { margin, containerPadding, rowHeight } = positionParams;
99 const colWidth = calcGridColWidth(positionParams);
100
101 let width: number;
102 let height: number;
103 let top: number;
104 let left: number;
105
106 // If resizing, use the exact width and height from resize callbacks
107 if (resizePosition) {
108 width = Math.round(resizePosition.width);
109 height = Math.round(resizePosition.height);
110 } else {
111 // Calculate from grid units
112 width = calcGridItemWHPx(w, colWidth, margin[0]);
113 height = calcGridItemWHPx(h, rowHeight, margin[1]);
114 }
115
116 // If dragging, use the exact left/top from drag callbacks
117 if (dragPosition) {
118 top = Math.round(dragPosition.top);
119 left = Math.round(dragPosition.left);
120 } else if (resizePosition) {
121 // If resizing, use the exact left/top from resize position
122 top = Math.round(resizePosition.top);
123 left = Math.round(resizePosition.left);
124 } else {
125 // Calculate from grid units
126 top = Math.round((rowHeight + margin[1]) * y + containerPadding[1]);
127 left = Math.round((colWidth + margin[0]) * x + containerPadding[0]);
128 }
129
130 // When not dragging or resizing, fix margin inconsistencies caused by rounding.
131 // Due to Math.round(), the gap between adjacent items can differ from the
132 // expected margin (e.g., 0px or 2px instead of 1px). We fix this by comparing
133 // where the next sibling would start vs where this item ends, and adjusting
134 // the width/height to maintain consistent margins.
135 if (!dragPosition && !resizePosition) {
136 if (Number.isFinite(w)) {
137 // Calculate where the next column's item would start
138 const siblingLeft = Math.round(
139 (colWidth + margin[0]) * (x + w) + containerPadding[0]
140 );
141 // Calculate actual margin: sibling start - (our left + our width)

Callers 5

utils-test.jsFile · 0.90
GridItemFunction · 0.85

Calls 2

calcGridColWidthFunction · 0.85
calcGridItemWHPxFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…