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

Function aspectRatio

src/core/constraints.ts:202–239  ·  view source on GitHub ↗
(ratio: number)

Source from the content-addressed store, hash-verified

200 * ```
201 */
202export function aspectRatio(ratio: number): LayoutConstraint {
203 return {
204 name: `aspectRatio(${ratio})`,
205
206 constrainSize(
207 _item: LayoutItem,
208 w: number,
209 _h: number,
210 _handle: ResizeHandleAxis,
211 context: ConstraintContext
212 ): { w: number; h: number } {
213 const { cols, containerWidth, rowHeight, margin } = context;
214 // Calculate column width in pixels
215 // colWidth = (containerWidth - margin[0] * (cols - 1)) / cols
216 // Note: simplified formula assumes no container padding
217 const colWidth = (containerWidth - margin[0] * (cols - 1)) / cols;
218
219 // Calculate pixel width of the item
220 // pixelWidth = colWidth * w + margin[0] * (w - 1)
221 const pixelWidth = colWidth * w + margin[0] * Math.max(0, w - 1);
222
223 // Calculate required pixel height for aspect ratio
224 const pixelHeight = pixelWidth / ratio;
225
226 // Convert pixel height back to grid units
227 // pixelHeight = rowHeight * h + margin[1] * (h - 1)
228 // Solving for h:
229 // pixelHeight = h * (rowHeight + margin[1]) - margin[1]
230 // h = (pixelHeight + margin[1]) / (rowHeight + margin[1])
231 const h = Math.max(
232 1,
233 Math.round((pixelHeight + margin[1]) / (rowHeight + margin[1]))
234 );
235
236 return { w, h };
237 }
238 };
239}
240
241/**
242 * Create a snap-to-grid constraint.

Callers 2

AspectRatioLayoutFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…