Skip to content

Commit ed46a55

Browse files
idemeta-codesync[bot]
authored andcommitted
Size a wrapping row's min-content width by its widest child (#2104)
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 <div style="display: flex; width: 200px"> <div style="display: flex; flex-wrap: wrap; flex-shrink: 1; gap: 20px"> <!-- Chrome: 70px wide, two lines. Yoga: 140px wide, one line --> <div style="width: 50px; height: 10px"></div> <div style="width: 60px; height: 10px; margin: 0 5px"></div> </div> <div style="width: 180px; flex-shrink: 0"></div> </div> ``` 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 <ide@expo.dev>
1 parent 2d0f6f0 commit ed46a55

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

‎packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,8 +1027,12 @@ static MinContentSize measureOrWalkMinContentMainSize(
10271027

10281028
const Direction direction = node->resolveDirection(ownerDirection);
10291029
// Children add up along the container's main axis; across it the largest
1030-
// counts.
1031-
const bool alongMainAxis = isRow(node->style().flexDirection()) == wantRow;
1030+
// counts. A wrapping row can put each child on a line of its own, so its
1031+
// largest child counts along it too (css-flexbox-1 §9.9.1). A wrapping
1032+
// column still adds up: Chrome finds a min-content height by layout at an
1033+
// indefinite height, where nothing wraps.
1034+
const bool childrenAddUp = isRow(node->style().flexDirection()) == wantRow &&
1035+
(node->style().flexWrap() == Wrap::NoWrap || !wantRow);
10321036

10331037
float minContent = 0.0f;
10341038
size_t itemCount = 0;
@@ -1052,15 +1056,15 @@ static MinContentSize measureOrWalkMinContentMainSize(
10521056
generationCount);
10531057
const float childSize =
10541058
child.size + childStyle.computeMarginForAxis(requestedAxis, ownerWidth);
1055-
minContent = alongMainAxis ? minContent + childSize
1059+
minContent = childrenAddUp ? minContent + childSize
10561060
: std::max(minContent, childSize);
10571061
resolvesAgainstOwnerWidth = resolvesAgainstOwnerWidth ||
10581062
child.resolvesAgainstOwnerWidth || childStyle.hasPercentMargin();
10591063
}
10601064

10611065
// A percentage gap resolves against the size being found, so it counts as
10621066
// zero (css-sizing-3 §5.2.1).
1063-
if (alongMainAxis && itemCount > 1) {
1067+
if (childrenAddUp && itemCount > 1) {
10641068
minContent += node->style().computeGapForAxis(requestedAxis, 0.0f) *
10651069
static_cast<float>(itemCount - 1);
10661070
}
@@ -1190,9 +1194,9 @@ static MinContentSize computeChildMinContentContribution(
11901194
// * Container: iterate in-flow children. For each, take its
11911195
// min-content contribution along `requestedAxis` plus its margins:
11921196
// the sum of them where that is the container's main axis, the
1193-
// largest where it is its cross axis. Add the gaps between them
1194-
// along the main axis, and the container's own padding and border
1195-
// on both ends of the axis.
1197+
// largest where it is its cross axis or the container is a
1198+
// wrapping row. Add the gaps between the children that add up, and
1199+
// the container's own padding and border on both ends of the axis.
11961200
//
11971201
// Container-level recursion does no layout writes (no positions, no
11981202
// alignment, no flex distribution); only the descendant leaf measure

0 commit comments

Comments
 (0)