From fb397a5425309d07eb01777f06ade4b052fba129 Mon Sep 17 00:00:00 2001 From: James Ide Date: Fri, 4 Dec 2026 06:58:05 -0800 Subject: [PATCH] Shrink the items of a fit-content row that is wider than its space (#2143) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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
aaaa bbbb cccc
``` 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 --- .../main/java/com/facebook/yoga/YogaErrata.kt | 2 + .../ReactCommon/yoga/yoga/YGEnums.cpp | 2 + .../ReactCommon/yoga/yoga/YGEnums.h | 1 + .../yoga/yoga/algorithm/CalculateLayout.cpp | 56 ++++++++++++++----- .../ReactCommon/yoga/yoga/config/Config.h | 3 +- .../ReactCommon/yoga/yoga/enums/Errata.h | 1 + .../api-snapshots/ReactAndroidDebugCxx.api | 1 + .../ReactAndroidFrameworksCxx.api | 1 + .../api-snapshots/ReactAndroidNewarchCxx.api | 1 + .../api-snapshots/ReactAndroidReleaseCxx.api | 1 + .../api-snapshots/ReactAppleDebugCxx.api | 1 + .../api-snapshots/ReactAppleFrameworksCxx.api | 1 + .../api-snapshots/ReactAppleNewarchCxx.api | 1 + .../api-snapshots/ReactAppleReleaseCxx.api | 1 + .../api-snapshots/ReactCommonDebugCxx.api | 1 + .../ReactCommonFrameworksCxx.api | 1 + .../api-snapshots/ReactCommonNewarchCxx.api | 1 + .../api-snapshots/ReactCommonReleaseCxx.api | 1 + 18 files changed, 63 insertions(+), 14 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaErrata.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaErrata.kt index ff2bcaf1b637..ba0ddbe1cec6 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaErrata.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaErrata.kt @@ -16,6 +16,7 @@ public enum class YogaErrata(public val intValue: Int) { ABSOLUTE_PERCENT_AGAINST_INNER_SIZE(4), MIN_SIZE_UNDEFINED_INSTEAD_OF_AUTO(8), FLEX_FIRST_PASS_USES_RUNNING_TOTALS(16), + FIT_CONTENT_ROW_KEEPS_MAX_CONTENT_WIDTH(32), ALL(2147483647), CLASSIC(2147483646); @@ -31,6 +32,7 @@ public enum class YogaErrata(public val intValue: Int) { 4 -> ABSOLUTE_PERCENT_AGAINST_INNER_SIZE 8 -> MIN_SIZE_UNDEFINED_INSTEAD_OF_AUTO 16 -> FLEX_FIRST_PASS_USES_RUNNING_TOTALS + 32 -> FIT_CONTENT_ROW_KEEPS_MAX_CONTENT_WIDTH 2147483647 -> ALL 2147483646 -> CLASSIC else -> throw IllegalArgumentException("Unknown enum value: $value") diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp b/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp index 6d9c77fe69b6..b0c547d7e81d 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp @@ -121,6 +121,8 @@ const char* YGErrataToString(const YGErrata value) { return "min-size-undefined-instead-of-auto"; case YGErrataFlexFirstPassUsesRunningTotals: return "flex-first-pass-uses-running-totals"; + case YGErrataFitContentRowKeepsMaxContentWidth: + return "fit-content-row-keeps-max-content-width"; case YGErrataAll: return "all"; case YGErrataClassic: diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.h b/packages/react-native/ReactCommon/yoga/yoga/YGEnums.h index a65af836fb85..90a4bc90690a 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.h +++ b/packages/react-native/ReactCommon/yoga/yoga/YGEnums.h @@ -69,6 +69,7 @@ YG_ENUM_DECL( YGErrataAbsolutePercentAgainstInnerSize = 4, YGErrataMinSizeUndefinedInsteadOfAuto = 8, YGErrataFlexFirstPassUsesRunningTotals = 16, + YGErrataFitContentRowKeepsMaxContentWidth = 32, YGErrataAll = 2147483647, YGErrataClassic = 2147483646) YG_DEFINE_ENUM_FLAG_OPERATORS(YGErrata) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 130d8fbc82ea..8abeaa76408f 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -340,8 +340,17 @@ static void computeFlexBasisForChild( // The W3C spec doesn't say anything about the 'overflow' property, but all // major browsers appear to implement the following logic. - if ((!isMainAxisRow && node->style().overflow() == Overflow::Scroll) || - node->style().overflow() != Overflow::Scroll) { + // A row that shrinks its items to fit the space available would do so in + // this measure too, and its flex base size in a row is its max-content + // width (css-flexbox-1 §9.2, step 3E), so measure it without that space. + // Other items keep the space, so that text in them wraps to it. + const bool isRowThatShrinksToFit = isMainAxisRow && + isRow(child->style().flexDirection()) && !child->hasMeasureFunc() && + !child->hasErrata(Errata::StretchFlexBasis) && + !child->hasErrata(Errata::FitContentRowKeepsMaxContentWidth); + if (!isRowThatShrinksToFit && + ((!isMainAxisRow && node->style().overflow() == Overflow::Scroll) || + node->style().overflow() != Overflow::Scroll)) { if (yoga::isUndefined(childWidth) && yoga::isDefined(width)) { childWidth = width; childWidthSizingMode = SizingMode::FitContent; @@ -2454,18 +2463,39 @@ static void calculateLayoutImpl( bool useLegacyStretchBehaviour = node->hasErrata(Errata::StretchFlexBasis); - if (!useLegacyStretchBehaviour && - ((yoga::isDefined(flexLine.layout.totalFlexGrowFactors) && - flexLine.layout.totalFlexGrowFactors == 0) || - (yoga::isDefined(node->resolveFlexGrow()) && - node->resolveFlexGrow() == 0))) { - // If we don't have any children to flex or we can't flex the node - // itself, space we've used is all space we need. Root node also - // should be shrunk to minimum - availableInnerMainDim = flexLine.sizeConsumed; - } + if (!useLegacyStretchBehaviour && isMainAxisRow && + sizingModeMainDim == SizingMode::FitContent && + flexLine.sizeConsumed > availableInnerMainDim && + !node->hasErrata(Errata::FitContentRowKeepsMaxContentWidth)) { + // A row sized to fit content that is wider than the space available + // is as wide as that space, or as its min-content width if that is + // larger, and its items shrink to fit (css-sizing-3 §2.1). A column + // keeps its content's height, as it does in browsers. + availableInnerMainDim = yoga::maxOrDefined( + availableInnerMainDim, + computeMinContentMainSize( + node, + mainAxis, + ownerDirection, + ownerWidth, + ownerHeight, + generationCount) + .size - + paddingAndBorderAxisMain); + } else { + if (!useLegacyStretchBehaviour && + ((yoga::isDefined(flexLine.layout.totalFlexGrowFactors) && + flexLine.layout.totalFlexGrowFactors == 0) || + (yoga::isDefined(node->resolveFlexGrow()) && + node->resolveFlexGrow() == 0))) { + // If we don't have any children to flex or we can't flex the node + // itself, space we've used is all space we need. Root node also + // should be shrunk to minimum + availableInnerMainDim = flexLine.sizeConsumed; + } - sizeBasedOnContent = !useLegacyStretchBehaviour; + sizeBasedOnContent = !useLegacyStretchBehaviour; + } } } diff --git a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h index 95f63e87a483..fa78e5c18cf5 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/config/Config.h +++ b/packages/react-native/ReactCommon/yoga/yoga/config/Config.h @@ -88,7 +88,8 @@ class YG_EXPORT Config : public ::YGConfig { uint32_t version_ = 0; ExperimentalFeatureSet experimentalFeatures_{}; Errata errata_ = Errata::MinSizeUndefinedInsteadOfAuto | - Errata::FlexFirstPassUsesRunningTotals; + Errata::FlexFirstPassUsesRunningTotals | + Errata::FitContentRowKeepsMaxContentWidth; float pointScaleFactor_ = 1.0f; void* context_ = nullptr; }; diff --git a/packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h index 09e6f83a3eba..ff2a9c2fffbc 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h +++ b/packages/react-native/ReactCommon/yoga/yoga/enums/Errata.h @@ -22,6 +22,7 @@ enum class Errata : uint32_t { AbsolutePercentAgainstInnerSize = YGErrataAbsolutePercentAgainstInnerSize, MinSizeUndefinedInsteadOfAuto = YGErrataMinSizeUndefinedInsteadOfAuto, FlexFirstPassUsesRunningTotals = YGErrataFlexFirstPassUsesRunningTotals, + FitContentRowKeepsMaxContentWidth = YGErrataFitContentRowKeepsMaxContentWidth, All = YGErrataAll, Classic = YGErrataClassic, }; diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api index 17a46633c800..456c30e7f37d 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api @@ -7027,6 +7027,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None, diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api index 4b3d8b0f972b..fa5fd1266516 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api @@ -11547,6 +11547,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None, diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api index ed88e24ecf01..81dc01048c7a 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api @@ -6875,6 +6875,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None, diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api index ed88e24ecf01..81dc01048c7a 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api @@ -6875,6 +6875,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None, diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index a3468f62ab87..81ca4add275f 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -8765,6 +8765,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None, diff --git a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api index 0bf8e8646dd3..c77753f04eb3 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api @@ -13311,6 +13311,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None, diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index 0a7ab1e68aad..7fc171e2fbea 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -8616,6 +8616,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None, diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index 43038ba001cd..1f2a23ed55dd 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -8623,6 +8623,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None, diff --git a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api index d658091ca121..424160bfb7dc 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api @@ -4223,6 +4223,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None, diff --git a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api index a3424c76afff..9b9f604d2fef 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api @@ -8635,6 +8635,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None, diff --git a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api index 438ce9309066..d3cdff15ee91 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api @@ -4216,6 +4216,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None, diff --git a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api index 438ce9309066..d3cdff15ee91 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api @@ -4216,6 +4216,7 @@ enum facebook::yoga::Errata : uint32_t { AbsolutePositionWithoutInsetsExcludesPadding, All, Classic, + FitContentRowKeepsMaxContentWidth, FlexFirstPassUsesRunningTotals, MinSizeUndefinedInsteadOfAuto, None,