Skip to content

Commit aeeb9a2

Browse files
authored
feat: added onAnimate callback (#127)
* chore: added onAnimate callback implementation * chore: updated examples
1 parent 23c1b99 commit aeeb9a2

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

example/src/screens/basic/BasicExamples.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ const createExampleScreen = ({ type, count = 25 }: ExampleScreenProps) =>
4949
const handleSheetChange = useCallback(index => {
5050
console.log('handleSheetChange', index);
5151
}, []);
52+
const handleSheetAnimate = useCallback(
53+
(fromIndex: number, toIndex: number) => {
54+
console.log('handleSheetAnimate', `from ${fromIndex} to ${toIndex}`);
55+
},
56+
[]
57+
);
5258
const handleSnapPress = useCallback(index => {
5359
bottomSheetRef.current?.snapTo(index);
5460
}, []);
@@ -119,6 +125,7 @@ const createExampleScreen = ({ type, count = 25 }: ExampleScreenProps) =>
119125
enableContentPanningGesture={enableContentPanningGesture}
120126
enableHandlePanningGesture={enableHandlePanningGesture}
121127
onChange={handleSheetChange}
128+
onAnimate={handleSheetAnimate}
122129
>
123130
<ContactList key={`${type}.list`} type={type} count={count} />
124131
</BottomSheet>

src/components/bottomSheet/BottomSheet.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
8989
animatedIndex: _providedAnimatedIndex,
9090
// callbacks
9191
onChange: _providedOnChange,
92+
onAnimate: _providedOnAnimate,
9293
// components
9394
handleComponent,
9495
backdropComponent,
@@ -214,6 +215,18 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
214215
},
215216
[_providedOnChange]
216217
);
218+
const handleOnAnimate = useCallback(
219+
(toPoint: number) => {
220+
if (!_providedOnAnimate) {
221+
return;
222+
}
223+
const toIndex = snapPoints.findIndex(item => item === toPoint);
224+
if (toIndex !== currentIndexRef.current) {
225+
_providedOnAnimate(currentIndexRef.current, toIndex);
226+
}
227+
},
228+
[_providedOnAnimate, snapPoints]
229+
);
217230
const handleSettingScrollableRef = useCallback(
218231
(scrollableRef: ScrollableRef) => {
219232
setScrollableRef(scrollableRef);
@@ -266,6 +279,9 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
266279
const animateToPoint = useCallback(
267280
(point: number) => {
268281
'worklet';
282+
283+
runOnJS(handleOnAnimate)(point);
284+
269285
animationState.value = ANIMATION_STATE.RUNNING;
270286
animatedPosition.value = withTiming(
271287
point,
@@ -282,6 +298,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
282298
animationDuration,
283299
animationEasing,
284300
animateToPointCompleted,
301+
handleOnAnimate,
285302
]
286303
);
287304

src/components/bottomSheet/types.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ export interface BottomSheetProps extends BottomSheetAnimationConfigs {
8181
* @type (index: number) => void;
8282
*/
8383
onChange?: (index: number) => void;
84+
/**
85+
* Callback when the sheet about to animate to a new position.
86+
* @type (fromIndex: number, toIndex: number) => void;
87+
*/
88+
onAnimate?: (fromIndex: number, toIndex: number) => void;
8489

8590
// components
8691
/**

0 commit comments

Comments
 (0)