1
1
/* eslint-disable react-hooks/exhaustive-deps */
2
2
import { useEffect , useCallback } from 'react' ;
3
- import { runOnJS , useSharedValue , withSpring , withTiming } from 'react-native-reanimated' ;
4
- import { HiddenLocation } from '../hooks/useHiddenLocation' ;
3
+ import { runOnJS , useSharedValue , withSpring , withTiming , withDelay } from 'react-native-reanimated' ;
4
+ import type { HiddenLocation } from '../hooks/useHiddenLocation' ;
5
5
import { PanningDirections , PanningDirectionsEnum , DEFAULT_ANIMATION_CONFIG } from '../panView' ;
6
6
import useAnimationEndNotifier , {
7
7
AnimationNotifierEndProps ,
@@ -11,6 +11,11 @@ const TransitionViewDirectionEnum = PanningDirectionsEnum;
11
11
type TransitionViewDirection = PanningDirections ;
12
12
export { TransitionViewAnimationType , TransitionViewDirectionEnum , TransitionViewDirection } ;
13
13
14
+ interface Delay {
15
+ enter ?: number ;
16
+ exit ?: number ;
17
+ }
18
+
14
19
export interface AnimatedTransitionProps extends AnimationNotifierEndProps {
15
20
/**
16
21
* Callback to the animation start.
@@ -24,12 +29,16 @@ export interface AnimatedTransitionProps extends AnimationNotifierEndProps {
24
29
* If this is given there will be an exit animation to this direction.
25
30
*/
26
31
exitTo ?: TransitionViewDirection ;
32
+ /**
33
+ * Delay the enter \ exit animation.
34
+ */
35
+ delay ?: Delay ;
27
36
hiddenLocation : HiddenLocation ;
28
37
onInitPosition : ( ) => void ;
29
38
}
30
39
31
40
export default function useAnimatedTransition ( props : AnimatedTransitionProps ) {
32
- const { hiddenLocation, onInitPosition, enterFrom, exitTo, onAnimationStart, onAnimationEnd} = props ;
41
+ const { hiddenLocation, onInitPosition, enterFrom, exitTo, onAnimationStart, onAnimationEnd, delay } = props ;
33
42
34
43
// Has to start at {0, 0} with {opacity: 0} so layout can be measured
35
44
const translationX = useSharedValue < number > ( 0 ) ;
@@ -76,14 +85,15 @@ export default function useAnimatedTransition(props: AnimatedTransitionProps) {
76
85
77
86
const translateTo = useCallback ( ( to : { x : number ; y : number } ,
78
87
animationDirection : TransitionViewDirection ,
79
- callback : ( isFinished ?: boolean ) => void ) => {
88
+ callback : ( isFinished ?: boolean ) => void ,
89
+ delay = 0 ) => {
80
90
'worklet' ;
81
91
// @ts -expect-error
82
92
if ( [ TransitionViewDirectionEnum . LEFT , TransitionViewDirectionEnum . RIGHT ] . includes ( animationDirection ) ) {
83
- translationX . value = withSpring ( to . x , DEFAULT_ANIMATION_CONFIG , callback ) ;
93
+ translationX . value = withDelay ( delay , withSpring ( to . x , DEFAULT_ANIMATION_CONFIG , callback ) ) ;
84
94
// @ts -expect-error
85
95
} else if ( [ TransitionViewDirectionEnum . UP , TransitionViewDirectionEnum . DOWN ] . includes ( animationDirection ) ) {
86
- translationY . value = withSpring ( to . y , DEFAULT_ANIMATION_CONFIG , callback ) ;
96
+ translationY . value = withDelay ( delay , withSpring ( to . y , DEFAULT_ANIMATION_CONFIG , callback ) ) ;
87
97
}
88
98
} ,
89
99
[ ] ) ;
@@ -95,9 +105,9 @@ export default function useAnimatedTransition(props: AnimatedTransitionProps) {
95
105
runOnJS ( onAnimationStart ) ( 'enter' ) ;
96
106
}
97
107
98
- translateTo ( { x : 0 , y : 0 } , enterFrom , onEnterAnimationEnd ) ;
108
+ translateTo ( { x : 0 , y : 0 } , enterFrom , onEnterAnimationEnd , delay ?. enter ) ;
99
109
}
100
- } , [ onEnterAnimationEnd ] ) ;
110
+ } , [ onEnterAnimationEnd , delay ?. enter ] ) ;
101
111
102
112
const animateOut = useCallback ( ( ) => {
103
113
'worklet' ;
@@ -106,9 +116,9 @@ export default function useAnimatedTransition(props: AnimatedTransitionProps) {
106
116
runOnJS ( onAnimationStart ) ( 'exit' ) ;
107
117
}
108
118
109
- translateTo ( getLocation ( exitTo ) , exitTo , onExitAnimationEnd ) ;
119
+ translateTo ( getLocation ( exitTo ) , exitTo , onExitAnimationEnd , delay ?. exit ) ;
110
120
}
111
- } , [ hiddenLocation , exitTo , onExitAnimationEnd ] ) ;
121
+ } , [ hiddenLocation , exitTo , onExitAnimationEnd , delay ?. exit ] ) ;
112
122
113
123
return { animateIn, animateOut, translation : { x : translationX , y : translationY } } ;
114
124
}
0 commit comments