From 4fbbf25470239a1f5ecf786e0f532c496526299d Mon Sep 17 00:00:00 2001 From: aseeb shibin Date: Sat, 28 Nov 2026 14:52:03 +0300 Subject: [PATCH] fix(fabric): read hitSlop for all ViewKind children in getContentBounds (fix #60278) YogaLayoutableShadowNode::getContentBounds() previously only read hitSlop from children whose component handle matched ViewShadowNode::Handle(). Because hitSlop lives on BaseViewProps, other ConcreteViewShadowNode components such as ScrollView also parse hitSlop, so checking ShadowNodeTraits::ViewKind and reading hitSlop from ViewProps ensures their hitSlop widens the parent's overflowInset. --- .../ScrollView/__tests__/ScrollView-itest.js | 28 +++++++++ .../view/YogaLayoutableShadowNode.cpp | 11 +--- .../components/view/tests/LayoutTest.cpp | 57 +++++++++++++++++++ 3 files changed, 87 insertions(+), 9 deletions(-) diff --git a/packages/react-native/Libraries/Components/ScrollView/__tests__/ScrollView-itest.js b/packages/react-native/Libraries/Components/ScrollView/__tests__/ScrollView-itest.js index 04135f43326f..ec405bf6311f 100644 --- a/packages/react-native/Libraries/Components/ScrollView/__tests__/ScrollView-itest.js +++ b/packages/react-native/Libraries/Components/ScrollView/__tests__/ScrollView-itest.js @@ -71,6 +71,34 @@ describe('', () => { }); }); + describe('hitSlop', () => { + it('widens parent overflowInset when hitSlop extends past parent', () => { + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + const parent = root + .getRenderedOutput({ + includeLayoutMetrics: true, + props: ['^layoutMetrics-overflowInset$'], + }) + .toJSONObject(); + + expect(parent.props['layoutMetrics-overflowInset']).toBe( + '{top:-10,right:-20,bottom:-10,left:-20}', + ); + }); + }); + describe('ref', () => { it('receives an instance or null', () => { const root = Fantom.createRoot(); 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 825334daf3d5..71019a44767f 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -892,14 +891,8 @@ Rect YogaLayoutableShadowNode::getContentBounds() const { auto layoutMetricsWithOverflowInset = childNode.getLayoutMetrics(); if (layoutMetricsWithOverflowInset.displayType != DisplayType::None) { - // ViewShadowNode is final, so comparing component handles is equivalent - // to a `dynamic_cast`, without its cost for every non-View child. - auto viewChildNode = - childNode.getComponentHandle() == ViewShadowNode::Handle() - ? static_cast(&childNode) - : nullptr; - auto hitSlop = viewChildNode != nullptr - ? viewChildNode->getConcreteProps().hitSlop + auto hitSlop = childNode.getTraits().check(ShadowNodeTraits::ViewKind) + ? static_cast(*childNode.getProps()).hitSlop : EdgeInsets{}; // The contentBounds should always union with existing child node layout 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 c8c2c58ef07f..7bcbc7588e01 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 @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -444,4 +445,60 @@ TEST_F(LayoutTest, overflowInsetHitSlopTransformTranslateTest) { EXPECT_EQ(layoutMetricsABC.overflowInset.bottom, 0); } +TEST_F(LayoutTest, overflowInsetScrollViewHitSlopTest) { + std::shared_ptr rootShadowNode; + std::shared_ptr viewShadowNode; + + // clang-format off + auto element = + Element() + .reference(rootShadowNode) + .tag(1) + .props([] { + auto sharedProps = std::make_shared(); + auto &props = *sharedProps; + props.layoutConstraints = LayoutConstraints{ + .minimumSize = {.width = 0, .height = 0}, + .maximumSize = {.width = 500, .height = 500}}; + props.yogaStyle.setDimension(yoga::Dimension::Width, yoga::StyleSizeLength::points(200)); + props.yogaStyle.setDimension(yoga::Dimension::Height, yoga::StyleSizeLength::points(200)); + return sharedProps; + }) + .children({ + Element() + .reference(viewShadowNode) + .tag(2) + .props([] { + auto sharedProps = std::make_shared(); + auto &props = *sharedProps; + props.yogaStyle.setPositionType(yoga::PositionType::Absolute); + props.yogaStyle.setDimension(yoga::Dimension::Width, yoga::StyleSizeLength::points(100)); + props.yogaStyle.setDimension(yoga::Dimension::Height, yoga::StyleSizeLength::points(100)); + return sharedProps; + }) + .children({ + Element() + .tag(3) + .props([] { + auto sharedProps = std::make_shared(); + auto &props = *sharedProps; + props.yogaStyle.setDimension(yoga::Dimension::Width, yoga::StyleSizeLength::points(40)); + props.yogaStyle.setDimension(yoga::Dimension::Height, yoga::StyleSizeLength::points(40)); + props.hitSlop = EdgeInsets{.left = 20, .top = 10, .right = 80, .bottom = 70}; + return sharedProps; + }) + }) + }); + // clang-format on + + builder_.build(element); + rootShadowNode->layoutIfNeeded(); + + auto layoutMetrics = viewShadowNode->getLayoutMetrics(); + EXPECT_EQ(layoutMetrics.overflowInset.left, -20); + EXPECT_EQ(layoutMetrics.overflowInset.top, -10); + EXPECT_EQ(layoutMetrics.overflowInset.right, -20); + EXPECT_EQ(layoutMetrics.overflowInset.bottom, -10); +} + } // namespace facebook::react