From 7b5086180d2d49a423e8e1bc6ff648dac122dabd Mon Sep 17 00:00:00 2001 From: James Ide Date: Tue, 1 Dec 2026 19:34:38 -0800 Subject: [PATCH] Recompute a flex basis once its container has an exact main size (#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
``` 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 --- .../yoga/yoga/algorithm/CalculateLayout.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 398e5e9af6f..f72d8f092c1 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -271,10 +271,18 @@ static void computeFlexBasisForChild( resolvedFlexBasis.isDefined() && yoga::isDefined(mainAxisSize); if (useResolvedFlexBasis) { + // A basis computed earlier in this layout may come from the item's + // content, if the container was first measured with no main size. Replace + // it once the container has an exact main size, where the items flex from + // their flex bases. A container sized by its content within an available + // size keeps it, as the item's contribution to that size. + const bool hasExactMainSize = + (isMainAxisRow ? widthMode : heightMode) == SizingMode::StretchFit; if (child->getLayout().computedFlexBasis.isUndefined() || (child->getConfig()->isExperimentalFeatureEnabled( ExperimentalFeature::WebFlexBasis) && - child->getLayout().computedFlexBasisGeneration != generationCount)) { + (hasExactMainSize || + child->getLayout().computedFlexBasisGeneration != generationCount))) { const FloatOptional paddingAndBorder = FloatOptional( paddingAndBorderForAxis(child, mainAxis, direction, ownerWidth)); child->setLayoutComputedFlexBasis(