From dcbe6bbcc59631bce61fedc8b25afc62296189bb Mon Sep 17 00:00:00 2001 From: GitHub Workflows Support Date: Thu, 3 Dec 2026 07:09:58 -0800 Subject: [PATCH] Count gaps in a flex container's min-content size (#2103) 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). When the item is itself a flex container, Chrome counts the gaps between its children along its main axis in that size. Yoga's min-content walk leaves them out, so the item can shrink until its children overflow it by the gaps: ```html
``` How === The walk adds one gap between each two in-flow children when it sums them along the container's main axis. A percentage gap counts as zero, because a percentage gutter resolves against zero for an intrinsic size contribution (css-sizing-3 §5.2.1). Chrome does the same. 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, two fixtures fail: | Fixture | Chrome | Without the change | | --- | --- | --- | | `counts_column_gaps` (item width) | 130 | 90 | | `counts_row_gaps` (item height) | 80 | 60 | `gaps_skip_cross_axis_and_absolute_children` and `percent_gap_counts_as_zero` pass with and without the change; they pin what must not count. Adding cross-axis gaps, counting an absolutely positioned child, or resolving a percentage gap against the owner's width makes one of them fail (50 becomes 70, 30 becomes 50, and 60 becomes 80). `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 leaving out the gaps between a flex item's children Reviewed By: javache Differential Revision: D135335463 --- .../yoga/yoga/algorithm/CalculateLayout.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 8d06148f34b..1eccc7c1fba 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -1031,6 +1031,7 @@ static MinContentSize measureOrWalkMinContentMainSize( const bool alongMainAxis = isRow(node->style().flexDirection()) == wantRow; float minContent = 0.0f; + size_t itemCount = 0; bool resolvesAgainstOwnerWidth = node->style().hasPercentPadding(); for (size_t i = 0; i < node->getChildCount(); i++) { @@ -1039,6 +1040,7 @@ static MinContentSize measureOrWalkMinContentMainSize( childStyle.positionType() == PositionType::Absolute) { continue; } + itemCount++; const MinContentSize child = computeChildMinContentContribution( node, @@ -1056,6 +1058,13 @@ static MinContentSize measureOrWalkMinContentMainSize( child.resolvesAgainstOwnerWidth || childStyle.hasPercentMargin(); } + // A percentage gap resolves against the size being found, so it counts as + // zero (css-sizing-3 §5.2.1). + if (alongMainAxis && itemCount > 1) { + minContent += node->style().computeGapForAxis(requestedAxis, 0.0f) * + static_cast(itemCount - 1); + } + minContent += node->style().computeFlexStartPaddingAndBorder( requestedAxis, direction, ownerWidth) + node->style().computeFlexEndPaddingAndBorder( @@ -1181,8 +1190,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 container's own -// padding and border on both ends of the axis. +// 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. // // Container-level recursion does no layout writes (no positions, no // alignment, no flex distribution); only the descendant leaf measure