Skip to content

Commit e5f96be

Browse files
committed
feat: add prop disableAutoFixScroll to disable/enable auto scroll adjustment
1 parent 3c1ee30 commit e5f96be

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

example/src/screens/usage/FlatList.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const FlatList: React.FC<FlatListUsageScreenNavigationProps> = () => {
8686
getItemLayout={(_, index) => ({ index, length: ITEM_HEIGHT, offset: index * ITEM_HEIGHT })}
8787
initialNumToRender={50}
8888
maxToRenderPerBatch={100}
89+
disableAutoFixScroll
8990
keyExtractor={(_, i) => `text-row-${i}`}
9091
/>
9192
);

src/components/containers/FlatList.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const AnimatedFlatListWithHeaders = <ItemT extends unknown>({
3434
onMomentumScrollEnd,
3535
ignoreLeftSafeArea,
3636
ignoreRightSafeArea,
37+
disableAutoFixScroll,
3738
...rest
3839
}: AnimatedFlatListProps<ItemT> & SharedScrollContainerProps) => {
3940
const insets = useSafeAreaInsets();
@@ -46,6 +47,7 @@ const AnimatedFlatListWithHeaders = <ItemT extends unknown>({
4647
scrollRef,
4748
scrollY,
4849
largeHeaderShown,
50+
disableAutoFixScroll,
4951
largeHeaderExists: !!LargeHeaderComponent,
5052
});
5153

src/components/containers/ScrollView.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const AnimatedScrollViewWithHeaders: React.FC<
2626
onScrollEndDrag,
2727
onMomentumScrollBegin,
2828
onMomentumScrollEnd,
29+
disableAutoFixScroll,
2930
children,
3031
...rest
3132
}) => {
@@ -38,6 +39,7 @@ const AnimatedScrollViewWithHeaders: React.FC<
3839
scrollRef,
3940
scrollY,
4041
largeHeaderShown,
42+
disableAutoFixScroll,
4143
largeHeaderExists: !!LargeHeaderComponent,
4244
});
4345

src/components/containers/types.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,56 +62,55 @@ export type SharedScrollContainerProps = {
6262
HeaderComponent: (props: ScrollHeaderProps) => React.ReactNode;
6363
/**
6464
* This is executed when the onLayout event is fired on the large header container component.
65-
*
66-
* @param {LayoutRectangle} rect
6765
*/
6866
onLargeHeaderLayout?: (rect: LayoutRectangle) => void;
6967
/**
7068
* The large header's container style.
7169
*
7270
* @default undefined
73-
* @type {StyleProp<ViewStyle>}
7471
*/
7572
largeHeaderContainerStyle?: StyleProp<ViewStyle>;
7673
/**
7774
* The style of the root container of the scoll container.
7875
*
7976
* @default undefined
80-
* @type {StyleProp<ViewStyle>}
8177
*/
8278
containerStyle?: StyleProp<ViewStyle>;
8379
/**
8480
* Whether the scroll container should ignore the left safe area. This is useful for landscape
8581
* mode on iOS where devices have a notch/status bar on the left side.
82+
*
83+
* @default false
8684
*/
8785
ignoreLeftSafeArea?: boolean;
8886
/**
8987
* Whether the scroll container should ignore the right safe area. This is useful for landscape
9088
* mode on iOS where devices have a notch/status bar on the right side.
89+
*
90+
* @default false
9191
*/
9292
ignoreRightSafeArea?: boolean;
9393
/**
94-
* Fires if a user initiates a scroll gesture.
94+
* Disables the auto fix scroll mechanism. This is useful if you want to disable the auto scroll
95+
* when the large header is partially visible.
9596
*
96-
* @param {NativeSyntheticEvent<NativeScrollEvent>} event
97+
* @default false
98+
*/
99+
disableAutoFixScroll?: boolean;
100+
/**
101+
* Fires if a user initiates a scroll gesture.
97102
*/
98103
onScrollBeginDrag?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
99104
/**
100105
* Fires when a user has finished scrolling.
101-
*
102-
* @param {NativeSyntheticEvent<NativeScrollEvent>} event
103106
*/
104107
onScrollEndDrag?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
105108
/**
106109
* Fires when scroll view has begun moving.
107-
*
108-
* @param {NativeSyntheticEvent<NativeScrollEvent>} event
109110
*/
110111
onMomentumScrollBegin?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
111112
/**
112113
* Fires when scroll view has finished moving.
113-
*
114-
* @param {NativeSyntheticEvent<NativeScrollEvent>} event
115114
*/
116115
onMomentumScrollEnd?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
117116
};

src/components/containers/useScrollContainerLogic.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,17 @@ interface UseScrollContainerLogicArgs {
3939
* @type {SharedScrollContainerProps['largeHeaderShown']}
4040
*/
4141
largeHeaderShown: SharedScrollContainerProps['largeHeaderShown'];
42-
42+
/**
43+
* Whether or not the large header exists.
44+
*/
4345
largeHeaderExists: boolean;
46+
/**
47+
* Disables the auto fix scroll mechanism. This is useful if you want to disable the auto scroll
48+
* when the large header is partially visible.
49+
*
50+
* @default false
51+
*/
52+
disableAutoFixScroll?: boolean;
4453
}
4554

4655
/**
@@ -54,6 +63,7 @@ export const useScrollContainerLogic = ({
5463
scrollY,
5564
largeHeaderShown,
5665
largeHeaderExists,
66+
disableAutoFixScroll = false,
5767
adjustmentOffset = 4,
5868
}: UseScrollContainerLogicArgs) => {
5969
const largeHeaderHeight = useSharedValue(0);
@@ -82,6 +92,8 @@ export const useScrollContainerLogic = ({
8292
});
8393

8494
const debouncedFixScroll = useDebouncedCallback(() => {
95+
if (disableAutoFixScroll) return;
96+
8597
if (largeHeaderHeight.value !== 0 && scrollRef && scrollRef.current) {
8698
if (scrollY.value >= largeHeaderHeight.value / 2 && scrollY.value < largeHeaderHeight.value) {
8799
// Scroll to end of large header

0 commit comments

Comments
 (0)