@hoddy-ui/core 2.5.26 → 2.5.28

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hoddy-ui/core",
3
- "version": "2.5.26",
3
+ "version": "2.5.28",
4
4
  "description": "Core rich react native components written in typescript",
5
5
  "main": "index.ts",
6
6
  "repository": {
@@ -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
- export let showFlashMessage: (msg: FlashMessageProps) => void = () => {};
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,46 @@ const FlashMessage: React.FC = () => {
43
56
  });
44
57
  };
45
58
 
46
- showFlashMessage = (msg: FlashMessageProps) => {
47
- // Clear existing timeout if any
48
- if (timeoutRef.current) {
49
- clearTimeout(timeoutRef.current);
50
- timeoutRef.current = null;
51
- }
52
-
53
- // Reset position immediately before starting new animation
54
- translateY.value = -200;
55
- opacity.value = 0;
56
-
57
- setMessage(msg);
58
-
59
- // Animate in
60
- translateY.value = withTiming(0, { duration: 300 });
61
- opacity.value = withTiming(1, { duration: 300 });
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
+ console.log("Auto-hiding flash message after duration");
81
+
82
+ translateY.value = withTiming(-200, { duration: 300 });
83
+ opacity.value = withTiming(0, { duration: 300 }, () => {
84
+ runOnJS(hideMessage)();
85
+ });
86
+ timeoutRef.current = null;
87
+ }, duration);
88
+ };
62
89
 
63
- // Animate out after duration
64
- const duration = msg.duration || 3000;
65
- timeoutRef.current = setTimeout(() => {
66
- translateY.value = withTiming(-200, { duration: 300 });
67
- opacity.value = withTiming(0, { duration: 300 }, () => {
68
- runOnJS(hideMessage)();
69
- });
70
- timeoutRef.current = null;
71
- }, duration);
72
- };
90
+ const unsubscribe = subscribeToFlashMessages(listener);
91
+ return () => {
92
+ if (timeoutRef.current) {
93
+ clearTimeout(timeoutRef.current);
94
+ timeoutRef.current = null;
95
+ }
96
+ unsubscribe();
97
+ };
98
+ }, []);
73
99
 
74
100
  const animatedStyle = useAnimatedStyle(() => {
75
101
  return {