1
1
import React , { useContext , useCallback , useMemo } from 'react' ;
2
2
import { StyleSheet } from 'react-native' ;
3
- import { useAnimatedStyle , useSharedValue , withSpring } from 'react-native-reanimated' ;
3
+ import { useAnimatedStyle , useSharedValue , withTiming , Easing } from 'react-native-reanimated' ;
4
4
import { useDidUpdate } from '../../hooks' ;
5
5
import Assets from '../../assets' ;
6
6
import { Spacings , Colors } from '../../style' ;
@@ -10,28 +10,34 @@ import FieldContext from './FieldContext';
10
10
import { TextFieldProps } from './types' ;
11
11
12
12
const hitSlop = { top : 20 , bottom : 20 , left : 20 , right : 20 } ;
13
- const NON_VISIBLE_POSITION = 100 ;
13
+ const NON_VISIBLE_POSITION = 18 ;
14
14
const VISIBLE_POSITION = 0 ;
15
- const SPRING_ANIMATION_CONFIG = { velocity : 300 , damping : 20 , stiffness : 300 , mass : 0.8 } ;
15
+ const TIMING_CONFIG = {
16
+ duration : 200 ,
17
+ easing : Easing . bezier ( 0.33 , 1 , 0.68 , 1 )
18
+ } ;
16
19
17
20
const ClearButton = ( { testID, onClear, onChangeText} : Pick < TextFieldProps , 'onClear' | 'testID' | 'onChangeText' > ) => {
18
21
const { hasValue} = useContext ( FieldContext ) ;
19
22
const animatedValue = useSharedValue ( hasValue ? VISIBLE_POSITION : NON_VISIBLE_POSITION ) ;
23
+ const animatedOpacity = useSharedValue ( hasValue ? 1 : 0 ) ;
20
24
21
25
// @ts -expect-error should be fixed in version 3.5 (https://github.com/software-mansion/react-native-reanimated/pull/4881)
22
26
const animatedStyle = useAnimatedStyle ( ( ) => {
23
27
return {
24
- transform : [ { translateX : animatedValue . value } , { translateY : 0 } ]
28
+ transform : [ { translateY : animatedValue . value } , { translateX : 0 } ] ,
29
+ opacity : animatedOpacity . value
25
30
} ;
26
31
} ) ;
27
32
28
33
const style = useMemo ( ( ) => [ styles . container , animatedStyle ] , [ animatedStyle ] ) ;
29
34
30
35
const animate = useCallback ( ( ) => {
31
36
const toValue = hasValue ? VISIBLE_POSITION : NON_VISIBLE_POSITION ;
32
- animatedValue . value = withSpring ( toValue , SPRING_ANIMATION_CONFIG ) ;
37
+ animatedValue . value = withTiming ( toValue , TIMING_CONFIG ) ;
38
+ animatedOpacity . value = withTiming ( hasValue ? 1 : 0 , TIMING_CONFIG ) ;
33
39
} ,
34
- [ animatedValue , hasValue ] ) ;
40
+ [ animatedValue , animatedOpacity , hasValue ] ) ;
35
41
36
42
useDidUpdate ( ( ) => {
37
43
animate ( ) ;
0 commit comments