-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextNoteButton.js
More file actions
63 lines (52 loc) · 1.6 KB
/
TextNoteButton.js
File metadata and controls
63 lines (52 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React, { useState, useEffect, useRef } from 'react';
import { Text, Animated, StyleSheet, Pressable } from 'react-native';
const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
const TextNoteButton = ({onDelete, item}) => {
const fadeAnim = useRef(new Animated.Value(0)).current;
const [isPressed, setIsPressed] = useState(false);
function animateColor(toValue) {
Animated.timing(fadeAnim, {
toValue,
duration: 1800,
useNativeDriver: false,
}).start();
}
function doDelete() {
fadeAnim.setValue(0);
onDelete();
}
useEffect(() => {
if (isPressed) {
animateColor(1); // Start the color change animation
const timer = setTimeout(doDelete, 1100); // Call onDelete after 3 seconds
// Cleanup function to cancel the onDelete call and color animation
return () => {
clearTimeout(timer);
animateColor(0);
};
} else {
animateColor(0); // Reset the color
}
}, [isPressed]);
const backgroundColor = fadeAnim.interpolate({
inputRange: [0, 1],
outputRange: ['rgb(255, 255, 255)', 'rgb(255, 0, 0)']
});
return (
<AnimatedPressable
delayLongPress={1100}
onPressIn={() => setIsPressed(true)}
onPressOut={() => setIsPressed(false)}
style={[styles.button, { backgroundColor }]}
>
<Text>{item.content}</Text>
</AnimatedPressable>
);
};
const styles = StyleSheet.create({
button: {
padding: 5,
borderRadius: 20,
}
});
export default TextNoteButton;