Skip to content

Measure a leaf's min-content height at the width it is given - #2105

Merged
intergalacticspacehighway merged 3 commits into
react:mainfrom
expo:@ide/automin-leaf-height
Dec 2, 2026
Merged

intergalacticspacehighway merged 3 commits into
react:mainfrom
expo:@ide/automin-leaf-height

Conversation

@ide

@ide ide commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Why

A flex item does not shrink below its min-content size (css-flexbox-1 §4.5). In a column that size is a height, and for text it is the height of the lines it wraps to at the box's width (css-sizing-3 §2.1: a block container's min-content block size equals its max-content block size). Yoga's min-content walk probes a measured leaf with at most zero on the probed axis and no limit on the other, so a text measurer answers with zero or with one unbroken line, and the text overflows its box:

<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>

This is a proposal because it changes the arguments a host's measure function is called with.

How

With the experimental feature MinContentHeightAtGivenWidth, the walk measures the leaf at its content width (AtMost) with no limit on its height. Without the feature nothing changes.

Chrome, the feature off and the feature on for wrapping text in a column too short for it

The walk finds the width on the way down from the flex container: a box's stated width, or its owner's content box less its margins, within its minimum and maximum. That is the box's width when it is stretched in a column; a narrower box that is not stretched may wrap to more lines than were measured. A row's children are walked against no width, so text below a row counts as unwrapped unless it, or a box between it and the row, states a width. In both cases the item can shrink further than its content needs. A wrapping column is treated as a column, since Chrome wraps its text at the column's width (text_in_wrapping_column_keeps_wrapped_lines). In a height walk, a percentage padding below the item resolves against this width.

The leaf's height now depends on the owner's width, so its memo and those above it on that axis are keyed on minContentOwnerWidth (the existing minContentResolvesAgainstOwnerWidth flag). A new width measures the leaf again.

The gate is the open question. The layout change exists only with MinSizeUndefinedInsteadOfAuto off; what needs a gate is the callback contract. YGMinContentMeasureFunc, and a leaf's YGMeasureFunc when it has none, is documented as called with AtMost 0 on the probed axis, and a host that recognizes a height probe by those arguments would stop recognizing it. An experimental feature lets each host turn the fix on when its measure functions are ready. The alternative is an erratum that keeps the old probe, set by default like MinSizeUndefinedInsteadOfAuto: one switch fewer for a host that asks for YGErrataNone, but it changes the callback arguments for every such host at once. I will switch to it if you prefer. YGNode.h documents the new arguments either way.

React Native's MinContentTest cases (packages/react-native/ReactCommon/yoga/test/BaselineTest.cpp) assert that a new width leaves a clean leaf shared. With the feature on, three of them hold only if the leaf's height is static (YGNodeSetMinContentHeight).

Test Plan

python3 enums.py regenerates the enum files with no change, yarn gentest -f YGAutoMinSizeTest generates the new fixtures from Chrome, and cmake --build tests/build && tests/build/yogatests passes with them and with the hand-written tests for the measure function's arguments and the memo.

The height of the shrinking item, in Chrome and with the change to CalculateLayout.cpp reverted:

Fixture Chrome Without the change
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
text_wraps_at_max_width 30 10
text_in_wrapping_column_keeps_wrapped_lines 30 5

text_fits_wider_parent, text_fits_min_width and text_below_row_is_not_wrapped keep text wider than the container on one line (10). Without the change they give 5, and measuring at the container's width instead of the box's would give 30. min_content_height_follows_given_width gets 16 where it expects 32 and then 48, and min_content_height_without_owner_width_is_unconstrained fails on the measure modes. min_content_height_probe_asks_for_at_most_zero pins the contract with the feature off, and passes with and without the change.

yarn gentest -h leaves the tree clean, and node ./scripts/clang-format.js --check passes. The generated Java and JavaScript tests were not run.

Changelog:
[General][Added] - Add the Yoga experimental feature MinContentHeightAtGivenWidth, which keeps wrapped text in a column from shrinking below its lines when automatic minimum sizes are on

@meta-cla meta-cla Bot added the CLA Signed label Nov 29, 2026
@ide
ide force-pushed the @ide/automin-leaf-height branch 2 times, most recently from f700360 to ab0607d Compare November 29, 2026 12:17
@ide
ide marked this pull request as ready for review November 29, 2026 12:27
@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 Nov 29, 2026
@ide
ide force-pushed the @ide/automin-leaf-height branch from ab0607d to 1493405 Compare December 1, 2026 01:23
@ide ide closed this Dec 1, 2026
@ide ide reopened this Dec 1, 2026
@ide
ide force-pushed the @ide/automin-leaf-height branch 3 times, most recently from 182d0f6 to 2341679 Compare December 1, 2026 10:40
Why
===
A flex item does not shrink below its min-content size (css-flexbox-1 §4.5). In a column that size is a height, and for text it is the height of the lines it wraps to at the box's width (css-sizing-3 §2.1: a block container's min-content block size equals its max-content block size). Yoga's min-content walk probes a measured leaf with at most zero on the probed axis and no limit on the other, so a text measurer answers with zero or with one unbroken line, and the text overflows its box:

```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>
```

This is a proposal because it changes the arguments a host's measure function is called with.

How
===
With the experimental feature `MinContentHeightAtGivenWidth`, the walk measures the leaf at its content width (`AtMost`) with no limit on its height. Without the feature nothing changes.

The walk finds the width on the way down from the flex container: a box's stated width, or its owner's content box less its margins, within its minimum and maximum. That is the box's width when it is stretched in a column; a narrower box that is not stretched may wrap to more lines than were measured. A row's children are walked against no width, so text below a row counts as unwrapped unless it, or a box between it and the row, states a width. In both cases the item can shrink further than its content needs. A wrapping column is treated as a column, since Chrome wraps its text at the column's width (`text_in_wrapping_column_keeps_wrapped_lines`). In a height walk, a percentage padding below the item resolves against this width.

The leaf's height now depends on the owner's width, so its memo and those above it on that axis are keyed on `minContentOwnerWidth` (the existing `minContentResolvesAgainstOwnerWidth` flag). A new width measures the leaf again.

The gate is the open question. The layout change exists only with `MinSizeUndefinedInsteadOfAuto` off; what needs a gate is the callback contract. `YGMinContentMeasureFunc`, and a leaf's `YGMeasureFunc` when it has none, is documented as called with `AtMost 0` on the probed axis, and a host that recognizes a height probe by those arguments would stop recognizing it. An experimental feature lets each host turn the fix on when its measure functions are ready. The alternative is an erratum that keeps the old probe, set by default like `MinSizeUndefinedInsteadOfAuto`: one switch fewer for a host that asks for `YGErrataNone`, but it changes the callback arguments for every such host at once. I will switch to it if you prefer. `YGNode.h` documents the new arguments either way.

React Native's `MinContentTest` cases (`packages/react-native/ReactCommon/yoga/test/BaselineTest.cpp`) assert that a new width leaves a clean leaf shared. With the feature on, three of them hold only if the leaf's height is static (`YGNodeSetMinContentHeight`).

Test Plan
=========
`python3 enums.py` regenerates the enum files with no change, `yarn gentest -f YGAutoMinSizeTest` generates the new fixtures from Chrome, and `cmake --build tests/build && tests/build/yogatests` passes with them and with the hand-written tests for the measure function's arguments and the memo.

The height of the shrinking item, in Chrome and with the change to `CalculateLayout.cpp` reverted:

| Fixture | Chrome | Without the change |
| --- | --- | --- |
| `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 |
| `text_wraps_at_max_width` | 30 | 10 |
| `text_in_wrapping_column_keeps_wrapped_lines` | 30 | 5 |

`text_fits_wider_parent`, `text_fits_min_width` and `text_below_row_is_not_wrapped` keep text wider than the container on one line (10). Without the change they give 5, and measuring at the container's width instead of the box's would give 30. `min_content_height_follows_given_width` gets 16 where it expects 32 and then 48, and `min_content_height_without_owner_width_is_unconstrained` fails on the measure modes. `min_content_height_probe_asks_for_at_most_zero` pins the contract with the feature off, and passes with and without the change.

`yarn gentest -h` leaves the tree clean, and `node ./scripts/clang-format.js --check` passes. The generated Java and JavaScript tests were not run.

Changelog:
[General][Added] - Add the Yoga experimental feature MinContentHeightAtGivenWidth, which keeps wrapped text in a column from shrinking below its lines when automatic minimum sizes are on
… parent's content width

The walk gave each child its parent's content width for its padding, but its
margins still used the grandparent's width.
Below a row the walk passes no width, so a box's max-width was never applied
and its text was measured on one line.

@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.

Pushed two fixes to the walk: percentage margins now resolve against the parent's content width, and max-width applies when the width is unknown. Let's do a follow-up to remove the flag. I'll merge it and we can see if anything fails at Meta internally.

@intergalacticspacehighway intergalacticspacehighway changed the title Proposal: measure a leaf's min-content height at the width it is given Measure a leaf's min-content height at the width it is given Dec 2, 2026
@intergalacticspacehighway
intergalacticspacehighway merged commit a94a3b5 into react:main Dec 2, 2026
34 checks passed
@ide
ide deleted the @ide/automin-leaf-height branch December 2, 2026 01:03
ide added a commit to expo/yoga that referenced this pull request 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
@ide

ide commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

@intergalacticspacehighway Thanks for the two fixes. #2161 removes MinContentHeightAtGivenWidth, so the walk always measures a leaf at the width it is given.

With your fixes, removing the flag makes one of React Native's MinContentTest cases fail: testWalkClonesOnlyTheChildrenItWritesTo expects a clone that the walk no longer needs. react/react-native#60452 moves that test to a row, where it passes against Yoga both before and after #2161, so it can land first.

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