Skip to content

Commit

Permalink
fix: reset scrollable offset when its size change (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhom committed Feb 20, 2021
1 parent 10be2f1 commit bf7a255
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/flatList/FlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const BottomSheetFlatListComponent = forwardRef(
const {
scrollableRef,
handleOnBeginDragEvent,
handleOnContentSizeChange,
handleSettingScrollable,
} = useScrollableInternal('FlatList');
const {
Expand Down Expand Up @@ -74,6 +75,7 @@ const BottomSheetFlatListComponent = forwardRef(
bounces={false}
decelerationRate={decelerationRate}
scrollEventThrottle={16}
onContentSizeChange={handleOnContentSizeChange}
onScrollBeginDrag={handleOnBeginDragEvent}
/>
</NativeViewGestureHandler>
Expand Down
2 changes: 2 additions & 0 deletions src/components/scrollView/ScrollView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const BottomSheetScrollViewComponent = forwardRef(
const {
scrollableRef,
handleOnBeginDragEvent,
handleOnContentSizeChange,
handleSettingScrollable,
} = useScrollableInternal('ScrollView');
const {
Expand Down Expand Up @@ -72,6 +73,7 @@ const BottomSheetScrollViewComponent = forwardRef(
bounces={false}
decelerationRate={decelerationRate}
scrollEventThrottle={16}
onContentSizeChange={handleOnContentSizeChange}
onScrollBeginDrag={handleOnBeginDragEvent}
/>
</NativeViewGestureHandler>
Expand Down
2 changes: 2 additions & 0 deletions src/components/sectionList/SectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const BottomSheetSectionListComponent = forwardRef(
const {
scrollableRef,
handleOnBeginDragEvent,
handleOnContentSizeChange,
handleSettingScrollable,
} = useScrollableInternal('SectionList');
const {
Expand Down Expand Up @@ -73,6 +74,7 @@ const BottomSheetSectionListComponent = forwardRef(
bounces={false}
decelerationRate={decelerationRate}
scrollEventThrottle={16}
onContentSizeChange={handleOnContentSizeChange}
onScrollBeginDrag={handleOnBeginDragEvent}
/>
</NativeViewGestureHandler>
Expand Down
15 changes: 15 additions & 0 deletions src/hooks/useScrollableInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Scrollable, ScrollableType } from '../types';

export const useScrollableInternal = (type: ScrollableType) => {
// refs
const scrollableContentHeightRef = useRef<number>(0);
const scrollableContentOffsetYRef = useRef<number>(0);
const scrollableContentOffsetY = useValue<number>(0);
const scrollableRef = useRef<Scrollable>(null);
Expand All @@ -19,6 +20,19 @@ export const useScrollableInternal = (type: ScrollableType) => {
} = useBottomSheetInternal();

// callbacks
/**
* Reset the scrollable offset y when its size get smaller,
* this fixes #286.
*/
const handleOnContentSizeChange = useCallback(
(_, height: number) => {
if (scrollableContentHeightRef.current > height) {
scrollableContentOffsetY.setValue(0);
}
scrollableContentHeightRef.current = height;
},
[scrollableContentOffsetY]
);
const handleOnBeginDragEvent = useMemo(
() =>
event([
Expand Down Expand Up @@ -66,6 +80,7 @@ export const useScrollableInternal = (type: ScrollableType) => {
return {
scrollableRef,
handleOnBeginDragEvent,
handleOnContentSizeChange,
handleSettingScrollable,
};
};

0 comments on commit bf7a255

Please sign in to comment.