From cc1ec89c41eb642a2cb20c122f8f15b4ea21a039 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Thu, 3 Dec 2026 15:59:33 -0800 Subject: [PATCH] Add overflow: clip, which keeps the automatic minimum size (#2106) (#60476) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/react/react-native/pull/60476 * Proposal: add overflow: clip, which keeps the automatic minimum size Why === A flex item that is a scroll container has an automatic minimum size of zero; any other has a content-based one (css-flexbox-1 §4.5). `overflow: hidden` makes a scroll container; `overflow: clip` clips without making one (css-overflow-3 §3.1: "the `visible` and `clip` values are known as the non-scrollable values", and "if neither axis computes to a scrollable value, the box is not a scroll container"). Yoga has `visible`, `hidden` and `scroll`. An app that only wants an item's content clipped, as React Native apps do with `overflow: 'hidden'` for rounded corners, has to say `hidden`, which with automatic minimum sizes on also gives up the item's minimum. This is a proposal because it adds a value to a public enum. How === `YGOverflowClip` clips and keeps the minimum: the automatic minimum is zero for `hidden` and `scroll` and content-based for `visible` and `clip`. Everywhere else Yoga only asks whether a box is `scroll`, so `clip` lays out as `hidden`. Clipping is the host's job, as for `hidden`. The value is not behind an erratum or an experimental feature: no existing value changes meaning, and only a node given the new value behaves differently. A `switch` over `YGOverflow` with no `default` gains a `-Wswitch` warning. React Native's switch in `YogaLayoutableShadowNode.cpp` handles `YGOverflowClip` alongside `hidden` and `scroll`, setting `FormsContentClip`. A native regression test verifies that the clip value sets the trait. Mapping the `overflow: 'clip'` style value to it is a separate React Native change. Test Plan ========= `python3 enums.py` regenerates the enum files with no change, and `yarn gentest` changes only the `YGAutoMinSizeTest` files, which gain `overflow_clip_item_keeps_content_size` next to the existing `overflow_hidden_item_shrinks_past_content`. `cmake --build tests/build && tests/build/yogatests` and `yarn format-check && yarn lint` pass. The new fixture cannot be built without the enum value. With `clip` treated as `hidden`, the item is 60 wide where Chrome has 120. `yarn gentest -h` leaves the tree clean, and `node ./scripts/clang-format.js --check` passes. The generated Java tests were not run, because the Gradle project has no unit-test source set; the Kotlin library compiles with the new enum value. The generated JavaScript tests were not run either, because they need Emscripten. Changelog: [General][Added] - Add `YGOverflowClip` to Yoga: an item that clips without being a scroll container keeps its automatic minimum size * Document overflow and what each value changes in layout Why === The website has no page for `overflow`, and the property affects layout in ways a reader cannot guess from its name: `hidden` and `scroll` make a scroll container, which has an automatic minimum size of zero (css-flexbox-1 §4.5), and `clip` does not. How === The new page `website/docs/styling/overflow.mdx` covers `visible`, `hidden`, `scroll` and `clip` as Yoga reads them: which make a scroll container, what that does to an item's automatic minimum size, and how a `scroll` node's content is measured. It states the limits: Yoga clips nothing, one value covers both axes, and automatic minimum sizes apply only with `MinSizeUndefinedInsteadOfAuto` off, so with a default config `clip` lays out exactly as `visible` and `hidden` do. The playground accepts `overflow: 'clip'`. Test Plan ========= The site builds with `docusaurus build`, which fails on a broken link, against a stub of the WebAssembly module, because the real build needs Emscripten. The playgrounds were therefore not run in a browser. `yarn tsc` in `website/` passes, and `prettier --check` and `eslint` pass on the changed TypeScript. The numbers on the page come from a program run against `libyogacore` built from this branch. Changelog: [Internal] * Deserialize overflow: clip in benchmark TreeDeserialization * Build the base with its own benchmark program when the PR's does not build The instruction-count job builds the base Yoga with the PR's benchmark program. When that program uses an API the PR adds, such as YGOverflowClip, the base build failed. Fall back to the base's own benchmark program, with a warning. --------- bypass-github-export-checks Yoga PR Reviewed By: vzaidman Differential Revision: D135507378 Signed-off-by: Pieter De Baets --- .../main/java/com/facebook/yoga/YogaOverflow.kt | 4 +++- .../components/view/YogaLayoutableShadowNode.cpp | 1 + .../renderer/components/view/tests/LayoutTest.cpp | 14 ++++++++++++++ .../react-native/ReactCommon/yoga/yoga/YGEnums.cpp | 2 ++ .../react-native/ReactCommon/yoga/yoga/YGEnums.h | 3 ++- .../yoga/yoga/algorithm/CalculateLayout.cpp | 10 ++++++---- .../ReactCommon/yoga/yoga/enums/Overflow.h | 3 ++- .../cxx-api/api-snapshots/ReactAndroidDebugCxx.api | 1 + .../api-snapshots/ReactAndroidFrameworksCxx.api | 1 + .../api-snapshots/ReactAndroidNewarchCxx.api | 1 + .../api-snapshots/ReactAndroidReleaseCxx.api | 1 + .../cxx-api/api-snapshots/ReactAppleDebugCxx.api | 1 + .../api-snapshots/ReactAppleFrameworksCxx.api | 1 + .../cxx-api/api-snapshots/ReactAppleNewarchCxx.api | 1 + .../cxx-api/api-snapshots/ReactAppleReleaseCxx.api | 1 + .../cxx-api/api-snapshots/ReactCommonDebugCxx.api | 1 + .../api-snapshots/ReactCommonFrameworksCxx.api | 1 + .../api-snapshots/ReactCommonNewarchCxx.api | 1 + .../api-snapshots/ReactCommonReleaseCxx.api | 1 + 19 files changed, 42 insertions(+), 7 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaOverflow.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaOverflow.kt index 90ca7c8a2551..98a4b1eb1025 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaOverflow.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/yoga/YogaOverflow.kt @@ -12,7 +12,8 @@ package com.facebook.yoga public enum class YogaOverflow(public val intValue: Int) { VISIBLE(0), HIDDEN(1), - SCROLL(2); + SCROLL(2), + CLIP(3); public fun intValue(): Int = intValue @@ -23,6 +24,7 @@ public enum class YogaOverflow(public val intValue: Int) { 0 -> VISIBLE 1 -> HIDDEN 2 -> SCROLL + 3 -> CLIP else -> throw IllegalArgumentException("Unknown enum value: $value") } } diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp index 71019a44767f..2739eefb9ba1 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -453,6 +453,7 @@ void YogaLayoutableShadowNode::updateYogaProps() { break; case YGOverflowHidden: case YGOverflowScroll: + case YGOverflowClip: ShadowNode::traits_.set(ShadowNodeTraits::Trait::FormsContentClip); break; } diff --git a/packages/react-native/ReactCommon/react/renderer/components/view/tests/LayoutTest.cpp b/packages/react-native/ReactCommon/react/renderer/components/view/tests/LayoutTest.cpp index 7bcbc7588e01..2612f5312605 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/tests/LayoutTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/tests/LayoutTest.cpp @@ -184,6 +184,20 @@ class LayoutTest : public ::testing::Test { } }; +TEST_F(LayoutTest, overflowClipFormsContentClip) { + std::shared_ptr viewShadowNode; + auto element = Element().reference(viewShadowNode).props([] { + auto props = std::make_shared(); + props->yogaStyle.setOverflow(yoga::Overflow::Clip); + return props; + }); + + builder_.build(element); + + EXPECT_TRUE(viewShadowNode->getTraits().check( + ShadowNodeTraits::Trait::FormsContentClip)); +} + // Test the layout as described above with no extra changes TEST_F(LayoutTest, overflowInsetTest) { initialize(AS_IS); diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp b/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp index 194e99a6aa60..6d9c77fe69b6 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/YGEnums.cpp @@ -257,6 +257,8 @@ const char* YGOverflowToString(const YGOverflow value) { return "hidden"; case YGOverflowScroll: return "scroll"; + case YGOverflowClip: + return "clip"; } return "unknown"; } diff --git a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.h b/packages/react-native/ReactCommon/yoga/yoga/YGEnums.h index b10b74f98fbe..a65af836fb85 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/YGEnums.h +++ b/packages/react-native/ReactCommon/yoga/yoga/YGEnums.h @@ -137,7 +137,8 @@ YG_ENUM_DECL( YGOverflow, YGOverflowVisible, YGOverflowHidden, - YGOverflowScroll) + YGOverflowScroll, + YGOverflowClip) YG_ENUM_DECL( YGPositionType, diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index aa6131b410b3..610dae1e370f 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -1330,10 +1330,12 @@ static FloatOptional computeAutoMinMainSize( if (child->style().minDimension(dimension(mainAxis)).isDefined()) { return FloatOptional{}; } - // Per CSS §4.5: a flex item whose own `overflow` is not `visible` gets - // auto-min = 0 (let scroll/clip handle overflow rather than enforce a - // content-based minimum). - if (child->style().overflow() != Overflow::Visible) { + // Per CSS §4.5: a flex item that is a scroll container gets auto-min = 0 + // (let scrolling handle overflow rather than enforce a content-based + // minimum). `overflow: clip` clips without making a scroll container + // (css-overflow-3 §3.1), so it keeps its content-based minimum. + const Overflow overflow = child->style().overflow(); + if (overflow == Overflow::Hidden || overflow == Overflow::Scroll) { return FloatOptional{0.0f}; } diff --git a/packages/react-native/ReactCommon/yoga/yoga/enums/Overflow.h b/packages/react-native/ReactCommon/yoga/yoga/enums/Overflow.h index 20901b02c05a..0df9518b818b 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/enums/Overflow.h +++ b/packages/react-native/ReactCommon/yoga/yoga/enums/Overflow.h @@ -19,11 +19,12 @@ enum class Overflow : uint8_t { Visible = YGOverflowVisible, Hidden = YGOverflowHidden, Scroll = YGOverflowScroll, + Clip = YGOverflowClip, }; template <> constexpr int32_t ordinalCount() { - return 3; + return 4; } constexpr Overflow scopedEnum(YGOverflow unscoped) { diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api index ece6b944c73a..3ab4e7cd11e1 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api @@ -7104,6 +7104,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible, diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api index 96f1de574a16..b520c48af496 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api @@ -11615,6 +11615,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible, diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api index 809dada6ec58..d48c22c4efe9 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api @@ -6952,6 +6952,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible, diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api index 809dada6ec58..d48c22c4efe9 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api @@ -6952,6 +6952,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible, diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index 00b196227198..6189f1f82908 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -8843,6 +8843,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible, diff --git a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api index fc711f07ba9a..85668b7d3ccc 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api @@ -13380,6 +13380,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible, diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index a6260dedf98d..df4f68c31155 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -8694,6 +8694,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible, diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index b7f61c537afd..64b0e2327e07 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -8701,6 +8701,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible, diff --git a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api index bfc0cd340bc9..2d39e71b808c 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api @@ -4301,6 +4301,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible, diff --git a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api index 66dccbe50765..f68615e9118b 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api @@ -8704,6 +8704,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible, diff --git a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api index 230268227245..5ff99aabf8c6 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api @@ -4294,6 +4294,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible, diff --git a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api index 230268227245..5ff99aabf8c6 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api @@ -4294,6 +4294,7 @@ enum facebook::yoga::NodeType : uint8_t { } enum facebook::yoga::Overflow : uint8_t { + Clip, Hidden, Scroll, Visible,