From 9733b92f3dfddc42be86f4f58d9af68b52f76aaf Mon Sep 17 00:00:00 2001 From: James Reggio Date: Wed, 19 Sep 2018 11:15:54 -0700 Subject: [PATCH] Add `onScrollToTop` to ScrollView for iOS (#21204) Summary: This PR exposes the `onScrollToTop` event on iOS using the same event-forwarding infrastructure as other ScrollView events. (As such, its `nativeEvent` object reflects the same fields as other ScrollView events.) Motivation: ---------- If your app is only interested in knowing the position of a ScrollView after a scroll has completed, it can use `onScrollEndDrag` and `onMomentumScrollEnd` to inspect the `contentOffset` after a drag-initiated scroll has finished. (This is much less expensive than observing the `onScroll` event if you only want to know the end position.) However, neither of these `End` events fire if the ScrollView is scrolled to the top by tapping the status bar. By exposing `onScrollToTop`, it is now possible for an app to cheaply know when such a scroll has completed. Pull Request resolved: https://github.com/facebook/react-native/pull/21204 Differential Revision: D9943618 Pulled By: hramos fbshipit-source-id: ac5ee42b7f12d94655ffda617f8f811138da7f6f --- Libraries/Components/ScrollView/ScrollView.js | 5 +++++ React/Views/ScrollView/RCTScrollView.h | 1 + React/Views/ScrollView/RCTScrollView.m | 1 + React/Views/ScrollView/RCTScrollViewManager.m | 1 + 4 files changed, 8 insertions(+) diff --git a/Libraries/Components/ScrollView/ScrollView.js b/Libraries/Components/ScrollView/ScrollView.js index e7f650c33..0030ac329 100644 --- a/Libraries/Components/ScrollView/ScrollView.js +++ b/Libraries/Components/ScrollView/ScrollView.js @@ -217,6 +217,11 @@ type IOSProps = $ReadOnly<{| * @platform ios */ scrollsToTop?: ?boolean, + /** + * Fires when the scroll view scrolls to top after the status bar has been tapped + * @platform ios + */ + onScrollToTop?: ?Function, /** * When true, shows a horizontal scroll indicator. * The default value is true. diff --git a/React/Views/ScrollView/RCTScrollView.h b/React/Views/ScrollView/RCTScrollView.h index 6640b9bc4..3404422c7 100644 --- a/React/Views/ScrollView/RCTScrollView.h +++ b/React/Views/ScrollView/RCTScrollView.h @@ -55,6 +55,7 @@ // need to be coalesced before sending, for performance reasons. @property (nonatomic, copy) RCTDirectEventBlock onScrollBeginDrag; @property (nonatomic, copy) RCTDirectEventBlock onScroll; +@property (nonatomic, copy) RCTDirectEventBlock onScrollToTop; @property (nonatomic, copy) RCTDirectEventBlock onScrollEndDrag; @property (nonatomic, copy) RCTDirectEventBlock onMomentumScrollBegin; @property (nonatomic, copy) RCTDirectEventBlock onMomentumScrollEnd; diff --git a/React/Views/ScrollView/RCTScrollView.m b/React/Views/ScrollView/RCTScrollView.m index 79b7342c4..525073006 100644 --- a/React/Views/ScrollView/RCTScrollView.m +++ b/React/Views/ScrollView/RCTScrollView.m @@ -648,6 +648,7 @@ for (NSObject *scrollViewListener in _scrollListeners) { \ RCT_SCROLL_EVENT_HANDLER(scrollViewWillBeginDecelerating, onMomentumScrollBegin) RCT_SCROLL_EVENT_HANDLER(scrollViewDidZoom, onScroll) +RCT_SCROLL_EVENT_HANDLER(scrollViewDidScrollToTop, onScrollToTop) - (void)addScrollListener:(NSObject *)scrollListener { diff --git a/React/Views/ScrollView/RCTScrollViewManager.m b/React/Views/ScrollView/RCTScrollViewManager.m index 36265af20..6494d52f4 100644 --- a/React/Views/ScrollView/RCTScrollViewManager.m +++ b/React/Views/ScrollView/RCTScrollViewManager.m @@ -88,6 +88,7 @@ RCT_EXPORT_VIEW_PROPERTY(snapToAlignment, NSString) RCT_REMAP_VIEW_PROPERTY(contentOffset, scrollView.contentOffset, CGPoint) RCT_EXPORT_VIEW_PROPERTY(onScrollBeginDrag, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onScroll, RCTDirectEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onScrollToTop, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onScrollEndDrag, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onMomentumScrollBegin, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onMomentumScrollEnd, RCTDirectEventBlock)