Skip to content

Commit b49769c

Browse files
authored
fix(fabric): read hitSlop for all ViewKind children in getContentBounds
## Summary: `YogaLayoutableShadowNode::getContentBounds()` previously read `hitSlop` only from children whose component handle matched `ViewShadowNode::Handle()`. Because `hitSlop` is defined on `BaseViewProps`, other `ConcreteViewShadowNode` components such as `ScrollView` also parse `hitSlop`, so their `hitSlop` extending past their parent's box never widened the parent's `overflowInset`. Check `ShadowNodeTraits::ViewKind` instead and read `hitSlop` from `ViewProps`, matching how `YogaLayoutableShadowNode`, `PointerEventsProcessor`, and `LayoutAnimationKeyFrameManager` access `ViewProps`-derived nodes. Fixes #60278 ## Changelog: [GENERAL] [FIXED] - Read `hitSlop` in `YogaLayoutableShadowNode::getContentBounds` for all `ViewKind` children (including `ScrollView`) ## Test Plan: - Built and ran `react_renderer_components_view_tests` (via CMake/Make on macOS arm64 with `-lc++abi` for `libjsi.dylib`): - `111 tests from 11 test suites ran` — `111 passed`, including all 7 tests in `LayoutTest.*` (`LayoutTest.overflowInsetScrollViewHitSlopTest` passed). - `yarn eslint --max-warnings 0 packages/react-native/Libraries/Components/ScrollView/__tests__/ScrollView-itest.js` — passed. - `yarn prettier --check packages/react-native/Libraries/Components/ScrollView/__tests__/ScrollView-itest.js` — passed. - `node ./scripts/clang-format.js --check packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp packages/react-native/ReactCommon/react/renderer/components/view/tests/LayoutTest.cpp` — passed. - `yarn flow-check` — 0 errors in source/test files (exited 2 locally due to pre-existing `.git/logs/refs/remotes/upstream/@chrfalch/prebuild-fix-missing-script-file-in-package.json` and `.git/refs/remotes/upstream/@chrfalch/prebuild-fix-missing-script-file-in-package.json`). - Could not run `yarn fantom packages/react-native/Libraries/Components/ScrollView/__tests__/ScrollView-itest.js` locally (`fantom_tester` build failed on macOS due to disk space constraints / missing `-lc++abi` on `libjsi.dylib`); relying on CI `fantom-tests.yml` on Linux. Differential Revision: D135332008 Pull Request resolved: #60360
1 parent 0ea7132 commit b49769c

3 files changed

Lines changed: 87 additions & 9 deletions

File tree

‎packages/react-native/Libraries/Components/ScrollView/__tests__/ScrollView-itest.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,34 @@ describe('<ScrollView>', () => {
103103
});
104104
});
105105

106+
describe('hitSlop', () => {
107+
it('widens parent overflowInset when hitSlop extends past parent', () => {
108+
const root = Fantom.createRoot();
109+
110+
Fantom.runTask(() => {
111+
root.render(
112+
<View collapsable={false} style={{width: 100, height: 100}}>
113+
<ScrollView
114+
hitSlop={{top: 10, left: 20, bottom: 70, right: 80}}
115+
style={{width: 40, height: 40, flexGrow: 0}}
116+
/>
117+
</View>,
118+
);
119+
});
120+
121+
const parent = root
122+
.getRenderedOutput({
123+
includeLayoutMetrics: true,
124+
props: ['^layoutMetrics-overflowInset$'],
125+
})
126+
.toJSONObject();
127+
128+
expect(parent.props['layoutMetrics-overflowInset']).toBe(
129+
'{top:-10,right:-20,bottom:-10,left:-20}',
130+
);
131+
});
132+
});
133+
106134
describe('ref', () => {
107135
it('receives an instance or null', () => {
108136
const root = Fantom.createRoot();

‎packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <react/featureflags/ReactNativeFeatureFlags.h>
1414
#include <react/renderer/components/view/LayoutConformanceShadowNode.h>
1515
#include <react/renderer/components/view/ViewProps.h>
16-
#include <react/renderer/components/view/ViewShadowNode.h>
1716
#include <react/renderer/components/view/conversions.h>
1817
#include <react/renderer/core/ComponentDescriptor.h>
1918
#include <react/renderer/core/LayoutConstraints.h>
@@ -892,14 +891,8 @@ Rect YogaLayoutableShadowNode::getContentBounds() const {
892891

893892
auto layoutMetricsWithOverflowInset = childNode.getLayoutMetrics();
894893
if (layoutMetricsWithOverflowInset.displayType != DisplayType::None) {
895-
// ViewShadowNode is final, so comparing component handles is equivalent
896-
// to a `dynamic_cast`, without its cost for every non-View child.
897-
auto viewChildNode =
898-
childNode.getComponentHandle() == ViewShadowNode::Handle()
899-
? static_cast<const ViewShadowNode*>(&childNode)
900-
: nullptr;
901-
auto hitSlop = viewChildNode != nullptr
902-
? viewChildNode->getConcreteProps().hitSlop
894+
auto hitSlop = childNode.getTraits().check(ShadowNodeTraits::ViewKind)
895+
? static_cast<const ViewProps&>(*childNode.getProps()).hitSlop
903896
: EdgeInsets{};
904897

905898
// The contentBounds should always union with existing child node layout

‎packages/react-native/ReactCommon/react/renderer/components/view/tests/LayoutTest.cpp‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
1111
#include <react/renderer/components/root/RootComponentDescriptor.h>
12+
#include <react/renderer/components/scrollview/ScrollViewShadowNode.h>
1213
#include <react/renderer/components/view/ViewComponentDescriptor.h>
1314
#include <react/renderer/element/ComponentBuilder.h>
1415
#include <react/renderer/element/Element.h>
@@ -444,4 +445,60 @@ TEST_F(LayoutTest, overflowInsetHitSlopTransformTranslateTest) {
444445
EXPECT_EQ(layoutMetricsABC.overflowInset.bottom, 0);
445446
}
446447

448+
TEST_F(LayoutTest, overflowInsetScrollViewHitSlopTest) {
449+
std::shared_ptr<RootShadowNode> rootShadowNode;
450+
std::shared_ptr<ViewShadowNode> viewShadowNode;
451+
452+
// clang-format off
453+
auto element =
454+
Element<RootShadowNode>()
455+
.reference(rootShadowNode)
456+
.tag(1)
457+
.props([] {
458+
auto sharedProps = std::make_shared<RootProps>();
459+
auto &props = *sharedProps;
460+
props.layoutConstraints = LayoutConstraints{
461+
.minimumSize = {.width = 0, .height = 0},
462+
.maximumSize = {.width = 500, .height = 500}};
463+
props.yogaStyle.setDimension(yoga::Dimension::Width, yoga::StyleSizeLength::points(200));
464+
props.yogaStyle.setDimension(yoga::Dimension::Height, yoga::StyleSizeLength::points(200));
465+
return sharedProps;
466+
})
467+
.children({
468+
Element<ViewShadowNode>()
469+
.reference(viewShadowNode)
470+
.tag(2)
471+
.props([] {
472+
auto sharedProps = std::make_shared<ViewProps>();
473+
auto &props = *sharedProps;
474+
props.yogaStyle.setPositionType(yoga::PositionType::Absolute);
475+
props.yogaStyle.setDimension(yoga::Dimension::Width, yoga::StyleSizeLength::points(100));
476+
props.yogaStyle.setDimension(yoga::Dimension::Height, yoga::StyleSizeLength::points(100));
477+
return sharedProps;
478+
})
479+
.children({
480+
Element<ScrollViewShadowNode>()
481+
.tag(3)
482+
.props([] {
483+
auto sharedProps = std::make_shared<ScrollViewProps>();
484+
auto &props = *sharedProps;
485+
props.yogaStyle.setDimension(yoga::Dimension::Width, yoga::StyleSizeLength::points(40));
486+
props.yogaStyle.setDimension(yoga::Dimension::Height, yoga::StyleSizeLength::points(40));
487+
props.hitSlop = EdgeInsets{.left = 20, .top = 10, .right = 80, .bottom = 70};
488+
return sharedProps;
489+
})
490+
})
491+
});
492+
// clang-format on
493+
494+
builder_.build(element);
495+
rootShadowNode->layoutIfNeeded();
496+
497+
auto layoutMetrics = viewShadowNode->getLayoutMetrics();
498+
EXPECT_EQ(layoutMetrics.overflowInset.left, -20);
499+
EXPECT_EQ(layoutMetrics.overflowInset.top, -10);
500+
EXPECT_EQ(layoutMetrics.overflowInset.right, -20);
501+
EXPECT_EQ(layoutMetrics.overflowInset.bottom, -10);
502+
}
503+
447504
} // namespace facebook::react

0 commit comments

Comments
 (0)