@@ -52,6 +52,22 @@ function getProps(node: Node): ContainerProps | null {
5252 return node.props;
5353 }
5454
55+function getNodeFlex(node: Node, isVertical: boolean): number {
56+ const explicitFlex = getProps(node)?.flex;
57+ if (explicitFlex && explicitFlex > 0) {
58+ return explicitFlex;
59+ }
60+ if (
61+ !isVertical &&
62+ node.type === "text" &&
63+ node.props.repeat === "fill" &&
64+ visibleWidth(node.content) > 0
65+ ) {
66+ return 1;
67+ }
68+ return 0;
69+}
70+
5571 function requiredAt<T>(
5672 items: readonly T[],
5773 index: number,
@@ -297,6 +313,100 @@ function largestRemainder(fractions: number[], total: number): number[] {
297313 return floored;
298314 }
299315
316+interface FlexAllocationItem {
317+ flex: number;
318+ min: number;
319+ max: number;
320+}
321+
322+function allocateFlexSizes(
323+ items: readonly FlexAllocationItem[],
324+ total: number,
325+): number[] {
326+ const sizes = Array.from({ length: items.length }, () => 0);
327+ let remainingIndices = items.map((_, index) => index);
328+ let remainingSpace = total;
329+ let remainingFlex = items.reduce((sum, item) => sum + item.flex, 0);
330+
331+ while (remainingIndices.length > 0 && remainingFlex > 0) {
332+ const rawSizes = remainingIndices.map((index) => {
333+ const item = requiredAt(items, index, "flex allocation item");
334+ return (item.flex / remainingFlex) * remainingSpace;
335+ });
336+
337+ const minClamped = new Set<number>();
338+
339+ for (let i = 0; i < remainingIndices.length; i++) {
340+ const index = requiredAt(remainingIndices, i, "flex allocation index");
341+ const rawSize = requiredAt(rawSizes, i, "raw flex size");
342+ const item = requiredAt(items, index, "flex allocation item");
343+
344+ if (rawSize < item.min) {
345+ sizes[index] = item.min;
346+ remainingSpace -= item.min;
347+ remainingFlex -= item.flex;
348+ minClamped.add(index);
349+ }
350+ }
351+
352+ if (minClamped.size === 0) {
353+ const rounded = largestRemainder(rawSizes, remainingSpace);
354+ for (let i = 0; i < remainingIndices.length; i++) {
355+ const index = requiredAt(remainingIndices, i, "flex allocation index");
356+ sizes[index] = requiredAt(rounded, i, "rounded flex size");
357+ }
358+ break;
359+ }
360+
361+ remainingIndices = remainingIndices.filter(
362+ (index) => !minClamped.has(index),
363+ );
364+ }
365+
366+ let activeIndices = items.map((_, index) => index);
367+ let activeFlex = items.reduce((sum, item) => sum + item.flex, 0);
368+
369+ while (activeIndices.length > 0 && activeFlex > 0) {
370+ let excess = 0;
371+ const maxClamped = new Set<number>();
372+
373+ for (const index of activeIndices) {
374+ const item = requiredAt(items, index, "flex allocation item");
375+ const max = Math.max(item.min, item.max);
376+ const size = requiredAt(sizes, index, "allocated flex size");
377+
378+ if (size > max) {
379+ sizes[index] = max;
380+ excess += size - max;
381+ activeFlex -= item.flex;
382+ maxClamped.add(index);
383+ }
384+ }
385+
386+ if (maxClamped.size === 0) {
387+ return sizes;
388+ }
389+
390+ activeIndices = activeIndices.filter((index) => !maxClamped.has(index));
391+ if (activeIndices.length === 0 || activeFlex <= 0 || excess <= 0) {
392+ return sizes;
393+ }
394+
395+ const rawExtras = activeIndices.map((index) => {
396+ const item = requiredAt(items, index, "flex allocation item");
397+ return (item.flex / activeFlex) * excess;
398+ });
399+ const rounded = largestRemainder(rawExtras, excess);
400+
401+ for (let i = 0; i < activeIndices.length; i++) {
402+ const index = requiredAt(activeIndices, i, "flex allocation index");
403+ sizes[index] += requiredAt(rounded, i, "rounded flex size");
404+ }
405+ }
406+
407+ return sizes;
408+}
409+
300410 // --- Main layout ---
301411
302412 /**
@@ -384,7 +494,7 @@ function layoutWrapHStack(
384494
385495 for (const child of children) {
386496 const cProps = getProps(child);
387- const flex = cProps?.flex ?? 0;
497+ const flex = getNodeFlex(child, false);
388498 flexValues.push(flex);
389499
390500 let baseW: number;
@@ -448,24 +558,23 @@ function layoutWrapHStack(
448558 flexPositions.push(ci);
449559 }
450560 }
451- const rawSizes = flexPositions.map((ci) => {
452- const rowIndex = requiredAt(rowIdx, ci, "wrap row index");
453- return (
454- (requiredAt(flexValues, rowIndex, "flex value") / totalFlex) *
455- flexSpace
456- );
457- });
458- const rounded = largestRemainder(rawSizes, flexSpace);
561+
562+ const allocated = allocateFlexSizes(
563+ flexPositions.map((ci) => {
564+ const rowIndex = requiredAt(rowIdx, ci, "wrap row index");
565+ const cProps = getProps(requiredAt(children, rowIndex, "child node"));
566+ return {
567+ flex: requiredAt(flexValues, rowIndex, "flex value"),
568+ min: cProps?.minWidth ?? 0,
569+ max: cProps?.maxWidth ?? Infinity,
570+ };
571+ }),
572+ flexSpace,
573+ );
459574
460575 for (let fi = 0; fi < flexPositions.length; fi++) {
461576 const ci = requiredAt(flexPositions, fi, "flex position");
462- let size = requiredAt(rounded, fi, "rounded flex size");
463- const rowIndex = requiredAt(rowIdx, ci, "wrap row index");
464- const cProps = getProps(requiredAt(children, rowIndex, "child node"));
465- if (cProps) {
466- size = clamp(size, cProps.minWidth ?? 0, cProps.maxWidth ?? Infinity);
467- }
468- childWidths[ci] = size;
577+ childWidths[ci] = requiredAt(allocated, fi, "allocated flex size");
469578 }
470579 }
471580
@@ -638,7 +747,7 @@ function layoutNode(
638747
639748 for (const child of children) {
640749 const cProps = getProps(child);
641- const flex = cProps?.flex ?? 0;
750+ const flex = getNodeFlex(child, isVertical);
642751
643752 if (flex > 0) {
644753 totalFlex += flex;
@@ -719,23 +828,26 @@ function layoutNode(
719828
720829 if (totalFlex > 0) {
721830 const flexInfos = infos.filter((c) => c.flex > 0);
722- const rawSizes = flexInfos.map((c) => (c.flex / totalFlex) * flexSpace);
723- const rounded = largestRemainder(rawSizes, flexSpace);
831+ const allocated = allocateFlexSizes(
832+ flexInfos.map((info) => {
833+ const cProps = getProps(info.node);
834+ return {
835+ flex: info.flex,
836+ min: isVertical ? (cProps?.minHeight ?? 0) : (cProps?.minWidth ?? 0),
837+ max: isVertical
838+ ? (cProps?.maxHeight ?? Infinity)
839+ : (cProps?.maxWidth ?? Infinity),
840+ };
841+ }),
842+ flexSpace,
843+ );
724844
725845 for (let i = 0; i < flexInfos.length; i++) {
726- let size = requiredAt(rounded, i, "rounded flex size");
727- const flexInfo = requiredAt(flexInfos, i, "flex child info");
728- const cProps = getProps(flexInfo.node);
729- if (cProps) {
730- const minMain = isVertical
731- ? (cProps.minHeight ?? 0)
732- : (cProps.minWidth ?? 0);
733- const maxMain = isVertical
734- ? (cProps.maxHeight ?? Infinity)
735- : (cProps.maxWidth ?? Infinity);
736- size = clamp(size, minMain, maxMain);
737- }
738- flexInfo.mainSize = size;
846+ requiredAt(flexInfos, i, "flex child info").mainSize = requiredAt(
847+ allocated,
848+ i,
849+ "allocated flex size",
850+ );
739851 }
740852 }
741853