Skip to content

Commit dcbe6bb

Browse files
GitHub Workflows Supportfacebook-github-bot
authored andcommitted
Count gaps in a flex container's min-content size (#2103)
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 <div style="display: flex; width: 200px"> <div style="display: flex; flex-shrink: 1; gap: 20px"> <!-- Chrome: 130px. Yoga: 90px --> <div style="width: 30px; flex-shrink: 0"></div> <div style="width: 30px; flex-shrink: 0"></div> <div style="width: 30px; flex-shrink: 0"></div> </div> <div style="width: 180px; flex-shrink: 0"></div> </div> ``` 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
1 parent 6ff2bcd commit dcbe6bb

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ static MinContentSize measureOrWalkMinContentMainSize(
10311031
const bool alongMainAxis = isRow(node->style().flexDirection()) == wantRow;
10321032

10331033
float minContent = 0.0f;
1034+
size_t itemCount = 0;
10341035
bool resolvesAgainstOwnerWidth = node->style().hasPercentPadding();
10351036

10361037
for (size_t i = 0; i < node->getChildCount(); i++) {
@@ -1039,6 +1040,7 @@ static MinContentSize measureOrWalkMinContentMainSize(
10391040
childStyle.positionType() == PositionType::Absolute) {
10401041
continue;
10411042
}
1043+
itemCount++;
10421044

10431045
const MinContentSize child = computeChildMinContentContribution(
10441046
node,
@@ -1056,6 +1058,13 @@ static MinContentSize measureOrWalkMinContentMainSize(
10561058
child.resolvesAgainstOwnerWidth || childStyle.hasPercentMargin();
10571059
}
10581060

1061+
// A percentage gap resolves against the size being found, so it counts as
1062+
// zero (css-sizing-3 §5.2.1).
1063+
if (alongMainAxis && itemCount > 1) {
1064+
minContent += node->style().computeGapForAxis(requestedAxis, 0.0f) *
1065+
static_cast<float>(itemCount - 1);
1066+
}
1067+
10591068
minContent += node->style().computeFlexStartPaddingAndBorder(
10601069
requestedAxis, direction, ownerWidth) +
10611070
node->style().computeFlexEndPaddingAndBorder(
@@ -1181,8 +1190,9 @@ static MinContentSize computeChildMinContentContribution(
11811190
// * Container: iterate in-flow children. For each, take its
11821191
// min-content contribution along `requestedAxis` plus its margins:
11831192
// the sum of them where that is the container's main axis, the
1184-
// largest where it is its cross axis. Add the container's own
1185-
// padding and border on both ends of the axis.
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.
11861196
//
11871197
// Container-level recursion does no layout writes (no positions, no
11881198
// alignment, no flex distribution); only the descendant leaf measure

0 commit comments

Comments
 (0)