UNPKG

2.51 kBJavaScriptView Raw
1import * as React from 'react';
2import { Animated, StyleSheet } from 'react-native';
3import { TypingAnimation } from 'react-native-typing-animation';
4import { useUpdateLayoutEffect } from './hooks/useUpdateLayoutEffect';
5import Color from './Color';
6const TypingIndicator = ({ isTyping }) => {
7 const { yCoords, heightScale, marginScale } = React.useMemo(() => ({
8 yCoords: new Animated.Value(200),
9 heightScale: new Animated.Value(0),
10 marginScale: new Animated.Value(0),
11 }), []);
12 // on isTyping fire side effect
13 useUpdateLayoutEffect(() => {
14 if (isTyping) {
15 slideIn();
16 }
17 else {
18 slideOut();
19 }
20 }, [isTyping]);
21 // side effect
22 const slideIn = () => {
23 Animated.parallel([
24 Animated.spring(yCoords, {
25 toValue: 0,
26 useNativeDriver: false,
27 }),
28 Animated.timing(heightScale, {
29 toValue: 35,
30 duration: 250,
31 useNativeDriver: false,
32 }),
33 Animated.timing(marginScale, {
34 toValue: 8,
35 duration: 250,
36 useNativeDriver: false,
37 }),
38 ]).start();
39 };
40 // side effect
41 const slideOut = () => {
42 Animated.parallel([
43 Animated.spring(yCoords, {
44 toValue: 200,
45 useNativeDriver: false,
46 }),
47 Animated.timing(heightScale, {
48 toValue: 0,
49 duration: 250,
50 useNativeDriver: false,
51 }),
52 Animated.timing(marginScale, {
53 toValue: 0,
54 duration: 250,
55 useNativeDriver: false,
56 }),
57 ]).start();
58 };
59 return (<Animated.View style={[
60 styles.container,
61 {
62 transform: [
63 {
64 translateY: yCoords,
65 },
66 ],
67 height: heightScale,
68 marginBottom: marginScale,
69 },
70 ]}>
71 {isTyping ? (<TypingAnimation style={{ marginLeft: 6, marginTop: 7.2 }} dotRadius={4} dotMargin={5.5} dotColor={'rgba(0, 0, 0, 0.38)'}/>) : null}
72 </Animated.View>);
73};
74const styles = StyleSheet.create({
75 container: {
76 marginLeft: 8,
77 width: 45,
78 borderRadius: 15,
79 backgroundColor: Color.leftBubbleBackground,
80 },
81});
82export default TypingIndicator;
83//# sourceMappingURL=TypingIndicator.js.map
\No newline at end of file