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

Function calcWH

src/core/calculate.ts:233–264  ·  view source on GitHub ↗
(
  positionParams: PositionParams,
  width: number,
  height: number,
  x: number,
  y: number,
  handle: ResizeHandleAxis
)

Source from the content-addressed store, hash-verified

231 * @returns w, h in grid units
232 */
233export function calcWH(
234 positionParams: PositionParams,
235 width: number,
236 height: number,
237 x: number,
238 y: number,
239 handle: ResizeHandleAxis
240): { w: number; h: number } {
241 const { margin, maxRows, cols, rowHeight } = positionParams;
242 const colWidth = calcGridColWidth(positionParams);
243
244 // width = colWidth * w - (margin * (w - 1))
245 // w = (width + margin) / (colWidth + margin)
246 const w = Math.round((width + margin[0]) / (colWidth + margin[0]));
247 const h = Math.round((height + margin[1]) / (rowHeight + margin[1]));
248
249 // Clamp based on resize handle direction
250 let _w = clamp(w, 0, cols - x);
251 let _h = clamp(h, 0, maxRows - y);
252
253 // West handles can resize to full width
254 if (handle === "sw" || handle === "w" || handle === "nw") {
255 _w = clamp(w, 0, cols);
256 }
257
258 // North handles can resize to full height
259 if (handle === "nw" || handle === "n" || handle === "ne") {
260 _h = clamp(h, 0, maxRows);
261 }
262
263 return { w: _w, h: _h };
264}
265
266/**
267 * Calculate grid units from pixel dimensions without clamping.

Callers 3

utils-test.jsFile · 0.90

Calls 2

calcGridColWidthFunction · 0.85
clampFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…