1
1
/* eslint-disable react-hooks/exhaustive-deps */
2
2
import { useEffect , useCallback } from 'react' ;
3
- import { runOnJS , useSharedValue , withSpring , withTiming , withDelay } from 'react-native-reanimated' ;
3
+ import {
4
+ runOnJS ,
5
+ useSharedValue ,
6
+ withSpring ,
7
+ withTiming ,
8
+ withDelay ,
9
+ WithTimingConfig ,
10
+ WithSpringConfig
11
+ } from 'react-native-reanimated' ;
4
12
import type { HiddenLocation } from '../hooks/useHiddenLocation' ;
5
13
import { PanningDirections , PanningDirectionsEnum , DEFAULT_ANIMATION_CONFIG } from '../panView' ;
6
14
import useAnimationEndNotifier , {
@@ -11,6 +19,9 @@ const TransitionViewDirectionEnum = PanningDirectionsEnum;
11
19
type TransitionViewDirection = PanningDirections ;
12
20
export { TransitionViewAnimationType , TransitionViewDirectionEnum , TransitionViewDirection } ;
13
21
22
+ const ENTER_ANIMATION_CONFIG = DEFAULT_ANIMATION_CONFIG ;
23
+ const EXIT_ANIMATION_CONFIG = { duration : 300 } ;
24
+
14
25
interface Delay {
15
26
enter ?: number ;
16
27
exit ?: number ;
@@ -60,40 +71,39 @@ export default function useAnimatedTransition(props: AnimatedTransitionProps) {
60
71
} ;
61
72
} ;
62
73
63
- const initPosition = useCallback ( ( to : { x : number ; y : number } ,
64
- animationDirection : TransitionViewDirection ,
65
- callback : ( isFinished ?: boolean ) => void ) => {
74
+ const initPosition = useCallback ( ( ) => {
66
75
'worklet' ;
76
+ const to = getLocation ( enterFrom ) ;
67
77
// @ts -expect-error
68
- if ( [ TransitionViewDirectionEnum . LEFT , TransitionViewDirectionEnum . RIGHT ] . includes ( animationDirection ) ) {
69
- translationX . value = withTiming ( to . x , { duration : 0 } , callback ) ;
78
+ if ( [ TransitionViewDirectionEnum . LEFT , TransitionViewDirectionEnum . RIGHT ] . includes ( enterFrom ) ) {
79
+ translationX . value = withTiming ( to . x , { duration : 0 } , animateIn ) ;
70
80
// @ts -expect-error
71
- } else if ( [ TransitionViewDirectionEnum . UP , TransitionViewDirectionEnum . DOWN ] . includes ( animationDirection ) ) {
72
- translationY . value = withTiming ( to . y , { duration : 0 } , callback ) ;
81
+ } else if ( [ TransitionViewDirectionEnum . UP , TransitionViewDirectionEnum . DOWN ] . includes ( enterFrom ) ) {
82
+ translationY . value = withTiming ( to . y , { duration : 0 } , animateIn ) ;
73
83
}
74
84
75
85
onInitPosition ( ) ;
76
- } ,
77
- [ onInitPosition ] ) ;
86
+ } , [ onInitPosition ] ) ;
78
87
79
88
useEffect ( ( ) => {
80
89
if ( hiddenLocation . wasMeasured && enterFrom ) {
81
- const location = getLocation ( enterFrom ) ;
82
- initPosition ( location , enterFrom , animateIn ) ;
90
+ initPosition ( ) ;
83
91
}
84
92
} , [ hiddenLocation ] ) ;
85
93
86
94
const translateTo = useCallback ( ( to : { x : number ; y : number } ,
95
+ animation : typeof withTiming | typeof withSpring ,
96
+ animationConfig : WithTimingConfig | WithSpringConfig ,
87
97
animationDirection : TransitionViewDirection ,
88
98
callback : ( isFinished ?: boolean ) => void ,
89
99
delay = 0 ) => {
90
100
'worklet' ;
91
101
// @ts -expect-error
92
102
if ( [ TransitionViewDirectionEnum . LEFT , TransitionViewDirectionEnum . RIGHT ] . includes ( animationDirection ) ) {
93
- translationX . value = withDelay ( delay , withSpring ( to . x , DEFAULT_ANIMATION_CONFIG , callback ) ) ;
103
+ translationX . value = withDelay ( delay , animation ( to . x , animationConfig , callback ) ) ;
94
104
// @ts -expect-error
95
105
} else if ( [ TransitionViewDirectionEnum . UP , TransitionViewDirectionEnum . DOWN ] . includes ( animationDirection ) ) {
96
- translationY . value = withDelay ( delay , withSpring ( to . y , DEFAULT_ANIMATION_CONFIG , callback ) ) ;
106
+ translationY . value = withDelay ( delay , animation ( to . y , animationConfig , callback ) ) ;
97
107
}
98
108
} ,
99
109
[ ] ) ;
@@ -105,7 +115,7 @@ export default function useAnimatedTransition(props: AnimatedTransitionProps) {
105
115
runOnJS ( onAnimationStart ) ( 'enter' ) ;
106
116
}
107
117
108
- translateTo ( { x : 0 , y : 0 } , enterFrom , onEnterAnimationEnd , delay ?. enter ) ;
118
+ translateTo ( { x : 0 , y : 0 } , withSpring , ENTER_ANIMATION_CONFIG , enterFrom , onEnterAnimationEnd , delay ?. enter ) ;
109
119
}
110
120
} , [ onEnterAnimationEnd , delay ?. enter ] ) ;
111
121
@@ -116,7 +126,7 @@ export default function useAnimatedTransition(props: AnimatedTransitionProps) {
116
126
runOnJS ( onAnimationStart ) ( 'exit' ) ;
117
127
}
118
128
119
- translateTo ( getLocation ( exitTo ) , exitTo , onExitAnimationEnd , delay ?. exit ) ;
129
+ translateTo ( getLocation ( exitTo ) , withTiming , EXIT_ANIMATION_CONFIG , exitTo , onExitAnimationEnd , delay ?. exit ) ;
120
130
}
121
131
} , [ hiddenLocation , exitTo , onExitAnimationEnd , delay ?. exit ] ) ;
122
132
0 commit comments