From 327df8a719df58adc5aeeedb02d1923de4587d3d Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Wed, 3 Jan 2024 08:08:30 -0800 Subject: [PATCH] Implement onScrollToTop event for Fabric (#42128) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/42128 Scroll view was not emitting the `onScrollToTop` event when the user tapped on the status bar. This change fixes this by adding the event to the C++ Event Emitter and by invoking it into the `RCTScrollViewComponentView` ## Changelog [iOS][Added] - Add onScrollToTop event in Fabric Reviewed By: sammy-SC Differential Revision: D52509919 fbshipit-source-id: 7b72c927823fa971be99c4da4b0287d4e23a02b6 --- .../ScrollView/RCTScrollViewComponentView.mm | 1 + .../components/scrollview/ScrollViewEventEmitter.cpp | 8 ++++++++ .../components/scrollview/ScrollViewEventEmitter.h | 1 + 3 files changed, 10 insertions(+) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm index 5192358f661..b164cc3d6ac 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -472,6 +472,7 @@ static void RCTSendScrollEventForNativeAnimations_DEPRECATED(UIScrollView *scrol - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView { _isUserTriggeredScrolling = NO; + static_cast(*_eventEmitter).onScrollToTop([self _scrollViewMetrics]); [self _updateStateWithContentOffset]; } diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.cpp b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.cpp index 6fc866fab60..9ea8c939900 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.cpp +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.cpp @@ -64,6 +64,14 @@ void ScrollViewEventEmitter::onScroll( }); } +void ScrollViewEventEmitter::onScrollToTop( + const ScrollViewMetrics& scrollViewMetrics) const { + dispatchUniqueEvent( + "scrollToTop", [scrollViewMetrics](jsi::Runtime& runtime) { + return scrollViewMetricsPayload(runtime, scrollViewMetrics); + }); +} + void ScrollViewEventEmitter::onScrollBeginDrag( const ScrollViewMetrics& scrollViewMetrics) const { dispatchScrollViewEvent("scrollBeginDrag", scrollViewMetrics); diff --git a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.h b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.h index 50bff3465fd..8a849db8a50 100644 --- a/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.h +++ b/packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollViewEventEmitter.h @@ -33,6 +33,7 @@ class ScrollViewEventEmitter : public ViewEventEmitter { void onScrollEndDrag(const ScrollViewMetrics& scrollViewMetrics) const; void onMomentumScrollBegin(const ScrollViewMetrics& scrollViewMetrics) const; void onMomentumScrollEnd(const ScrollViewMetrics& scrollViewMetrics) const; + void onScrollToTop(const ScrollViewMetrics& scrollViewMetrics) const; private: void dispatchScrollViewEvent(