Skip to content

Commit 898270e

Browse files
souyahiagorhom
andauthored
fix: added positions to onAnimate, and prevent index to be negative with keyboard animations (#2271)(by @souyahia)
* feat: add position params in onAnimate event * fix: prevent index from going to negative on animate with keyboard events --------- Co-authored-by: gorhom <[email protected]>
1 parent 1ffc71b commit 898270e

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

src/components/bottomSheet/BottomSheet.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,15 +536,17 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
536536
);
537537
// biome-ignore lint/correctness/useExhaustiveDependencies(BottomSheet.name): used for debug only
538538
const handleOnAnimate = useCallback(
539-
function handleOnAnimate(targetIndex: number) {
539+
function handleOnAnimate(targetIndex: number, targetPosition: number) {
540540
if (__DEV__) {
541541
print({
542542
component: BottomSheet.name,
543543
method: handleOnAnimate.name,
544544
category: 'callback',
545545
params: {
546546
toIndex: targetIndex,
547+
toPosition: targetPosition,
547548
fromIndex: animatedCurrentIndex.value,
549+
fromPosition: animatedPosition.value,
548550
},
549551
});
550552
}
@@ -554,10 +556,15 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
554556
}
555557

556558
if (targetIndex !== animatedCurrentIndex.value) {
557-
_providedOnAnimate(animatedCurrentIndex.value, targetIndex);
559+
_providedOnAnimate(
560+
animatedCurrentIndex.value,
561+
targetIndex,
562+
animatedPosition.value,
563+
targetPosition
564+
);
558565
}
559566
},
560-
[_providedOnAnimate, animatedCurrentIndex]
567+
[_providedOnAnimate, animatedCurrentIndex, animatedPosition]
561568
);
562569
//#endregion
563570

@@ -643,10 +650,15 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
643650
animatedNextPosition.value = position;
644651

645652
/**
646-
* offset the position if keyboard is shown
653+
* offset the position if keyboard is shown,
654+
* and behavior not extend.
647655
*/
648656
let offset = 0;
649-
if (animatedKeyboardState.value === KEYBOARD_STATE.SHOWN) {
657+
if (
658+
animatedKeyboardState.value === KEYBOARD_STATE.SHOWN &&
659+
keyboardBehavior !== KEYBOARD_BEHAVIOR.extend &&
660+
position < animatedPosition.value
661+
) {
650662
offset = animatedKeyboardHeightInContainer.value;
651663
}
652664

@@ -657,7 +669,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
657669
/**
658670
* fire `onAnimate` callback
659671
*/
660-
runOnJS(handleOnAnimate)(animatedNextPositionIndex.value);
672+
runOnJS(handleOnAnimate)(animatedNextPositionIndex.value, position);
661673

662674
/**
663675
* start animation
@@ -672,6 +684,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
672684
},
673685
[
674686
handleOnAnimate,
687+
keyboardBehavior,
675688
_providedAnimationConfigs,
676689
_providedOverrideReduceMotion,
677690
]

src/components/bottomSheet/types.d.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,14 @@ export interface BottomSheetProps
264264
/**
265265
* Callback when the sheet about to animate to a new position.
266266
*
267-
* @type (fromIndex: number, toIndex: number) => void;
268-
*/
269-
onAnimate?: (fromIndex: number, toIndex: number) => void;
267+
* @type (fromIndex: number, toIndex: number, fromPosition: number, toPosition: number) => void;
268+
*/
269+
onAnimate?: (
270+
fromIndex: number,
271+
toIndex: number,
272+
fromPosition: number,
273+
toPosition: number
274+
) => void;
270275
//#endregion
271276

272277
//#region components

src/components/bottomSheetModal/BottomSheetModal.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,16 @@ function BottomSheetModalComponent<T = any>(
379379
[_providedOnChange]
380380
);
381381
const handleBottomSheetOnAnimate = useCallback(
382-
(fromIndex: number, toIndex: number) => {
382+
(
383+
fromIndex: number,
384+
toIndex: number,
385+
fromPosition: number,
386+
toPosition: number
387+
) => {
383388
nextIndexRef.current = toIndex;
384389

385390
if (_providedOnAnimate) {
386-
_providedOnAnimate(fromIndex, toIndex);
391+
_providedOnAnimate(fromIndex, toIndex, fromPosition, toPosition);
387392
}
388393
},
389394
[_providedOnAnimate]

website/docs/props.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ type onChange = (index: number) => void;
372372
Callback when the sheet about to animate to a new position.
373373

374374
```ts
375-
type onAnimate = (fromIndex: number, toIndex: number) => void;
375+
type onAnimate = (fromIndex: number, toIndex: number, fromPosition: number, toPosition: number) => void;
376376
```
377377

378378
| type | default | required |

0 commit comments

Comments
 (0)