Skip to content

Commit 84ba93d

Browse files
committed
feat: add one snap point change per one swipe
1 parent d12f3f7 commit 84ba93d

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

src/components/bottomSheet/BottomSheet.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import {
7979
DEFAULT_KEYBOARD_BLUR_BEHAVIOR,
8080
DEFAULT_KEYBOARD_INDEX,
8181
DEFAULT_KEYBOARD_INPUT_MODE,
82+
DEFAULT_ONE_SNAP_POINT_PER_SWIPE,
8283
DEFAULT_OVER_DRAG_RESISTANCE_FACTOR,
8384
INITIAL_POSITION,
8485
INITIAL_VALUE,
@@ -107,6 +108,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
107108
enableOverDrag = DEFAULT_ENABLE_OVER_DRAG,
108109
enablePanDownToClose = DEFAULT_ENABLE_PAN_DOWN_TO_CLOSE,
109110
enableDynamicSizing = DEFAULT_DYNAMIC_SIZING,
111+
enableOneSnapPointPerSwipe = DEFAULT_ONE_SNAP_POINT_PER_SWIPE,
110112
overDragResistanceFactor = DEFAULT_OVER_DRAG_RESISTANCE_FACTOR,
111113
overrideReduceMotion: _providedOverrideReduceMotion,
112114

@@ -1436,6 +1438,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
14361438
overDragResistanceFactor,
14371439
enableOverDrag,
14381440
enablePanDownToClose,
1441+
enableOneSnapPointPerSwipe,
14391442
animatedAnimationState,
14401443
animatedSheetState,
14411444
animatedScrollableState,
@@ -1480,6 +1483,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
14801483
overDragResistanceFactor,
14811484
enableOverDrag,
14821485
enablePanDownToClose,
1486+
enableOneSnapPointPerSwipe,
14831487
enableDynamicSizing,
14841488
enableBlurKeyboardOnGesture,
14851489
_providedSimultaneousHandlers,

src/components/bottomSheet/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const DEFAULT_ENABLE_OVER_DRAG = true;
1414
const DEFAULT_ENABLE_PAN_DOWN_TO_CLOSE = false;
1515
const DEFAULT_ANIMATE_ON_MOUNT = true;
1616
const DEFAULT_DYNAMIC_SIZING = true;
17+
const DEFAULT_ONE_SNAP_POINT_PER_SWIPE = false;
1718

1819
// keyboard
1920
const DEFAULT_KEYBOARD_BEHAVIOR = KEYBOARD_BEHAVIOR.interactive;
@@ -35,6 +36,7 @@ const DEFAULT_ACCESSIBILITY_ROLE = 'adjustable';
3536
export {
3637
DEFAULT_HANDLE_HEIGHT,
3738
DEFAULT_OVER_DRAG_RESISTANCE_FACTOR,
39+
DEFAULT_ONE_SNAP_POINT_PER_SWIPE,
3840
DEFAULT_ENABLE_CONTENT_PANNING_GESTURE,
3941
DEFAULT_ENABLE_HANDLE_PANNING_GESTURE,
4042
DEFAULT_ENABLE_OVER_DRAG,

src/components/bottomSheet/types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ export interface BottomSheetProps
9090
* @default true
9191
*/
9292
enableDynamicSizing?: boolean;
93+
/**
94+
* Enable change of one snap point per one swipe.
95+
* Works only with default Gesture Events Handlers.
96+
* @type boolean
97+
* @default false
98+
*/
99+
enableOneSnapPointPerSwipe?: boolean;
93100
/**
94101
* To start the sheet closed and snap to initial index when it's mounted.
95102
* @type boolean

src/contexts/internal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface BottomSheetInternalContextType
2828
| 'enableDynamicSizing'
2929
| 'enableBlurKeyboardOnGesture'
3030
| 'overDragResistanceFactor'
31+
| 'enableOneSnapPointPerSwipe'
3132
>
3233
> {
3334
// animated states

src/hooks/useGestureEventsHandlersDefault.tsx

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
4949
animatedLayoutState,
5050
enableOverDrag,
5151
enablePanDownToClose,
52+
enableOneSnapPointPerSwipe,
5253
overDragResistanceFactor,
5354
isInTemporaryPosition,
5455
enableBlurKeyboardOnGesture,
@@ -142,10 +143,25 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
142143
}
143144

144145
const { containerHeight } = animatedLayoutState.get();
145-
const lowestSnapPoint = enablePanDownToClose
146+
let lowestSnapPoint = enablePanDownToClose
146147
? containerHeight
147148
: detents[0];
148149

150+
if (enableOneSnapPointPerSwipe) {
151+
const currentIndex = detents.indexOf(
152+
context.value.initialPosition
153+
);
154+
155+
const nextIndex = Math.min(
156+
currentIndex + 1,
157+
detents.length - 1
158+
);
159+
const prevIndex = Math.max(currentIndex - 1, 0);
160+
161+
highestSnapPoint = detents[nextIndex];
162+
lowestSnapPoint = detents[prevIndex];
163+
}
164+
149165
/**
150166
* if scrollable is refreshable and sheet position at the highest
151167
* point, then do not interact with current gesture.
@@ -401,6 +417,24 @@ export const useGestureEventsHandlersDefault: GestureEventsHandlersHookType =
401417
return;
402418
}
403419

420+
if (enableOneSnapPointPerSwipe) {
421+
const currentIndex = snapPoints.indexOf(destinationPoint);
422+
const prevIndex = snapPoints.indexOf(context.value.initialPosition);
423+
424+
if (Math.abs(prevIndex - currentIndex) - 1) {
425+
const newIndex =
426+
prevIndex > currentIndex ? currentIndex + 1 : currentIndex - 1;
427+
428+
animateToPosition(
429+
snapPoints[newIndex],
430+
ANIMATION_SOURCE.GESTURE,
431+
velocityY / 2
432+
);
433+
434+
return;
435+
}
436+
}
437+
404438
animateToPosition(
405439
destinationPoint,
406440
ANIMATION_SOURCE.GESTURE,

0 commit comments

Comments
 (0)