Skip to content

packages/core/src/lib/layout/box.ts

Read as Markdown

This is the source snapshot used to build these API details. View this revision on GitHub.

Back to reference

1 import { contentBoxStyle, borderBoxPaddingFloor } from './box-sizing.js';
2 import {
3   layoutDiagnostic,
4   type LayoutDiagnosticContext,
5 } from '../style/diagnostics.js';
6 import { normalizeBoxInsets } from './box-insets.js';
7 import { resolveScalarSizingLength } from '../style/sizing-length.js';
8 import type { BoxStyle, ResolvedBoxStyle } from '../style/types.js';
9 import type { SizingLength } from '../style/sizing-length.js';
10 import type { Constraints } from './types.js';
11 
12 /**
13  * Resolves declared box sizing and padding into border and content boxes under parent constraints.
14  *
15  * @param style - Declared box sizing and edge properties. See {@link BoxStyle}.
16  * @param constraints - Available dimensions used to resolve percentages and constraints. See
17  * {@link Constraints} .
18  * @param context - Component and property context for diagnostic messages. See
19  * {@link LayoutDiagnosticContext} .
20  * @returns Resolved box dimensions and edge values. See {@link ResolvedBoxStyle}.
21  *
22  * @see {@link BoxStyle}
23  * @see {@link Constraints}
24  * @see {@link LayoutDiagnosticContext}
25  * @see {@link ResolvedBoxStyle}
26  */
27 export function resolveBox(
28   style: Readonly<BoxStyle>,
29   constraints: Readonly<Constraints>,
30   context: Readonly<LayoutDiagnosticContext> = {},
31 ): ResolvedBoxStyle {
32   const diagnosticContext = {
33     component: context.component ?? 'layout',
34     algorithm: context.algorithm ?? 'box-resolution',
35     constraints,
36   };
37   validateConstraints(constraints, diagnosticContext);
38   const ratio = style.aspectRatio;
39   if (ratio !== undefined && (!Number.isFinite(ratio) || ratio <= 0)) throw layoutDiagnostic({ ...diagnosticContext, property: 'aspectRatio', value: ratio, reason: 'must be finite and positive' });
40   const paddingFloor = borderBoxPaddingFloor(style);
41   if (style.boxSizing !== undefined) style = contentBoxStyle(style, { width: constraints.maxWidth, height: constraints.maxHeight, context: diagnosticContext });
42   const padding = normalizeBoxInsets(style, diagnosticContext);
43 
44   let width = style.width, height = style.height;
45   const widthAutomatic = width === undefined || width === 'auto';
46   const heightAutomatic = height === undefined || height === 'auto';
47   let vertical: { border: number; content: number } | undefined;
48   if (ratio !== undefined && widthAutomatic && !heightAutomatic) {
49     vertical = resolveAxis('height', height, style.minHeight, style.maxHeight,
50       constraints.minHeight, constraints.maxHeight, padding.top, padding.bottom, diagnosticContext, paddingFloor);
51     width = paddingFloor ? Math.max(0, vertical.border * ratio - padding.left - padding.right) : vertical.content * ratio;
52   }
53   const horizontal = resolveAxis(
54     'width',
55     width,
56     style.minWidth,
57     style.maxWidth,
58     constraints.minWidth,
59     constraints.maxWidth,
60     padding.left,
61     padding.right,
62     diagnosticContext,
63     paddingFloor,
64   );
65   if (ratio !== undefined && !widthAutomatic && heightAutomatic) {
66     height = paddingFloor ? Math.max(0, horizontal.border / ratio - padding.top - padding.bottom) : horizontal.content / ratio;
67   }
68   vertical ??= resolveAxis(
69     'height',
70     height,
71     style.minHeight,
72     style.maxHeight,
73     constraints.minHeight,
74     constraints.maxHeight,
75     padding.top,
76     padding.bottom,
77     diagnosticContext,
78     paddingFloor,
79   );
80 
81   return {
82     borderBox: {
83       x: 0,
84       y: 0,
85       width: horizontal.border,
86       height: vertical.border,
87     },
88     contentBox: {
89       x: padding.left,
90       y: padding.top,
91       width: horizontal.content,
92       height: vertical.content,
93     },
94   };
95 }
96 
97 function resolveAxis(
98   axis: 'width' | 'height',
99   specified: SizingLength | undefined,
100   specifiedMinimum: Exclude<SizingLength, 'auto'> | undefined,
101   specifiedMaximum: Exclude<SizingLength, 'auto'> | undefined,
102   constraintMinimum: number,
103   constraintMaximum: number,
104   paddingStart: number,
105   paddingEnd: number,
106   context: Readonly<LayoutDiagnosticContext>,
107   paddingFloor: boolean,
108 ): { border: number; content: number } {
109   const minimumProperty = axis === 'width' ? 'minWidth' : 'minHeight';
110   const maximumProperty = axis === 'width' ? 'maxWidth' : 'maxHeight';
111   const minimum =
112     specifiedMinimum === undefined
113       ? 0
114       : resolveAllocationLength(
115           specifiedMinimum,
116           constraintMaximum,
117           minimumProperty,
118           context,
119         );
120   const maximum =
121     specifiedMaximum === undefined
122       ? Number.POSITIVE_INFINITY
123       : resolveAllocationLength(
124           specifiedMaximum,
125           constraintMaximum,
126           maximumProperty,
127           context,
128         );
129 
130   if (minimum > maximum) {
131     throw layoutDiagnostic({
132       ...context,
133       property: minimumProperty,
134       value: minimum,
135       reason: `${minimumProperty} ${minimum} must not exceed ${maximumProperty} ${maximum}`,
136     });
137   }
138 
139   const padding = requireFiniteArithmetic(
140     paddingStart + paddingEnd,
141     axis,
142     context,
143   );
144   const resolved =
145     specified === undefined
146       ? undefined
147       : resolveScalarSizingLength(specified, constraintMaximum, axis, context);
148   let content: number;
149   if (resolved === undefined) {
150     if (!Number.isFinite(constraintMaximum)) {
151       throw layoutDiagnostic({
152         ...context,
153         property: axis,
154         value: specified ?? 'auto',
155         reason: 'requires a definite finite allocation for auto sizing',
156       });
157     }
158     content = Math.max(
159       0,
160       requireFiniteArithmetic(constraintMaximum - padding, axis, context),
161     );
162   } else {
163     if (resolved < 0) {
164       throw layoutDiagnostic({
165         ...context,
166         property: axis,
167         value: specified,
168         reason: 'must be nonnegative when used as a layout allocation',
169       });
170     }
171     content = resolved;
172   }
173 
174   content = Math.min(maximum, Math.max(minimum, content));
175   const paddedContent = requireFiniteArithmetic(
176     content + padding,
177     axis,
178     context,
179   );
180   const constrainedBorder = Math.min(
181     constraintMaximum,
182     Math.max(constraintMinimum, paddedContent),
183   );
184   const border = paddingFloor ? Math.max(padding, constrainedBorder) : constrainedBorder;
185   return {
186     border: requireFiniteArithmetic(border, axis, context),
187     content: Math.max(
188       0,
189       requireFiniteArithmetic(border - padding, axis, context),
190     ),
191   };
192 }
193 
194 function requireFiniteArithmetic(
195   value: number,
196   property: string,
197   context: Readonly<LayoutDiagnosticContext>,
198 ): number {
199   if (!Number.isFinite(value)) {
200     throw layoutDiagnostic({
201       ...context,
202       property,
203       value,
204       reason: 'resolved box arithmetic must remain finite',
205     });
206   }
207   return value;
208 }
209 
210 function resolveAllocationLength(
211   value: Exclude<SizingLength, 'auto'>,
212   reference: number,
213   property: string,
214   context: Readonly<LayoutDiagnosticContext>,
215 ): number {
216   const resolved = resolveScalarSizingLength(value, reference, property, context)!;
217   if (resolved < 0) {
218     throw layoutDiagnostic({
219       ...context,
220       property,
221       value,
222       reason: 'must be nonnegative when used as a layout allocation',
223     });
224   }
225   return resolved;
226 }
227 
228 function validateConstraints(
229   constraints: Readonly<Constraints>,
230   context: Readonly<LayoutDiagnosticContext>,
231 ): void {
232   validateConstraint(
233     constraints.minWidth,
234     'constraints.minWidth',
235     false,
236     context,
237   );
238   validateConstraint(
239     constraints.maxWidth,
240     'constraints.maxWidth',
241     true,
242     context,
243   );
244   validateConstraint(
245     constraints.minHeight,
246     'constraints.minHeight',
247     false,
248     context,
249   );
250   validateConstraint(
251     constraints.maxHeight,
252     'constraints.maxHeight',
253     true,
254     context,
255   );
256 
257   if (constraints.minWidth > constraints.maxWidth) {
258     throw layoutDiagnostic({
259       ...context,
260       property: 'constraints.minWidth',
261       value: constraints.minWidth,
262       reason: `constraints.minWidth ${constraints.minWidth} must not exceed constraints.maxWidth ${constraints.maxWidth}`,
263     });
264   }
265   if (constraints.minHeight > constraints.maxHeight) {
266     throw layoutDiagnostic({
267       ...context,
268       property: 'constraints.minHeight',
269       value: constraints.minHeight,
270       reason: `constraints.minHeight ${constraints.minHeight} must not exceed constraints.maxHeight ${constraints.maxHeight}`,
271     });
272   }
273 }
274 
275 function validateConstraint(
276   value: number,
277   property: string,
278   allowPositiveInfinity: boolean,
279   context: Readonly<LayoutDiagnosticContext>,
280 ): void {
281   if (
282     !Number.isFinite(value) &&
283     !(allowPositiveInfinity && value === Number.POSITIVE_INFINITY)
284   ) {
285     throw layoutDiagnostic({
286       ...context,
287       property,
288       value,
289       reason: 'must be finite',
290     });
291   }
292   if (value < 0) {
293     throw layoutDiagnostic({
294       ...context,
295       property,
296       value,
297       reason: 'must be nonnegative',
298     });
299   }
300 }
301 

Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.