Skip to content

Commit fb305e2

Browse files
authored
TextField - clearButton - fix animation (#3136)
* TextField - clearButton - fix animation * remove overflow hidden and add opacity animation
1 parent daac57c commit fb305e2

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/components/textField/ClearButton.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, {useContext, useCallback, useMemo} from 'react';
22
import {StyleSheet} from 'react-native';
3-
import {useAnimatedStyle, useSharedValue, withSpring} from 'react-native-reanimated';
3+
import {useAnimatedStyle, useSharedValue, withTiming, Easing} from 'react-native-reanimated';
44
import {useDidUpdate} from '../../hooks';
55
import Assets from '../../assets';
66
import {Spacings, Colors} from '../../style';
@@ -10,28 +10,34 @@ import FieldContext from './FieldContext';
1010
import {TextFieldProps} from './types';
1111

1212
const hitSlop = {top: 20, bottom: 20, left: 20, right: 20};
13-
const NON_VISIBLE_POSITION = 100;
13+
const NON_VISIBLE_POSITION = 18;
1414
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+
};
1619

1720
const ClearButton = ({testID, onClear, onChangeText}: Pick<TextFieldProps, 'onClear' | 'testID' | 'onChangeText'>) => {
1821
const {hasValue} = useContext(FieldContext);
1922
const animatedValue = useSharedValue(hasValue ? VISIBLE_POSITION : NON_VISIBLE_POSITION);
23+
const animatedOpacity = useSharedValue(hasValue ? 1 : 0);
2024

2125
// @ts-expect-error should be fixed in version 3.5 (https://github.com/software-mansion/react-native-reanimated/pull/4881)
2226
const animatedStyle = useAnimatedStyle(() => {
2327
return {
24-
transform: [{translateX: animatedValue.value}, {translateY: 0}]
28+
transform: [{translateY: animatedValue.value}, {translateX: 0}],
29+
opacity: animatedOpacity.value
2530
};
2631
});
2732

2833
const style = useMemo(() => [styles.container, animatedStyle], [animatedStyle]);
2934

3035
const animate = useCallback(() => {
3136
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);
3339
},
34-
[animatedValue, hasValue]);
40+
[animatedValue, animatedOpacity, hasValue]);
3541

3642
useDidUpdate(() => {
3743
animate();

0 commit comments

Comments
 (0)