@hoddy-ui/core 2.5.26 → 2.5.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Components/FlashMessage.tsx +53 -29
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useRef, useState } from "react";
|
|
1
|
+
import React, { useEffect, useRef, useState } from "react";
|
|
2
2
|
import { TouchableOpacity, View } from "react-native";
|
|
3
3
|
import Animated, {
|
|
4
4
|
runOnJS,
|
|
@@ -12,7 +12,20 @@ import { useColors } from "../hooks";
|
|
|
12
12
|
import { FlashMessageProps } from "../types";
|
|
13
13
|
import Typography from "./Typography";
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
// Event-based API to decouple the trigger from the component instance
|
|
16
|
+
type FlashListener = (msg: FlashMessageProps) => void;
|
|
17
|
+
const flashListeners = new Set<FlashListener>();
|
|
18
|
+
|
|
19
|
+
export const showFlashMessage = (msg: FlashMessageProps) => {
|
|
20
|
+
flashListeners.forEach((listener) => listener(msg));
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const subscribeToFlashMessages = (listener: FlashListener) => {
|
|
24
|
+
flashListeners.add(listener);
|
|
25
|
+
return () => {
|
|
26
|
+
flashListeners.delete(listener);
|
|
27
|
+
};
|
|
28
|
+
};
|
|
16
29
|
|
|
17
30
|
const FlashMessage: React.FC = () => {
|
|
18
31
|
const { top } = useSafeAreaInsets();
|
|
@@ -43,33 +56,44 @@ const FlashMessage: React.FC = () => {
|
|
|
43
56
|
});
|
|
44
57
|
};
|
|
45
58
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
const listener: FlashListener = (msg) => {
|
|
61
|
+
// Clear existing timeout if any
|
|
62
|
+
if (timeoutRef.current) {
|
|
63
|
+
clearTimeout(timeoutRef.current);
|
|
64
|
+
timeoutRef.current = null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Reset position immediately before starting new animation
|
|
68
|
+
translateY.value = -200;
|
|
69
|
+
opacity.value = 0;
|
|
70
|
+
|
|
71
|
+
setMessage(msg);
|
|
72
|
+
|
|
73
|
+
// Animate in
|
|
74
|
+
translateY.value = withTiming(0, { duration: 300 });
|
|
75
|
+
opacity.value = withTiming(1, { duration: 300 });
|
|
76
|
+
|
|
77
|
+
// Animate out after duration
|
|
78
|
+
const duration = msg.duration || 3000;
|
|
79
|
+
timeoutRef.current = setTimeout(() => {
|
|
80
|
+
translateY.value = withTiming(-200, { duration: 300 });
|
|
81
|
+
opacity.value = withTiming(0, { duration: 300 }, () => {
|
|
82
|
+
runOnJS(hideMessage)();
|
|
83
|
+
});
|
|
84
|
+
timeoutRef.current = null;
|
|
85
|
+
}, duration);
|
|
86
|
+
};
|
|
62
87
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
};
|
|
88
|
+
const unsubscribe = subscribeToFlashMessages(listener);
|
|
89
|
+
return () => {
|
|
90
|
+
if (timeoutRef.current) {
|
|
91
|
+
clearTimeout(timeoutRef.current);
|
|
92
|
+
timeoutRef.current = null;
|
|
93
|
+
}
|
|
94
|
+
unsubscribe();
|
|
95
|
+
};
|
|
96
|
+
}, [message, opacity, translateY]);
|
|
73
97
|
|
|
74
98
|
const animatedStyle = useAnimatedStyle(() => {
|
|
75
99
|
return {
|
|
@@ -104,7 +128,7 @@ const FlashMessage: React.FC = () => {
|
|
|
104
128
|
},
|
|
105
129
|
});
|
|
106
130
|
|
|
107
|
-
if (!message) return null;
|
|
131
|
+
// if (!message) return null;
|
|
108
132
|
|
|
109
133
|
return (
|
|
110
134
|
<Animated.View style={[styles.root, animatedStyle]}>
|