forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypingIndicator.tsx
More file actions
105 lines (95 loc) · 2.63 KB
/
Copy pathTypingIndicator.tsx
File metadata and controls
105 lines (95 loc) · 2.63 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React, { useEffect, useRef } from "react";
import { View, Animated, StyleSheet } from "react-native";
import type { ViewStyle } from "react-native";
/**
* Props for the TypingIndicator component.
*/
export interface TypingIndicatorProps {
/** Optional style override for the container */
style?: ViewStyle;
}
const DOT_SIZE = 6;
const DOT_SPACING = 4;
const ANIMATION_DURATION = 400;
/**
* Three animated dots that pulse in sequence, suitable for embedding
* inside an AssistantMessage to indicate the AI is still generating.
*
* Uses React Native's built-in `Animated` API (no Reanimated dependency).
*/
export function TypingIndicator({ style }: TypingIndicatorProps) {
const dot1 = useRef(new Animated.Value(0)).current;
const dot2 = useRef(new Animated.Value(0)).current;
const dot3 = useRef(new Animated.Value(0)).current;
useEffect(() => {
const createPulse = (dot: Animated.Value, delay: number) =>
Animated.sequence([
Animated.delay(delay),
Animated.loop(
Animated.sequence([
Animated.timing(dot, {
toValue: 1,
duration: ANIMATION_DURATION,
useNativeDriver: true,
}),
Animated.timing(dot, {
toValue: 0,
duration: ANIMATION_DURATION,
useNativeDriver: true,
}),
]),
),
]);
const animation = Animated.parallel([
createPulse(dot1, 0),
createPulse(dot2, ANIMATION_DURATION * 0.33),
createPulse(dot3, ANIMATION_DURATION * 0.66),
]);
animation.start();
return () => {
animation.stop();
};
}, [dot1, dot2, dot3]);
const dotStyle = (animatedValue: Animated.Value) => ({
...styles.dot,
opacity: animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [0.3, 1],
}),
transform: [
{
scale: animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [0.8, 1.2],
}),
},
],
});
return (
<View
testID="copilot-loading-cursor"
style={[styles.container, style]}
accessibilityLabel="Typing indicator"
accessibilityRole="text"
>
<Animated.View style={dotStyle(dot1)} />
<Animated.View style={dotStyle(dot2)} />
<Animated.View style={dotStyle(dot3)} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flexDirection: "row",
alignItems: "center",
paddingVertical: 4,
paddingHorizontal: 2,
},
dot: {
width: DOT_SIZE,
height: DOT_SIZE,
borderRadius: DOT_SIZE / 2,
backgroundColor: "#999999",
marginHorizontal: DOT_SPACING / 2,
},
});