From ef79f319d824a81e6f6f12d696e60d3d17c929f4 Mon Sep 17 00:00:00 2001 From: James Ide Date: Tue, 1 Dec 2026 05:28:28 -0800 Subject: [PATCH] Size a wrapping row's min-content width by its widest child (#2104) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Why === A flex item does not shrink below its min-content size (css-flexbox-1 §4.5). A wrapping row can put each child on a line of its own, so its min-content width is that of its widest child (§9.9.1: "for a multi-line container, the min-content main size is simply the largest min-content contribution of all the non-collapsed flex items"). Yoga's walk sums the children whether or not the container wraps, so a wrapping row never narrows enough to wrap: ```html
``` How === The walk takes the largest child, and no gaps, along the main axis of a wrapping row. A wrapping column keeps the sum, which differs from §9.9.1: its sentence covers both directions. Chrome finds a min-content height by laying the box out at an indefinite height, where a column never wraps, as css-sizing-3 §2.1 has it for block containers (the min-content block size equals the max-content block size). `wrapping_column_counts_all_children` records this. Only Chrome was checked. The min-content height of a wrapping row is not addressed. It depends on where the row's lines break, which the walk cannot know. The walk still takes the tallest child, so a column can shrink a wrapping row below the lines it wraps to. Test Plan ========= `yarn gentest -f YGAutoMinSizeTest` generates the new fixtures from Chrome, and `cmake --build tests/build && tests/build/yogatests` passes. With the change reverted, `wrapping_row_counts_widest_child` fails: the row is 140 wide and 10 tall where Chrome has 70 and 40. `wrapping_column_counts_all_children` passes with and without the change (110 tall), and fails with 60 if the rule also applies to columns. `yarn gentest -h` leaves the tree clean, and `node ./scripts/clang-format.js --check` passes. The generated Java tests were not run, because the Gradle project has no unit-test source set, and neither were the generated JavaScript tests, which need Emscripten. Changelog: [General][Fixed] - Fix Yoga's automatic minimum size keeping a wrapping row as wide as all of its children Differential Revision: D135341618 Signed-off-by: James Ide --- .../yoga/yoga/algorithm/CalculateLayout.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 1eccc7c1fba..b490dcca869 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -1027,8 +1027,12 @@ static MinContentSize measureOrWalkMinContentMainSize( const Direction direction = node->resolveDirection(ownerDirection); // Children add up along the container's main axis; across it the largest - // counts. - const bool alongMainAxis = isRow(node->style().flexDirection()) == wantRow; + // counts. A wrapping row can put each child on a line of its own, so its + // largest child counts along it too (css-flexbox-1 §9.9.1). A wrapping + // column still adds up: Chrome finds a min-content height by layout at an + // indefinite height, where nothing wraps. + const bool childrenAddUp = isRow(node->style().flexDirection()) == wantRow && + (node->style().flexWrap() == Wrap::NoWrap || !wantRow); float minContent = 0.0f; size_t itemCount = 0; @@ -1052,7 +1056,7 @@ static MinContentSize measureOrWalkMinContentMainSize( generationCount); const float childSize = child.size + childStyle.computeMarginForAxis(requestedAxis, ownerWidth); - minContent = alongMainAxis ? minContent + childSize + minContent = childrenAddUp ? minContent + childSize : std::max(minContent, childSize); resolvesAgainstOwnerWidth = resolvesAgainstOwnerWidth || child.resolvesAgainstOwnerWidth || childStyle.hasPercentMargin(); @@ -1060,7 +1064,7 @@ static MinContentSize measureOrWalkMinContentMainSize( // A percentage gap resolves against the size being found, so it counts as // zero (css-sizing-3 §5.2.1). - if (alongMainAxis && itemCount > 1) { + if (childrenAddUp && itemCount > 1) { minContent += node->style().computeGapForAxis(requestedAxis, 0.0f) * static_cast(itemCount - 1); } @@ -1190,9 +1194,9 @@ static MinContentSize computeChildMinContentContribution( // * Container: iterate in-flow children. For each, take its // min-content contribution along `requestedAxis` plus its margins: // the sum of them where that is the container's main axis, the -// largest where it is its cross axis. Add the gaps between them -// along the main axis, and the container's own padding and border -// on both ends of the axis. +// largest where it is its cross axis or the container is a +// wrapping row. Add the gaps between the children that add up, and +// the container's own padding and border on both ends of the axis. // // Container-level recursion does no layout writes (no positions, no // alignment, no flex distribution); only the descendant leaf measure