Skip to content

Recompute a flex basis once its container has an exact main size (#2139) - #60546

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

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

Conversation

@javache

@javache javache commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

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

Summary:
Why

computeFlexBasisForChild uses an item's flex basis only when the container's main size is known; otherwise it measures the item's content. Once an item has a computed basis, the function keeps it: forever by default, and for the rest of the layout pass with WebFlexBasis. A container is often measured with no main size and then laid out at an exact one in the same pass, and its items then flex from the content sizes of the measurement instead of their flex bases:

<div style="width: 300px; flex-direction: row">
  <div style="width: 100px">
    <div style="flex-basis: 50px; flex-grow: 1; min-height: 0">
      <div style="height: 100px"></div>
    </div>
    <div style="flex-basis: 50px; flex-grow: 1"></div>
  </div>
  <div style="width: 50px; height: 200px"></div>
</div>

The row first measures the column with no height, then stretches it to 200. Chrome grows both items from 50 to 100. With WebFlexBasis, Yoga grows them from 100 and 50, the sizes of the measurement, to 125 and 75. A percentage flex basis has the same problem: it cannot resolve during the measurement, and the content size it falls back to is kept when the container's height is known.

The result also depends on the measurement cache. With the other box 300 high, a first layout gives the items 175 and 125; laying the tree out at 200 and then changing that height to 300 gives 150 and 150, which is Chrome's result, because the column's measurement is cached and the bases are computed only at its exact height.

How

With WebFlexBasis, computeFlexBasisForChild now replaces the computed basis with the resolved flex basis whenever the container is laid out at an exact main size (SizingMode::StretchFit), where the items flex from their flex bases.

A container sized by its content within an available size (SizingMode::FitContent) still keeps a basis computed earlier in the pass: since #2108 that basis is the item's max-content contribution, which is what sizes such a container, and flex_basis_length_of_flexible_item_in_content_sized_row fails if it is replaced there too. This leaves two differences from a browser, both present before this change. Such a container with a min-height or min-width larger than its content grows its items from the kept bases: the column above with align-self: flex-start and min-height: 200px gives 125 and 75. And a column measured with no height still takes its size from its items' content, so with the other box 20 high the column is 150 high where a browser makes it 100; its items change from 100 and 50 to 75 and 75 there, where a browser has 50 and 50.

WebFlexBasis gates the change because it already decides when this function recomputes a basis. Nothing changes for a config without it, which keeps the first basis as before.

Test Plan

yarn gentest -f YGFlexTest adds flex_basis_in_column_measured_then_stretched with Chrome's values: the markup above, plus a second column whose item has flex-basis: 50% and content 20 high. cmake --build tests/build && tests/build/yogatests passes. With yoga/algorithm/CalculateLayout.cpp reverted, the new test fails: the growing items are 125 and 75 high where Chrome has 100 and 100, and the flex-basis: 50% item is 20 high where Chrome has 100.

The cache dependence and the two remaining differences were measured with throwaway tests and Chrome fixtures of the markup above, which are not part of the commit. yarn format-check and yarn lint pass. Not run: the generated Java and JavaScript tests.

Changelog:
[General][Fixed] - Fix Yoga's WebFlexBasis experimental feature flexing items from their content sizes, not their flex bases, in a container that is measured before it gets its size

Signed-off-by: James Ide ide@expo.dev

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

…ct#2139)

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

Summary:
Why
===

`computeFlexBasisForChild` uses an item's flex basis only when the container's main size is known; otherwise it measures the item's content. Once an item has a computed basis, the function keeps it: forever by default, and for the rest of the layout pass with `WebFlexBasis`. A container is often measured with no main size and then laid out at an exact one in the same pass, and its items then flex from the content sizes of the measurement instead of their flex bases:

```html
<div style="width: 300px; flex-direction: row">
  <div style="width: 100px">
    <div style="flex-basis: 50px; flex-grow: 1; min-height: 0">
      <div style="height: 100px"></div>
    </div>
    <div style="flex-basis: 50px; flex-grow: 1"></div>
  </div>
  <div style="width: 50px; height: 200px"></div>
</div>
```

The row first measures the column with no height, then stretches it to 200. Chrome grows both items from 50 to 100. With `WebFlexBasis`, Yoga grows them from 100 and 50, the sizes of the measurement, to 125 and 75. A percentage flex basis has the same problem: it cannot resolve during the measurement, and the content size it falls back to is kept when the container's height is known.

The result also depends on the measurement cache. With the other box 300 high, a first layout gives the items 175 and 125; laying the tree out at 200 and then changing that height to 300 gives 150 and 150, which is Chrome's result, because the column's measurement is cached and the bases are computed only at its exact height.

How
===

With `WebFlexBasis`, `computeFlexBasisForChild` now replaces the computed basis with the resolved flex basis whenever the container is laid out at an exact main size (`SizingMode::StretchFit`), where the items flex from their flex bases.

A container sized by its content within an available size (`SizingMode::FitContent`) still keeps a basis computed earlier in the pass: since react#2108 that basis is the item's max-content contribution, which is what sizes such a container, and `flex_basis_length_of_flexible_item_in_content_sized_row` fails if it is replaced there too. This leaves two differences from a browser, both present before this change. Such a container with a `min-height` or `min-width` larger than its content grows its items from the kept bases: the column above with `align-self: flex-start` and `min-height: 200px` gives 125 and 75. And a column measured with no height still takes its size from its items' content, so with the other box 20 high the column is 150 high where a browser makes it 100; its items change from 100 and 50 to 75 and 75 there, where a browser has 50 and 50.

`WebFlexBasis` gates the change because it already decides when this function recomputes a basis. Nothing changes for a config without it, which keeps the first basis as before.

Test Plan
=========

`yarn gentest -f YGFlexTest` adds `flex_basis_in_column_measured_then_stretched` with Chrome's values: the markup above, plus a second column whose item has `flex-basis: 50%` and content 20 high. `cmake --build tests/build && tests/build/yogatests` passes. With `yoga/algorithm/CalculateLayout.cpp` reverted, the new test fails: the growing items are 125 and 75 high where Chrome has 100 and 100, and the `flex-basis: 50%` item is 20 high where Chrome has 100.

The cache dependence and the two remaining differences were measured with throwaway tests and Chrome fixtures of the markup above, which are not part of the commit. `yarn format-check` and `yarn lint` pass. Not run: the generated Java and JavaScript tests.

Changelog:
[General][Fixed] - Fix Yoga's `WebFlexBasis` experimental feature flexing items from their content sizes, not their flex bases, in a container that is measured before it gets its size
Signed-off-by: James Ide <ide@expo.dev>
@meta-codesync
meta-codesync Bot merged commit b4e035b into react:main Dec 4, 2026
48 of 52 checks passed
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @javache in b4e035b

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.

3 participants