Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -238,9 +239,26 @@ function ContentOffsetList(): React.Node {
function ScrollViewScrollToExample(): React.Node {
let _scrollView: ?React.ElementRef<typeof ScrollView>;
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 (
<View>
<View testID={`scroll_position_${scrollPosition}`}>
{scrolledToTop ? (
/* $FlowFixMe[incompatible-type] Natural Inference rollout. See
* https://fburl.com/workplace/6291gfvu */
Expand All @@ -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);
}}
Expand All @@ -268,13 +288,19 @@ function ScrollViewScrollToExample(): React.Node {
<Button
label="Scroll to top"
onPress={() => {
setScrollPosition(position =>
position === 'top' ? position : 'pending',
);
nullthrows<$FlowFixMe>(_scrollView).scrollTo({y: 0});
}}
testID="scroll_to_top_button"
/>
<Button
label="Scroll to bottom"
onPress={() => {
setScrollPosition(position =>
position === 'bottom' ? position : 'pending',
);
nullthrows<$FlowFixMe>(_scrollView).scrollToEnd({animated: true});
}}
testID="scroll_to_bottom_button"
Expand Down
8 changes: 8 additions & 0 deletions packages/rn-tester/visual-tests/flows/scroll-view.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ appId: ${APP_ID}
timeout: 15000
- tapOn:
id: 'scroll_to_bottom_button'
- extendedWaitUntil:
visible:
id: 'scroll_position_bottom'
timeout: 5000
- waitForAnimationToEnd:
timeout: 5000
- takeScreenshot:
Expand All @@ -18,6 +22,10 @@ appId: ${APP_ID}
id: 'scroll_vertical'
- tapOn:
id: 'scroll_to_top_button'
- extendedWaitUntil:
visible:
id: 'scroll_position_top'
timeout: 5000
- waitForAnimationToEnd:
timeout: 5000
- takeScreenshot:
Expand Down
Loading