Skip to content

Always measure a leaf's min-content height at the width it is given - #2161

Merged
intergalacticspacehighway merged 1 commit into
react:mainfrom
expo:@ide/remove-min-content-height-at-given-width
Dec 2, 2026
Merged

intergalacticspacehighway merged 1 commit into
react:mainfrom
expo:@ide/remove-min-content-height-at-given-width

Conversation

@ide

@ide ide commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Why

#2105 added the min-content height walk that measures a leaf at its width, behind the experimental feature MinContentHeightAtGivenWidth, because it changes the arguments a host's measure function is called with. On #2105, the maintainer asked for a follow-up that removes the flag. The enum value is in no release of Yoga. If this lands in the same React Native release as #2105, that pull request's [Added] Changelog line for the feature should be left out of the release notes.

Without the feature, a height is probed with at most zero and no limit on the width, so wrapped text in a column shrinks below its lines:

<div style="display: flex; flex-direction: column; width: 50px; height: 20px">
  <div style="flex-shrink: 1">aaaa aaaa aaaa</div>   <!-- three lines. Chrome: 30px. Yoga: 10px -->
  <div style="height: 10px; flex-shrink: 0"></div>
</div>

Chrome, Yoga's main without the experimental feature, and this change for three of the fixtures

How

The experimental feature is removed from enums.py, and the two places in CalculateLayout.cpp that read it now always take the feature's path. Layouts change only with YGErrataMinSizeUndefinedInsteadOfAuto off, since only then does Yoga find a min-content size.

A host sees two changes. First, the arguments of a height probe: YGMinContentMeasureFunc, or the node's YGMeasureFunc when it has none, used to be called with an Undefined width and an AtMost 0 height. It is now called with the node's content width as an AtMost width, or an Undefined width when neither the node nor a box above it gives it one (below a row, or in a column sized by its content), and always with an Undefined height. A measure function that recognizes a height probe by AtMost 0, or that answers it by clamping its height to zero, no longer does; a leaf whose min-content height is a constant can state it with YGNodeSetMinContentHeight. A width probe is unchanged: an AtMost 0 width and an Undefined height. Second, a height result is keyed on the content width of the box around the leaf, so a clean leaf is measured again when that width changes, even if the leaf states its own width, and each box between the leaf and the flex item is walked again. A host that shares nodes between trees clones them for it. Yoga's main did this only with the feature on.

The same walk resolves a percentage margin or padding below the item against the content width of the box around it instead of the flex container's, which changes percent_margin_resolves_against_parent_content_width without any measure function.

YGNode.h documents the new arguments on YGMinContentMeasureFunc. No page of the website describes them.

Test Plan

python3 enums.py removes the enum value from the C, C++, Java and JavaScript enum files, and yarn gentest -f YGAutoMinSizeTest regenerates the fixtures, which no longer set the feature; Chrome's values are unchanged. cmake --build tests/build && tests/build/yogatests passes, node ./scripts/clang-format.js --check passes, and yarn gentest -h leaves the tree clean. The generated Java and JavaScript tests were not run.

With CalculateLayout.cpp reading the feature as off, which is main under a default set of experimental features, the 13 fixtures that used to set it fail. The height of the shrinking item:

Fixture Chrome Feature off
text_keeps_wrapped_lines 30 10
nested_text_keeps_wrapped_lines 35 10
text_wraps_inside_margin_and_padding 30 10
text_wraps_at_stated_width 30 10
text_wraps_at_stated_content_box_width 20 5
text_wraps_inside_padded_parent 30 10
percent_margin_resolves_against_parent_content_width 25 30
text_fits_wider_parent 10 5
text_fits_min_width 10 5
text_wraps_at_max_width 30 10
text_below_row_wraps_at_max_width 30 10
text_below_row_is_not_wrapped 10 5
text_in_wrapping_column_keeps_wrapped_lines 30 5

min_content_height_follows_given_width and min_content_height_without_owner_width_is_unconstrained fail there too, both on the measure modes, and the first on heights of 16 where it expects 32 and 48. min_content_height_probe_asks_for_at_most_zero pinned the old arguments and is removed; min_content_height_follows_given_width asserts the new ones, and nested_leaf_probed_at_most_once_per_axis_per_layout now asserts the width of 0 that a width probe passes.

No other test on main changes. Three more reach a height probe, auto_min_includes_leaf_padding_height, nested_flexbox_recurses_into_min_content and issue_1651_measured_items_freeze_at_lower_bound, and their measure functions return the same height for the old arguments and the new.

React Native's MinContentTest cases (packages/react-native/ReactCommon/yoga/test/BaselineTest.cpp), built against this tree, pass except testWalkClonesOnlyTheChildrenItWritesTo. It lays out a column a second time at a narrower width and expects the walk to clone a child with percentage padding below an item that states its width. That padding now resolves against the item's width, which did not change, so the walk reads the child and leaves it shared. The test needs to move to a row, where the walk still passes the container's width down; that version passes against Yoga before and after this change. react/react-native#60452 makes that change.

Changelog:
[General][Changed] - Yoga probes a measured leaf's min-content height with its content width as an AtMost width and an Undefined height, instead of an AtMost 0 height, so wrapped text in a column no longer shrinks below its lines when automatic minimum sizes are on

@meta-cla meta-cla Bot added the CLA Signed label Dec 2, 2026
@ide
ide force-pushed the @ide/remove-min-content-height-at-given-width branch from a2bc636 to dec7ed8 Compare December 2, 2026 01:28
@ide
ide marked this pull request as ready for review December 2, 2026 01:52
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Dec 2, 2026
Why
===
react#2105 added the min-content height walk that measures a leaf at its width, behind the experimental feature `MinContentHeightAtGivenWidth`, because it changes the arguments a host's measure function is called with. On react#2105, the maintainer asked for a follow-up that removes the flag. The enum value is in no release of Yoga. If this lands in the same React Native release as react#2105, that pull request's `[Added]` Changelog line for the feature should be left out of the release notes.

Without the feature, a height is probed with at most zero and no limit on the width, so wrapped text in a column shrinks below its lines:

```html
<div style="display: flex; flex-direction: column; width: 50px; height: 20px">
  <div style="flex-shrink: 1">aaaa aaaa aaaa</div>   <!-- three lines. Chrome: 30px. Yoga: 10px -->
  <div style="height: 10px; flex-shrink: 0"></div>
</div>
```

How
===
The experimental feature is removed from `enums.py`, and the two places in `CalculateLayout.cpp` that read it now always take the feature's path. Layouts change only with `YGErrataMinSizeUndefinedInsteadOfAuto` off, since only then does Yoga find a min-content size.

A host sees two changes. First, the arguments of a height probe: `YGMinContentMeasureFunc`, or the node's `YGMeasureFunc` when it has none, used to be called with an `Undefined` width and an `AtMost 0` height. It is now called with the node's content width as an `AtMost` width, or an `Undefined` width when neither the node nor a box above it gives it one (below a row, or in a column sized by its content), and always with an `Undefined` height. A measure function that recognizes a height probe by `AtMost 0`, or that answers it by clamping its height to zero, no longer does; a leaf whose min-content height is a constant can state it with `YGNodeSetMinContentHeight`. A width probe is unchanged: an `AtMost 0` width and an `Undefined` height. Second, a height result is keyed on the content width of the box around the leaf, so a clean leaf is measured again when that width changes, even if the leaf states its own width, and each box between the leaf and the flex item is walked again. A host that shares nodes between trees clones them for it. Yoga's `main` did this only with the feature on.

The same walk resolves a percentage margin or padding below the item against the content width of the box around it instead of the flex container's, which changes `percent_margin_resolves_against_parent_content_width` without any measure function.

`YGNode.h` documents the new arguments on `YGMinContentMeasureFunc`. No page of the website describes them.

Test Plan
=========
`python3 enums.py` removes the enum value from the C, C++, Java and JavaScript enum files, and `yarn gentest -f YGAutoMinSizeTest` regenerates the fixtures, which no longer set the feature; Chrome's values are unchanged. `cmake --build tests/build && tests/build/yogatests` passes, `node ./scripts/clang-format.js --check` passes, and `yarn gentest -h` leaves the tree clean. The generated Java and JavaScript tests were not run.

With `CalculateLayout.cpp` reading the feature as off, which is `main` under a default set of experimental features, the 13 fixtures that used to set it fail. The height of the shrinking item:

| Fixture | Chrome | Feature off |
| --- | --- | --- |
| `text_keeps_wrapped_lines` | 30 | 10 |
| `nested_text_keeps_wrapped_lines` | 35 | 10 |
| `text_wraps_inside_margin_and_padding` | 30 | 10 |
| `text_wraps_at_stated_width` | 30 | 10 |
| `text_wraps_at_stated_content_box_width` | 20 | 5 |
| `text_wraps_inside_padded_parent` | 30 | 10 |
| `percent_margin_resolves_against_parent_content_width` | 25 | 30 |
| `text_fits_wider_parent` | 10 | 5 |
| `text_fits_min_width` | 10 | 5 |
| `text_wraps_at_max_width` | 30 | 10 |
| `text_below_row_wraps_at_max_width` | 30 | 10 |
| `text_below_row_is_not_wrapped` | 10 | 5 |
| `text_in_wrapping_column_keeps_wrapped_lines` | 30 | 5 |

`min_content_height_follows_given_width` and `min_content_height_without_owner_width_is_unconstrained` fail there too, both on the measure modes, and the first on heights of 16 where it expects 32 and 48. `min_content_height_probe_asks_for_at_most_zero` pinned the old arguments and is removed; `min_content_height_follows_given_width` asserts the new ones, and `nested_leaf_probed_at_most_once_per_axis_per_layout` now asserts the width of 0 that a width probe passes.

No other test on `main` changes. Three more reach a height probe, `auto_min_includes_leaf_padding_height`, `nested_flexbox_recurses_into_min_content` and `issue_1651_measured_items_freeze_at_lower_bound`, and their measure functions return the same height for the old arguments and the new.

React Native's `MinContentTest` cases (`packages/react-native/ReactCommon/yoga/test/BaselineTest.cpp`), built against this tree, pass except `testWalkClonesOnlyTheChildrenItWritesTo`. It lays out a column a second time at a narrower width and expects the walk to clone a child with percentage padding below an item that states its width. That padding now resolves against the item's width, which did not change, so the walk reads the child and leaves it shared. The test needs to move to a row, where the walk still passes the container's width down; that version passes against Yoga before and after this change. react/react-native#60452 makes that change.

Changelog:
[General][Changed] - Yoga probes a measured leaf's min-content height with its content width as an `AtMost` width and an `Undefined` height, instead of an `AtMost 0` height, so wrapped text in a column no longer shrinks below its lines when automatic minimum sizes are on
@intergalacticspacehighway
intergalacticspacehighway force-pushed the @ide/remove-min-content-height-at-given-width branch from dec7ed8 to baa3140 Compare December 2, 2026 08:01

@intergalacticspacehighway intergalacticspacehighway left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging; we'll revert and keep the flag if something breaks internally at Meta.

@intergalacticspacehighway
intergalacticspacehighway merged commit c9dba14 into react:main Dec 2, 2026
34 checks passed
@ide
ide deleted the @ide/remove-min-content-height-at-given-width branch December 2, 2026 08:19
ide added a commit to expo/yoga that referenced this pull request Dec 3, 2026
Why
===

With `MinSizeUndefinedInsteadOfAuto` cleared, a flex item's automatic minimum size comes from a walk of its subtree. Since react#2161, the walk measures a leaf's min-content height at the leaf's own content width, which it derives from the width of each box around it, and it resolves the same style values more than once per box. Relaying out `rendering-sample-mac` with no errata after one text grows counts 256,769 instructions with GCC on react#2218, against 246,928 at `3a201ff47`, an earlier main.

How
===

A container resolves its direction once and passes it to `contentWidthUnderOwner`, which resolved it again. A measured leaf still reads its direction after its measure function returns, as a host can change it there. `contentWidthUnderOwner` subtracts margins only from a defined owner width. When the box has no width to fill, it bounds its maximum width once, instead of first bounding an undefined width, which stays undefined. The stated width and height are resolved from the style's stored value with `Style::resolvedDimension`, without building a `StyleSizeLength`. A child's stated size across the axis is resolved only when it has an aspect ratio to transfer it.

Test Plan
===

`cmake --build tests/build && tests/build/yogatests` passes. `leaf_with_overflowing_margins_is_measured_at_max_width` gives a leaf opposite percentage margins whose sum overflows to NaN under a defined owner width, and checks that its min-content height is measured at its `max-width`. `leaf_padding_follows_direction_set_while_measuring` sets a leaf's direction to RTL from its min-content measure function, and checks that its automatic minimum width includes the padding it has in RTL.

Random trees produce the same layouts as main: 200,000 each from the varied and nested generators with default configs, 300,000 flat rows, and 200,000 varied trees each with `MinSizeUndefinedInsteadOfAuto` cleared, with no errata, and with configs that differ between nodes.

Callgrind counts the instructions in one `YGNodeCalculateLayout` call, comparing react#2218 with this change. "All errata" is the configuration each capture records, React Native's; "no errata" clears every erratum. GCC's first layouts are counted with CI's method; Clang's and the relayouts with a harness that replays the same captures.

**Relayouts with no errata**

| Relayout | Compiler | `3a201ff47` | Main | react#2218 | This change | Change from react#2218 | Change from `3a201ff47` |
| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: |
| rendering-sample-mac, one text grows | GCC 15.2 x86-64 | 246,928 | 333,330 | 256,769 | 236,553 | −7.87% | −4.20% |
| rendering-sample-mac, one text grows | Clang 18.1 arm64 | 264,393 | 347,824 | 256,364 | 239,304 | −6.65% | −9.49% |
| profile-ios, then the root narrows by 20pt | GCC 15.2 x86-64 | 1,162,027 | 1,268,056 | 1,101,596 | 1,098,279 | −0.30% | −5.49% |
| profile-ios, then the root narrows by 20pt | Clang 18.1 arm64 | 1,313,623 | 1,365,936 | 1,149,762 | 1,147,321 | −0.21% | −12.66% |
| chat-mac, one text grows | GCC 15.2 x86-64 | 1,099,172 | 1,110,516 | 987,547 | 983,130 | −0.45% | −10.56% |
| chat-mac, one text grows | Clang 18.1 arm64 | 1,062,258 | 1,029,942 | 872,183 | 869,089 | −0.35% | −18.18% |

The other relayouts change by −0.91% to +0.00% with GCC and −0.67% to +0.00% with Clang.

**First layout**

| Capture | Errata | GCC 15.2 x86-64 | Change | Clang 18.1 arm64 | Change |
| --- | --- | ---: | ---: | ---: | ---: |
| chat-mac | all errata | 42,238,101 → 42,238,384 | +0.00% | 43,685,141 → 43,685,141 | +0.00% |
| chat-mac | no errata | 49,472,849 → 49,297,928 | −0.35% | 51,298,624 → 51,157,887 | −0.27% |
| feed-android | all errata | 8,236,026 → 8,236,019 | −0.00% | 8,566,118 → 8,566,118 | +0.00% |
| feed-android | no errata | 8,975,977 → 8,922,530 | −0.60% | 9,255,504 → 9,210,881 | −0.48% |
| profile-ios | all errata | 1,627,904 → 1,627,883 | −0.00% | 1,698,555 → 1,698,555 | +0.00% |
| profile-ios | no errata | 1,549,111 → 1,540,062 | −0.58% | 1,612,305 → 1,604,555 | −0.48% |
| rendering-sample-mac | all errata | 749,944 → 749,944 | +0.00% | 764,552 → 764,552 | +0.00% |
| rendering-sample-mac | no errata | 913,338 → 889,504 | −2.61% | 934,103 → 914,623 | −2.09% |

Changelog: [Internal]
ide added a commit to expo/yoga that referenced this pull request Dec 4, 2026
Why
===

With `MinSizeUndefinedInsteadOfAuto` cleared, a flex item's automatic minimum size comes from a walk of its subtree. Since react#2161, the walk measures a leaf's min-content height at the leaf's own content width, which it derives from the width of each box around it, and it resolves the same style values more than once per box. Relaying out `rendering-sample-mac` with no errata after one text grows counts 256,769 instructions with GCC on react#2218, against 246,928 at `3a201ff47`, an earlier main.

How
===

A container resolves its direction once and passes it to `contentWidthUnderOwner`, which resolved it again. A measured leaf still reads its direction after its measure function returns, as a host can change it there. `contentWidthUnderOwner` subtracts margins only from a defined owner width. When the box has no width to fill, it bounds its maximum width once, instead of first bounding an undefined width, which stays undefined. The stated width and height are resolved from the style's stored value with `Style::resolvedDimension`, without building a `StyleSizeLength`. A child's stated size across the axis is resolved only when it has an aspect ratio to transfer it.

Test Plan
===

`cmake --build tests/build && tests/build/yogatests` passes. `leaf_with_overflowing_margins_is_measured_at_max_width` gives a leaf opposite percentage margins whose sum overflows to NaN under a defined owner width, and checks that its min-content height is measured at its `max-width`. `leaf_padding_follows_direction_set_while_measuring` sets a leaf's direction to RTL from its min-content measure function, and checks that its automatic minimum width includes the padding it has in RTL.

Random trees produce the same layouts as main: 200,000 each from the varied and nested generators with default configs, 300,000 flat rows, and 200,000 varied trees each with `MinSizeUndefinedInsteadOfAuto` cleared, with no errata, and with configs that differ between nodes.

Callgrind counts the instructions in one `YGNodeCalculateLayout` call, comparing react#2218 with this change. "All errata" is the configuration each capture records, React Native's; "no errata" clears every erratum. GCC's first layouts are counted with CI's method; Clang's and the relayouts with a harness that replays the same captures.

**Relayouts with no errata**

| Relayout | Compiler | `3a201ff47` | Main | react#2218 | This change | Change from react#2218 | Change from `3a201ff47` |
| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: |
| rendering-sample-mac, one text grows | GCC 15.2 x86-64 | 246,928 | 333,330 | 256,769 | 236,553 | −7.87% | −4.20% |
| rendering-sample-mac, one text grows | Clang 18.1 arm64 | 264,393 | 347,824 | 256,364 | 239,304 | −6.65% | −9.49% |
| profile-ios, then the root narrows by 20pt | GCC 15.2 x86-64 | 1,162,027 | 1,268,056 | 1,101,596 | 1,098,279 | −0.30% | −5.49% |
| profile-ios, then the root narrows by 20pt | Clang 18.1 arm64 | 1,313,623 | 1,365,936 | 1,149,762 | 1,147,321 | −0.21% | −12.66% |
| chat-mac, one text grows | GCC 15.2 x86-64 | 1,099,172 | 1,110,516 | 987,547 | 983,130 | −0.45% | −10.56% |
| chat-mac, one text grows | Clang 18.1 arm64 | 1,062,258 | 1,029,942 | 872,183 | 869,089 | −0.35% | −18.18% |

The other relayouts change by −0.91% to +0.00% with GCC and −0.67% to +0.00% with Clang.

**First layout**

| Capture | Errata | GCC 15.2 x86-64 | Change | Clang 18.1 arm64 | Change |
| --- | --- | ---: | ---: | ---: | ---: |
| chat-mac | all errata | 42,238,101 → 42,238,384 | +0.00% | 43,685,141 → 43,685,141 | +0.00% |
| chat-mac | no errata | 49,472,849 → 49,297,928 | −0.35% | 51,298,624 → 51,157,887 | −0.27% |
| feed-android | all errata | 8,236,026 → 8,236,019 | −0.00% | 8,566,118 → 8,566,118 | +0.00% |
| feed-android | no errata | 8,975,977 → 8,922,530 | −0.60% | 9,255,504 → 9,210,881 | −0.48% |
| profile-ios | all errata | 1,627,904 → 1,627,883 | −0.00% | 1,698,555 → 1,698,555 | +0.00% |
| profile-ios | no errata | 1,549,111 → 1,540,062 | −0.58% | 1,612,305 → 1,604,555 | −0.48% |
| rendering-sample-mac | all errata | 749,944 → 749,944 | +0.00% | 764,552 → 764,552 | +0.00% |
| rendering-sample-mac | no errata | 913,338 → 889,504 | −2.61% | 934,103 → 914,623 | −2.09% |

Changelog: [Internal]
ide added a commit to expo/react-native that referenced this pull request Dec 5, 2026
`MinContentTest.testWalkClonesOnlyTheChildrenItWritesTo` lays out a column a second time at a narrower width and expects Yoga's min-content walk to clone one child of a shrinking item: the child with percentage padding, whose memo is keyed on the owner's width. The item states its width. Once Yoga always measures a min-content height at the width a box is given (react/yoga#2105, and react/yoga#2161, which removes the `MinContentHeightAtGivenWidth` experimental feature), the height walk resolves that padding against the item's width, which is the same in both trees, so it reads the child and clones nothing, and the test fails.

The test now uses a row. A width walk still passes the container's width down and resolves the padding against it, so the padded child's memo is still keyed on a width that changes. A static min-content width of 100 on one leaf keeps the item 100 wide in both trees, so the item's layout comes from cache and only the walk can clone its children. The assertions are the same, preceded by an `ASSERT_FLOAT_EQ` on the item's width: an item that does not keep its width is laid out again, which clones its children by itself, so the test stops there.

`testOwnPercentageMarginLeavesChildrenShared` moves to a row the same way. It claims that a child's own percentage margin keys the item's memo and not the child's, so a narrower second tree walks the item again but only reads the child. In the column, nothing checked that the item is walked again: with a container's memo kept for any owner width, the test passed. With react/yoga#2161 the column also stops depending on the margin, since the measured leaf's min-content height is keyed on the width it is measured at, which keys the item's memo by itself. In a row, the width walk resolves the margin against the root's width, and the margin is the only percentage below the item. The test now also checks the item's memoized owner width (`LayoutResults::minContentOwnerWidth`), which is the new width only when the walk walked the item again.

`BaselineTest.cpp`, built with GoogleTest against three copies of Yoga, passes: the vendored copy at this commit, react/yoga at `b6c58a63`, the commit before react/yoga#2161, where the experimental feature is off, and react/yoga at `c9dba147` (react/yoga#2161). It also passes against react/yoga's `main` at `403c3632`, and under `ctest` when built as `private/react-native-fantom/tester/react-common-tests/CMakeLists.txt` builds `ReactCommon/yoga/test`, against React Native's `yogacore` and GoogleTest 1.18.0.

Each check fails in all three copies when `CalculateLayout.cpp` breaks what it covers. In `testOwnPercentageMarginLeavesChildrenShared`, the owner width check fails, at 300, when a child's percentage margin no longer keys the item's memo or when a container's memo holds for any owner width; the column version passed with both. The content check fails when the walk clones every child it visits, and the leaf check when no memo from an earlier layout holds. With static min-content sizes ignored, both tests stop at their width checks, at 73; `testWalkClonesOnlyTheChildrenItWritesTo` used to report six more failures after it.

Signed-off-by: James Ide <ide@expo.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants