Skip to content

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

Merged
meta-codesync[bot] merged 1 commit into
react:mainfrom
javache:export-D135502567
Dec 3, 2026
Merged

meta-codesync[bot] merged 1 commit into
react:mainfrom
javache:export-D135502567

Conversation

@javache

@javache javache commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Summary:

  • Proposal: measure a leaf's min-content height at the width it is given

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.

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

  • Resolve percentage margins in the min-content height walk against the 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.

  • Wrap text at its max-width when the min-content height walk has no 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.


Differential Revision: D135502567

@meta-codesync

meta-codesync Bot commented Dec 3, 2026

Copy link
Copy Markdown

@javache has exported this pull request. If you are a Meta employee, you can view the originating Diff in D135502567.

@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 3, 2026
@github-actions github-actions Bot removed the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 3, 2026
)

Summary:
* Proposal: measure a leaf's min-content height at the width it is given

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

* Resolve percentage margins in the min-content height walk against the 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.

* Wrap text at its max-width when the min-content height walk has no 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.

---------

Differential Revision: D135502567
@meta-codesync
meta-codesync Bot merged commit 20eae72 into react:main Dec 3, 2026
75 of 77 checks passed
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @javache in 20eae72

When will my fix make it into a release? | How to file a pick request?

@react-native-bot react-native-bot added the Merged This PR has been merged. label Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged This PR has been merged. meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants