Skip to content

Commit 6e98e70

Browse files
committed
fix: api and allow for onScrollBeginDrag, onScrollEndDrag and more to be supplied
1 parent 3ff2510 commit 6e98e70

File tree

3 files changed

+81
-21
lines changed

3 files changed

+81
-21
lines changed

src/components/containers/FlatList.tsx

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import React from 'react';
22
import { View, FlatList, FlatListProps, StyleSheet } from 'react-native';
3+
import { useSafeAreaInsets } from 'react-native-safe-area-context';
34
import Animated, {
45
useAnimatedRef,
56
useScrollViewOffset,
67
AnimateProps,
78
} from 'react-native-reanimated';
8-
import { useSafeAreaInsets } from 'react-native-safe-area-context';
9+
910
import FadingView from '../containers/FadingView';
10-
import type { SharedScrollContainerProps } from './types';
1111
import { useScrollContainerLogic } from './useScrollContainerLogic';
12+
import type { SharedScrollContainerProps } from './types';
1213

1314
type AnimatedFlatListType<ItemT = any> = React.ComponentClass<
1415
AnimateProps<FlatListProps<ItemT>>,
@@ -21,13 +22,16 @@ type AnimatedFlatListProps<ItemT> = AnimatedFlatListBaseProps<ItemT> & {
2122
const AnimatedFlatList: AnimatedFlatListType = Animated.createAnimatedComponent(FlatList);
2223

2324
const AnimatedFlatListWithHeaders = <ItemT extends unknown>({
24-
data,
25-
renderItem,
2625
largeHeaderShown,
2726
containerStyle,
2827
LargeHeaderComponent,
2928
largeHeaderContainerStyle,
3029
HeaderComponent,
30+
onLargeHeaderLayout,
31+
onScrollBeginDrag,
32+
onScrollEndDrag,
33+
onMomentumScrollBegin,
34+
onMomentumScrollEnd,
3135
ignoreLeftSafeArea,
3236
ignoreRightSafeArea,
3337
...rest
@@ -58,18 +62,31 @@ const AnimatedFlatListWithHeaders = <ItemT extends unknown>({
5862
<AnimatedFlatList
5963
ref={scrollRef}
6064
scrollEventThrottle={16}
61-
overScrollMode={'auto'}
65+
overScrollMode="auto"
6266
automaticallyAdjustContentInsets={false}
63-
onScrollBeginDrag={debouncedFixScroll.cancel}
64-
onScrollEndDrag={debouncedFixScroll}
65-
onMomentumScrollBegin={debouncedFixScroll.cancel}
66-
onMomentumScrollEnd={debouncedFixScroll}
67-
showsVerticalScrollIndicator={true}
67+
onScrollBeginDrag={(e) => {
68+
debouncedFixScroll.cancel();
69+
if (onScrollBeginDrag) onScrollBeginDrag(e);
70+
}}
71+
onScrollEndDrag={(e) => {
72+
debouncedFixScroll();
73+
if (onScrollEndDrag) onScrollEndDrag(e);
74+
}}
75+
onMomentumScrollBegin={(e) => {
76+
debouncedFixScroll.cancel();
77+
if (onMomentumScrollBegin) onMomentumScrollBegin(e);
78+
}}
79+
onMomentumScrollEnd={(e) => {
80+
debouncedFixScroll();
81+
if (onMomentumScrollEnd) onMomentumScrollEnd(e);
82+
}}
6883
ListHeaderComponent={
6984
LargeHeaderComponent ? (
7085
<View
7186
onLayout={(e) => {
7287
largeHeaderHeight.value = e.nativeEvent.layout.height;
88+
89+
if (onLargeHeaderLayout) onLargeHeaderLayout(e.nativeEvent.layout);
7390
}}
7491
>
7592
<FadingView opacity={largeHeaderOpacity} style={largeHeaderContainerStyle}>
@@ -78,8 +95,6 @@ const AnimatedFlatListWithHeaders = <ItemT extends unknown>({
7895
</View>
7996
) : undefined
8097
}
81-
data={data}
82-
renderItem={renderItem}
8398
{...rest}
8499
/>
85100
</View>

src/components/containers/ScrollView.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
22
import { View, StyleSheet } from 'react-native';
3-
import Animated, { useAnimatedRef, useScrollViewOffset } from 'react-native-reanimated';
43
import { useSafeAreaInsets } from 'react-native-safe-area-context';
4+
import Animated, { useAnimatedRef, useScrollViewOffset } from 'react-native-reanimated';
55

66
import FadingView from './FadingView';
7-
import type { SharedScrollContainerProps } from './types';
87
import { useScrollContainerLogic } from './useScrollContainerLogic';
8+
import type { SharedScrollContainerProps } from './types';
99

1010
type AnimatedScrollViewProps = React.ComponentProps<typeof Animated.ScrollView> & {
1111
children?: React.ReactNode;
@@ -22,6 +22,10 @@ const AnimatedScrollViewWithHeaders: React.FC<
2222
onLargeHeaderLayout,
2323
ignoreLeftSafeArea,
2424
ignoreRightSafeArea,
25+
onScrollBeginDrag,
26+
onScrollEndDrag,
27+
onMomentumScrollBegin,
28+
onMomentumScrollEnd,
2529
children,
2630
...rest
2731
}) => {
@@ -50,13 +54,24 @@ const AnimatedScrollViewWithHeaders: React.FC<
5054
<Animated.ScrollView
5155
ref={scrollRef}
5256
scrollEventThrottle={16}
53-
overScrollMode={'auto'}
57+
overScrollMode="auto"
5458
automaticallyAdjustContentInsets={false}
55-
onScrollBeginDrag={debouncedFixScroll.cancel}
56-
onScrollEndDrag={debouncedFixScroll}
57-
onMomentumScrollBegin={debouncedFixScroll.cancel}
58-
onMomentumScrollEnd={debouncedFixScroll}
59-
showsVerticalScrollIndicator={true}
59+
onScrollBeginDrag={(e) => {
60+
debouncedFixScroll.cancel();
61+
if (onScrollBeginDrag) onScrollBeginDrag(e);
62+
}}
63+
onScrollEndDrag={(e) => {
64+
debouncedFixScroll();
65+
if (onScrollEndDrag) onScrollEndDrag(e);
66+
}}
67+
onMomentumScrollBegin={(e) => {
68+
debouncedFixScroll.cancel();
69+
if (onMomentumScrollBegin) onMomentumScrollBegin(e);
70+
}}
71+
onMomentumScrollEnd={(e) => {
72+
debouncedFixScroll();
73+
if (onMomentumScrollEnd) onMomentumScrollEnd(e);
74+
}}
6075
{...rest}
6176
>
6277
{LargeHeaderComponent ? (

src/components/containers/types.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import type { LayoutRectangle, StyleProp, ViewStyle } from 'react-native';
1+
import type {
2+
LayoutRectangle,
3+
NativeScrollEvent,
4+
NativeSyntheticEvent,
5+
StyleProp,
6+
ViewStyle,
7+
} from 'react-native';
28
import type Animated from 'react-native-reanimated';
39

410
/**
@@ -84,4 +90,28 @@ export type SharedScrollContainerProps = {
8490
* mode on iOS where devices have a notch/status bar on the right side.
8591
*/
8692
ignoreRightSafeArea?: boolean;
93+
/**
94+
* Fires if a user initiates a scroll gesture.
95+
*
96+
* @param {NativeSyntheticEvent<NativeScrollEvent>} event
97+
*/
98+
onScrollBeginDrag?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
99+
/**
100+
* Fires when a user has finished scrolling.
101+
*
102+
* @param {NativeSyntheticEvent<NativeScrollEvent>} event
103+
*/
104+
onScrollEndDrag?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
105+
/**
106+
* Fires when scroll view has begun moving.
107+
*
108+
* @param {NativeSyntheticEvent<NativeScrollEvent>} event
109+
*/
110+
onMomentumScrollBegin?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
111+
/**
112+
* Fires when scroll view has finished moving.
113+
*
114+
* @param {NativeSyntheticEvent<NativeScrollEvent>} event
115+
*/
116+
onMomentumScrollEnd?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
87117
};

0 commit comments

Comments
 (0)