packages/core/src/lib/style/sizing-length.ts
This is the source snapshot used to build these API details. View this revision on GitHub.
1 import { layoutDiagnostic, type LayoutDiagnosticContext } from './diagnostics.js';
2 import { parsePercentage, resolveLength } from './resolve-length.js';
3 import type { Length, Percentage } from './types.js';
4
5 const fitContentBrand: unique symbol = Symbol('pibbl.fit-content');
6
7 /** Sizing values requiring an intrinsic layout owner. See {@link SizingLength}. */
8 export type SizingKeyword = 'min-content' | 'max-content';
9
10 /** Immutable fit-content bound created by {@link fitContent}. See {@link SizingLength}. */
11 export interface FitContent {
12 /** Finite nonnegative logical length or parent-axis percentage. See {@link Percentage}. */
13 readonly limit: number | Percentage;
14 readonly [fitContentBrand]: true;
15 }
16
17 /** Sizing-only value; padding and insets retain {@link Length}. See {@link FitContent}, {@link SizingKeyword}. */
18 export type SizingLength = Length | SizingKeyword | FitContent | 'stretch';
19
20 /** A scalar result or an owner-resolved intrinsic sizing request. */
21 export type NormalizedSizingLength =
22 | Readonly<{ kind: 'auto' }>
23 | Readonly<{ kind: 'length'; value: number }>
24 | Readonly<{ kind: 'min-content' }>
25 | Readonly<{ kind: 'max-content' }>
26 | Readonly<{ kind: 'fit-content'; limit: number }>;
27
28 /** Options supplied by the layout owner that owns intrinsic-size policy. */
29 export interface SizingNormalizationOptions {
30 readonly reference: number | undefined;
31 readonly property: string;
32 readonly context?: Readonly<LayoutDiagnosticContext>;
33 readonly allowAuto: boolean;
34 readonly allowIntrinsic: boolean;
35 }
36
37 /**
38 * Creates an immutable bound between measured min- and max-content widths.
39 * @param limit - Nonnegative finite logical width or parent-axis {@link Percentage}.
40 * @returns Branded immutable {@link FitContent} sizing value.
41 * @see {@link SizingLength}
42 */
43 export function fitContent(limit: number | Percentage): FitContent {
44 const numeric = typeof limit === 'number' ? limit : parsePercentage(limit, 'fitContent');
45 if (!Number.isFinite(numeric) || numeric < 0) {
46 throw layoutDiagnostic({
47 property: 'fitContent',
48 value: limit,
49 reason: 'limit must be finite and nonnegative',
50 });
51 }
52 return Object.freeze({ [fitContentBrand]: true, limit }) as FitContent;
53 }
54
55 /** Identifies a fit-content value without accepting lookalike style records. */
56 export function isFitContent(value: unknown): value is FitContent {
57 return typeof value === 'object' && value !== null &&
58 (value as Partial<FitContent>)[fitContentBrand] === true;
59 }
60
61 /** Identifies a non-scalar sizing request without widening scalar length users. */
62 export function isIntrinsicSizingValue(value: unknown): value is SizingKeyword | FitContent {
63 return value === 'min-content' || value === 'max-content' || isFitContent(value);
64 }
65
66 /** Resolves only scalar sizing input at a non-intrinsic owner boundary. */
67 export function resolveScalarSizingLength(
68 value: SizingLength,
69 reference: number | undefined,
70 property: string,
71 context: Readonly<LayoutDiagnosticContext> = {},
72 ): number | undefined {
73 if (value === 'stretch') {
74 if (reference !== undefined && Number.isFinite(reference) && reference >= 0) return reference;
75 throw layoutDiagnostic({ ...context, property, value, reason: 'stretch requires a finite owning allocation' });
76 }
77 if (isIntrinsicSizingValue(value)) {
78 throw layoutDiagnostic({
79 ...context,
80 property,
81 value,
82 reason: 'requires a direct finite Flex intrinsic-sizing owner',
83 });
84 }
85 return resolveLength(value, reference, property, context);
86 }
87
88 /** Normalizes sizing syntax without teaching scalar length users intrinsic semantics. */
89 export function normalizeSizingLength(
90 value: SizingLength | undefined,
91 options: Readonly<SizingNormalizationOptions>,
92 ): NormalizedSizingLength | undefined {
93 if (value === undefined) return undefined;
94 const context = options.context ?? {};
95 if (isFitContent(value)) {
96 if (!options.allowIntrinsic) throw unsupported(options, value, 'intrinsic sizing requires a measuring layout owner');
97 const limit = resolveLength(value.limit, options.reference, options.property, context);
98 if (limit === undefined || limit < 0) {
99 throw layoutDiagnostic({ ...context, property: options.property, value, reason: 'fit-content limit must resolve to a finite nonnegative length' });
100 }
101 return { kind: 'fit-content', limit };
102 }
103 if (value === 'min-content' || value === 'max-content') {
104 if (!options.allowIntrinsic) throw unsupported(options, value, 'intrinsic sizing requires a measuring layout owner');
105 return { kind: value };
106 }
107 if (value === 'stretch') {
108 const resolved = resolveScalarSizingLength(value, options.reference, options.property, context)!;
109 return { kind: 'length', value: resolved };
110 }
111 const resolved = resolveLength(value, options.reference, options.property, context);
112 if (resolved === undefined) {
113 if (!options.allowAuto) throw unsupported(options, value, 'automatic sizing is not allowed for this property');
114 return { kind: 'auto' };
115 }
116 return { kind: 'length', value: resolved };
117 }
118
119 function unsupported(
120 options: Readonly<SizingNormalizationOptions>,
121 value: unknown,
122 reason: string,
123 ): Error {
124 return layoutDiagnostic({
125 ...(options.context ?? {}),
126 property: options.property,
127 value,
128 reason,
129 });
130 }
131
Documentation version
Section titled “Documentation version”Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.