Skip to content

Commit 35abd9f

Browse files
authored
Merge pull request #560 from dohooo/develop
feat: add a new props `minScrollDistancePerSwipe`
2 parents 29425ee + c181174 commit 35abd9f

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

.changeset/violet-buckets-act.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'react-native-reanimated-carousel': patch
3+
---
4+
5+
Add a new props `minScrollDistancePerSwipe` to set the minimum scroll instance to make carousel scroll.
6+

example/website/pages/props.mdx

+9-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,15 @@ Custom animations. For details, see below\[custom animation\]\(.\/custom-animati
289289

290290
### `maxScrollDistancePerSwipe`
291291

292-
Maximum offset value for one scroll. If `props.vertical = true`, this will be `maxScrollDistancePerSwipeY`. If `props.vertical = false`, this will be `maxScrollDistancePerSwipeX`.
292+
Maximum offset value for one scroll. Carousel cannot scroll over than this value.
293+
294+
| type | default | required |
295+
| ------ | ------- | -------- |
296+
| number | - ||
297+
298+
### `minScrollDistancePerSwipe`
299+
300+
Minimum offset value for once scroll. If the translation value is less than this value, the carousel will not scroll.
293301

294302
| type | default | required |
295303
| ------ | ------- | -------- |

src/components/ScrollViewGesture.tsx

+21-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const IScrollViewGesture: React.FC<PropsWithChildren<Props>> = (props) => {
4646
dataLength,
4747
overscrollEnabled,
4848
maxScrollDistancePerSwipe,
49+
minScrollDistancePerSwipe,
4950
fixedDirection,
5051
},
5152
} = React.useContext(CTX);
@@ -71,6 +72,7 @@ const IScrollViewGesture: React.FC<PropsWithChildren<Props>> = (props) => {
7172
const scrollEndVelocity = useSharedValue(0);
7273
const containerRef = useAnimatedRef<Animated.View>();
7374
const maxScrollDistancePerSwipeIsSet = typeof maxScrollDistancePerSwipe === "number";
75+
const minScrollDistancePerSwipeIsSet = typeof minScrollDistancePerSwipe === "number";
7476

7577
// Get the limit of the scroll.
7678
const getLimit = React.useCallback(() => {
@@ -351,10 +353,26 @@ const IScrollViewGesture: React.FC<PropsWithChildren<Props>> = (props) => {
351353

352354
const totalTranslation = scrollEndVelocity.value + scrollEndTranslation.value;
353355

354-
if (maxScrollDistancePerSwipeIsSet && Math.abs(totalTranslation) > maxScrollDistancePerSwipe) {
356+
/**
357+
* If the maximum scroll distance is set and the translation `exceeds the maximum scroll distance`,
358+
* the carousel will keep the view at the current position.
359+
*/
360+
if (
361+
maxScrollDistancePerSwipeIsSet && Math.abs(totalTranslation) > maxScrollDistancePerSwipe
362+
) {
355363
const nextPage = Math.round((panOffset.value + maxScrollDistancePerSwipe * Math.sign(totalTranslation)) / size) * size;
356364
translation.value = withSpring(withProcessTranslation(nextPage), onScrollEnd);
357365
}
366+
/**
367+
* If the minimum scroll distance is set and the translation `didn't exceeds the minimum scroll distance`,
368+
* the carousel will keep the view at the current position.
369+
*/
370+
else if (
371+
minScrollDistancePerSwipeIsSet && Math.abs(totalTranslation) < minScrollDistancePerSwipe
372+
) {
373+
const nextPage = Math.round((panOffset.value + minScrollDistancePerSwipe * Math.sign(totalTranslation)) / size) * size;
374+
translation.value = withSpring(withProcessTranslation(nextPage), onScrollEnd);
375+
}
358376
else {
359377
endWithSpring(onScrollEnd);
360378
}
@@ -373,6 +391,8 @@ const IScrollViewGesture: React.FC<PropsWithChildren<Props>> = (props) => {
373391
fixedDirection,
374392
maxScrollDistancePerSwipeIsSet,
375393
maxScrollDistancePerSwipe,
394+
maxScrollDistancePerSwipeIsSet,
395+
minScrollDistancePerSwipe,
376396
endWithSpring,
377397
withSpring,
378398
onScrollEnd,

src/types.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,14 @@ export type TCarouselProps<T = any> = {
153153
testID?: string
154154
/**
155155
* Maximum offset value for once scroll.
156-
* props.vertical = true => maxScrollDistancePerSwipeY
157-
* props.vertical = false => maxScrollDistancePerSwipeX
156+
* Carousel cannot scroll over than this value.
158157
* */
159158
maxScrollDistancePerSwipe?: number
159+
/**
160+
* Minimum offset value for once scroll.
161+
* If the translation value is less than this value, the carousel will not scroll.
162+
* */
163+
minScrollDistancePerSwipe?: number
160164
/**
161165
* @experimental This API will be changed in the future.
162166
* If positive, the carousel will scroll to the positive direction and vice versa.

0 commit comments

Comments
 (0)