Skip to content

Shrink the items of a fit-content row that is wider than its space (#2143) - #60549

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

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

Conversation

@javache

@javache javache commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Differential Revision: https://www.internalfb.com/diff/D135547835

Summary:
Why

A row that is not stretched, such as a row in a column with align-items: flex-start, is sized to fit its content. CSS makes it as wide as the space available when its content is wider than that, but no narrower than its min-content width, and then flexes its items in that width (the fit-content size, css-sizing-3 §2.1; css-flexbox-1 §9.2). Without the StretchFlexBasis erratum, Yoga keeps such a row at its max-content width and never shrinks its items:

<div style="width: 100px; align-items: flex-start">
  <div style="flex-direction: row">
    <div style="flex-shrink: 1">aaaa bbbb cccc</div>
    <div style="width: 40px; height: 10px"></div>
  </div>
</div>

Chrome makes the row 100 wide and wraps the text in the 60 left of it. Yoga makes the row 140 wide, with the text 100 wide beside the box.

How

When a row's main size is FitContent and a line is wider than the space available, calculateLayoutImpl now gives the row that space, or the row's min-content width if that is larger, and leaves the difference as negative free space for the items to shrink into. The min-content width is computeMinContentMainSize, which the automatic minimum size already uses, so the row stops shrinking where its items' stated sizes and unbreakable content add up: two width: 80px items keep a row 160 wide in a 100-wide column, as in Chrome, with or without automatic minimum sizes. A column is unchanged: Chrome keeps a column at its content's height, and Yoga already does.

The old behavior becomes the erratum FitContentRowKeepsMaxContentWidth, set on new configs and included in YGErrataAll and YGErrataClassic, as MinSizeUndefinedInsteadOfAuto and FlexFirstPassUsesRunningTotals are: clearing an erratum promises conformance, and a default config lays out as before. The configuring page names it in a sentence of its own, apart from the sentence that lists the other default errata, so that this change and other changes to that list do not conflict. With StretchFlexBasis set, Yoga already shrinks these items, to the space available and no further, and this change leaves that path alone.

A row that is itself an item of a row needs one more change. Its flex base size is its max-content width (css-flexbox-1 §9.2, step 3E), but Yoga measures an item for its flex basis within the width available to the container, and a row that shrinks to fit would shrink in that measure. With the erratum cleared, computeFlexBasisForChild measures a row item of a row with no available width. This applies in every row, not only one sized to fit its content: in a 100-wide row, a nested row of wrapping text and a 40-wide box was 140 wide with the text wrapped, and is now 180 wide with the text on one line, as in Chrome (fit_content_row_in_row_has_max_content_flex_basis). The nested row shrinks if it has flex-shrink. The change stops at rows because only a row shrinks in that measure; a column or a text node is still measured within the width available, so that text wraps to it, which is the third limit below.

Three limits remain with the erratum cleared:

  • A row whose items cannot shrink far enough is as wide as its items, where Chrome keeps it at the width available and lets the items overflow. A row sized to fit its content that holds the nested row above is 180 wide in Yoga and 100 in Chrome.
  • The min-content width does not floor an item that cannot shrink at its flex basis (css-flexbox-1 §9.9.3). For flex-basis: 80px; flex-shrink: 1 text beside a flex-basis: 80px box, Chrome makes the row 120 wide and Yoga 100.
  • An item of a row that is not a row itself, such as a column or text, is still measured for its flex basis within the width available, so text in it wraps where Chrome keeps it on one line. This is Yoga's behavior with or without the erratum.

Test Plan

yarn gentest -f YGFitContentRowTest, then cmake --build tests/build && tests/build/yogatests passes. The fixtures hold Chrome's layout with the errata cleared. With the erratum check forced on, which is the layout before this change, these fail:

Test Chrome Before
fit_content_row_shrinks_wrapping_text row 100 wide, text 60×30 row 140, text 100×20
fit_content_row_shrinks_without_automatic_minimum_sizes root height 30 20
fit_content_row_shrinks_inside_padding_and_gap root height 40 30
fit_content_row_stops_at_min_content_width row 120 wide, 50 high 140, 40
fit_content_scroll_row_shrinks_wrapping_text root height 30 10
fit_content_row_in_row_has_max_content_flex_basis nested row 180 wide 140

fit_content_row_keeps_stated_widths and fit_content_column_keeps_content_height pass before and after: the first fails if the row shrinks past its min-content width, and the second if a column shrinks. fit_content_row_stops_at_min_content_width also fails if the row's padding is counted twice. The hand-written fit_content_row_shrinks_only_without_errata checks that a new config has the erratum and keeps the old layout, that clearing it shrinks the row, and that YGErrataClassic, which includes it, keeps the old layout.

yarn format-check and yarn lint pass. The Java and JavaScript suites were generated but not run: this machine has no Emscripten or Gradle.

Changelog:
[General][Added] - Add the FitContentRowKeepsMaxContentWidth erratum; clearing it lets a row that is sized to fit its content shrink its items when the content is wider than the space available, and gives a row that is an item of a row its max-content width as its flex base size

@meta-codesync

meta-codesync Bot commented Dec 4, 2026

Copy link
Copy Markdown

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

…eact#2143)

Differential Revision: https://www.internalfb.com/diff/D135547835

Summary:
Why
===

A row that is not stretched, such as a row in a column with `align-items: flex-start`, is sized to fit its content. CSS makes it as wide as the space available when its content is wider than that, but no narrower than its min-content width, and then flexes its items in that width (the fit-content size, css-sizing-3 §2.1; css-flexbox-1 §9.2). Without the `StretchFlexBasis` erratum, Yoga keeps such a row at its max-content width and never shrinks its items:

```html
<div style="width: 100px; align-items: flex-start">
  <div style="flex-direction: row">
    <div style="flex-shrink: 1">aaaa bbbb cccc</div>
    <div style="width: 40px; height: 10px"></div>
  </div>
</div>
```

Chrome makes the row 100 wide and wraps the text in the 60 left of it. Yoga makes the row 140 wide, with the text 100 wide beside the box.

How
===

When a row's main size is `FitContent` and a line is wider than the space available, `calculateLayoutImpl` now gives the row that space, or the row's min-content width if that is larger, and leaves the difference as negative free space for the items to shrink into. The min-content width is `computeMinContentMainSize`, which the automatic minimum size already uses, so the row stops shrinking where its items' stated sizes and unbreakable content add up: two `width: 80px` items keep a row 160 wide in a 100-wide column, as in Chrome, with or without automatic minimum sizes. A column is unchanged: Chrome keeps a column at its content's height, and Yoga already does.

The old behavior becomes the erratum `FitContentRowKeepsMaxContentWidth`, set on new configs and included in `YGErrataAll` and `YGErrataClassic`, as `MinSizeUndefinedInsteadOfAuto` and `FlexFirstPassUsesRunningTotals` are: clearing an erratum promises conformance, and a default config lays out as before. The configuring page names it in a sentence of its own, apart from the sentence that lists the other default errata, so that this change and other changes to that list do not conflict. With `StretchFlexBasis` set, Yoga already shrinks these items, to the space available and no further, and this change leaves that path alone.

A row that is itself an item of a row needs one more change. Its flex base size is its max-content width (css-flexbox-1 §9.2, step 3E), but Yoga measures an item for its flex basis within the width available to the container, and a row that shrinks to fit would shrink in that measure. With the erratum cleared, `computeFlexBasisForChild` measures a row item of a row with no available width. This applies in every row, not only one sized to fit its content: in a 100-wide row, a nested row of wrapping text and a 40-wide box was 140 wide with the text wrapped, and is now 180 wide with the text on one line, as in Chrome (`fit_content_row_in_row_has_max_content_flex_basis`). The nested row shrinks if it has `flex-shrink`. The change stops at rows because only a row shrinks in that measure; a column or a text node is still measured within the width available, so that text wraps to it, which is the third limit below.

Three limits remain with the erratum cleared:

- A row whose items cannot shrink far enough is as wide as its items, where Chrome keeps it at the width available and lets the items overflow. A row sized to fit its content that holds the nested row above is 180 wide in Yoga and 100 in Chrome.
- The min-content width does not floor an item that cannot shrink at its flex basis (css-flexbox-1 §9.9.3). For `flex-basis: 80px; flex-shrink: 1` text beside a `flex-basis: 80px` box, Chrome makes the row 120 wide and Yoga 100.
- An item of a row that is not a row itself, such as a column or text, is still measured for its flex basis within the width available, so text in it wraps where Chrome keeps it on one line. This is Yoga's behavior with or without the erratum.

Test Plan
=========

`yarn gentest -f YGFitContentRowTest`, then `cmake --build tests/build && tests/build/yogatests` passes. The fixtures hold Chrome's layout with the errata cleared. With the erratum check forced on, which is the layout before this change, these fail:

| Test | Chrome | Before |
| --- | --- | --- |
| `fit_content_row_shrinks_wrapping_text` | row 100 wide, text 60×30 | row 140, text 100×20 |
| `fit_content_row_shrinks_without_automatic_minimum_sizes` | root height 30 | 20 |
| `fit_content_row_shrinks_inside_padding_and_gap` | root height 40 | 30 |
| `fit_content_row_stops_at_min_content_width` | row 120 wide, 50 high | 140, 40 |
| `fit_content_scroll_row_shrinks_wrapping_text` | root height 30 | 10 |
| `fit_content_row_in_row_has_max_content_flex_basis` | nested row 180 wide | 140 |

`fit_content_row_keeps_stated_widths` and `fit_content_column_keeps_content_height` pass before and after: the first fails if the row shrinks past its min-content width, and the second if a column shrinks. `fit_content_row_stops_at_min_content_width` also fails if the row's padding is counted twice. The hand-written `fit_content_row_shrinks_only_without_errata` checks that a new config has the erratum and keeps the old layout, that clearing it shrinks the row, and that `YGErrataClassic`, which includes it, keeps the old layout.

`yarn format-check` and `yarn lint` pass. The Java and JavaScript suites were generated but not run: this machine has no Emscripten or Gradle.

Changelog:
[General][Added] - Add the `FitContentRowKeepsMaxContentWidth` erratum; clearing it lets a row that is sized to fit its content shrink its items when the content is wider than the space available, and gives a row that is an item of a row its max-content width as its flex base size
@javache
javache force-pushed the export-D135547835 branch 2 times, most recently from 8e84e4d to 7cad85f Compare December 4, 2026 20:25

@Abbondanzo Abbondanzo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@meta-codesync
meta-codesync Bot merged commit 018bd47 into react:main Dec 4, 2026
11 of 16 checks passed
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @javache in 018bd47

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 4, 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.

4 participants