Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: next item function overscolling with overscrollEnabled is false #555

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion src/components/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { computedRealIndexWithAutoFillData } from "../utils/computed-with-auto-f

const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
(_props, ref) => {
const [containerWidth, setContainerWidth] = React.useState(0);
const props = useInitProps(_props);

const {
Expand Down Expand Up @@ -87,6 +88,8 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
duration: scrollAnimationDuration,
onScrollEnd: () => runOnJS(_onScrollEnd)(),
onScrollStart: () => !!onScrollStart && runOnJS(onScrollStart)(),
containerWidth,
overscrollEnabled: props.overscrollEnabled,
});

const { next, prev, scrollTo, getSharedIndex, getCurrentIndex }
Expand Down Expand Up @@ -155,7 +158,12 @@ const Carousel = React.forwardRef<ICarouselInstance, TCarouselProps<any>>(
const layoutConfig = useLayoutConfig({ ...props, size });

return (
<GestureHandlerRootView>
<GestureHandlerRootView
onLayout={(event) => {
const { width } = event.nativeEvent.layout;
setContainerWidth(width);
}}
>
<CTX.Provider value={{ props, common: commonVariables }}>
<ScrollViewGesture
key={mode}
Expand Down
31 changes: 23 additions & 8 deletions src/hooks/useCarouselController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ interface IOpts {
defaultIndex?: number
onScrollStart?: () => void
onScrollEnd?: () => void
containerWidth: number
overscrollEnabled?: boolean
}

export interface ICarouselController {
Expand All @@ -50,6 +52,8 @@ export function useCarouselController(options: IOpts): ICarouselController {
duration,
autoFillData,
fixedDirection,
containerWidth,
overscrollEnabled,
} = options;

const dataInfo = React.useMemo(
Expand Down Expand Up @@ -177,15 +181,16 @@ export function useCarouselController(options: IOpts): ICarouselController {

const nextPage = currentFixedPage() + count;
index.value = nextPage;
let value = -nextPage * size;
if (!loop && !overscrollEnabled) {
value = Math.max(value, -(dataLength * size - containerWidth));
}

if (animated) {
handlerOffset.value = scrollWithTiming(
-nextPage * size,
onFinished,
) as any;
handlerOffset.value = scrollWithTiming(value, onFinished) as any;
}
else {
handlerOffset.value = -nextPage * size;
handlerOffset.value = value;
onFinished?.();
}
},
Expand All @@ -205,21 +210,31 @@ export function useCarouselController(options: IOpts): ICarouselController {
const prev = React.useCallback(
(opts: TCarouselActionOptions = {}) => {
const { count = 1, animated = true, onFinished } = opts;
if (!canSliding() || (!loop && index.value <= 0)) return;
/* Checking handlerOffset.value === 0 because if going to the next item doesn't
clear the first element (can happen if overscrollEnabled = false), then we wouldn't
be allowed to go to the previous item
*/
if (!canSliding() || (!loop && index.value <= 0 && handlerOffset.value === 0)) return;

onScrollStart?.();

const prevPage = currentFixedPage() - count;
index.value = prevPage;

let value = -prevPage * size;
// Avoid overscrolling the first item
if(!loop && value > 0){
value = 0
}

if (animated) {
handlerOffset.value = scrollWithTiming(
-prevPage * size,
value,
onFinished,
);
}
else {
handlerOffset.value = -prevPage * size;
handlerOffset.value = value;
onFinished?.();
}
},
Expand Down