diff --git a/packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js b/packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js index b3acf2a152f6..af8aff83e506 100644 --- a/packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js +++ b/packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js @@ -9,6 +9,7 @@ */ import type {RNTesterModuleExample} from '../../types/RNTesterTypes'; +import type {ScrollEvent} from 'react-native'; import RNTesterText from '../../components/RNTesterText'; import ScrollViewPressableStickyHeaderExample from './ScrollViewPressableStickyHeaderExample'; @@ -238,9 +239,26 @@ function ContentOffsetList(): React.Node { function ScrollViewScrollToExample(): React.Node { let _scrollView: ?React.ElementRef; const [scrolledToTop, setScrolledToTop] = useState(false); + const [scrollPosition, setScrollPosition] = useState< + 'pending' | 'top' | 'bottom', + >('top'); + const updateScrollPosition = (event: ScrollEvent) => { + const {contentOffset, contentSize, layoutMeasurement} = event.nativeEvent; + // Native offsets can round by a fraction of a logical pixel. + if (contentOffset.y <= 1) { + setScrollPosition('top'); + } else if ( + contentOffset.y >= + contentSize.height - layoutMeasurement.height - 1 + ) { + setScrollPosition('bottom'); + } else { + setScrollPosition('pending'); + } + }; const textStyle = {color: 'blue', marginBottom: 10, textAlign: 'center'}; return ( - + {scrolledToTop ? ( /* $FlowFixMe[incompatible-type] Natural Inference rollout. See * https://fburl.com/workplace/6291gfvu */ @@ -253,10 +271,12 @@ function ScrollViewScrollToExample(): React.Node { }} automaticallyAdjustContentInsets={false} nestedScrollEnabled - onScroll={() => { + onScroll={event => { console.log('onScroll!'); setScrolledToTop(false); + updateScrollPosition(event); }} + onMomentumScrollEnd={updateScrollPosition} onScrollToTop={() => { setScrolledToTop(true); }} @@ -268,6 +288,9 @@ function ScrollViewScrollToExample(): React.Node {