@momo-kits/foundation 0.156.1-beta.3 → 0.156.1-beta.5

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.
@@ -7,6 +7,7 @@ import { ApplicationContext, MiniAppContext, ScreenContext } from '../Context';
7
7
  import { GridSystem } from '../Layout';
8
8
  import { version } from '../package.json';
9
9
  import { useAppState } from './utils';
10
+ import { TooltipPortalHost, TooltipPortalProvider } from './TooltipPortal.tsx';
10
11
 
11
12
  const runAfterInteractions = InteractionManager.runAfterInteractions;
12
13
 
@@ -407,8 +408,11 @@ const StackScreen: React.FC<any> = props => {
407
408
  },
408
409
  }}
409
410
  >
410
- <Component heightHeader={heightHeader} {...data} />
411
- {showGrid && <GridSystem />}
411
+ <TooltipPortalProvider>
412
+ <Component heightHeader={heightHeader} {...data} />
413
+ {showGrid && <GridSystem />}
414
+ <TooltipPortalHost />
415
+ </TooltipPortalProvider>
412
416
  </ScreenContext.Provider>
413
417
  );
414
418
  };
@@ -0,0 +1,123 @@
1
+ import React, {
2
+ createContext,
3
+ ReactNode,
4
+ RefObject,
5
+ useCallback,
6
+ useContext,
7
+ useReducer,
8
+ useRef,
9
+ } from 'react';
10
+ import { StyleSheet, View } from 'react-native';
11
+
12
+ interface TooltipPortalContextValue {
13
+ register: (id: string, content: ReactNode) => void;
14
+ unregister: (id: string, immediate?: boolean) => void;
15
+ portals: Map<string, ReactNode>;
16
+ hostRef: RefObject<View | null>;
17
+ }
18
+
19
+ const TooltipPortalContext = createContext<TooltipPortalContextValue | null>(
20
+ null,
21
+ );
22
+
23
+ export const TooltipPortalProvider: React.FC<{ children: ReactNode }> = ({
24
+ children,
25
+ }) => {
26
+ // Use ref for synchronous updates + reducer for manual re-renders
27
+ const portalsRef = useRef<Map<string, ReactNode>>(new Map());
28
+ const hostRef = useRef<View>(null);
29
+ const [updateCounter, forceUpdate] = useReducer((x: number) => x + 1, 0);
30
+ const pendingUpdateRef = useRef<any>(null);
31
+
32
+ const register = useCallback((id: string, content: ReactNode) => {
33
+ portalsRef.current.set(id, content);
34
+ forceUpdate(); // Trigger re-render to show new content
35
+ }, []);
36
+
37
+ const unregister = useCallback((id: string, immediate = false) => {
38
+ const hasItem = portalsRef.current.has(id);
39
+ if (!hasItem) return; // Already removed
40
+
41
+ portalsRef.current.delete(id);
42
+
43
+ // Clear any pending updates
44
+ if (pendingUpdateRef.current) {
45
+ clearTimeout(pendingUpdateRef.current);
46
+ pendingUpdateRef.current = null;
47
+ }
48
+
49
+ if (immediate) {
50
+ forceUpdate();
51
+ } else {
52
+ // Debounce normal updates to avoid excessive re-renders
53
+ pendingUpdateRef.current = setTimeout(() => {
54
+ forceUpdate();
55
+ pendingUpdateRef.current = null;
56
+ }, 0);
57
+ }
58
+ }, []);
59
+
60
+ console.log('updateCounter', updateCounter);
61
+
62
+ return (
63
+ <TooltipPortalContext.Provider
64
+ value={{ register, unregister, portals: portalsRef.current, hostRef }}
65
+ >
66
+ {children}
67
+ </TooltipPortalContext.Provider>
68
+ );
69
+ };
70
+
71
+ export const TooltipPortalHost: React.FC = () => {
72
+ const context = useContext(TooltipPortalContext);
73
+
74
+ if (!context) {
75
+ if (__DEV__) {
76
+ console.warn(
77
+ 'TooltipPortalHost must be used within TooltipPortalProvider',
78
+ );
79
+ }
80
+ return null;
81
+ }
82
+
83
+ const { portals, hostRef } = context;
84
+
85
+ return (
86
+ <View ref={hostRef} style={styles.hostContainer} pointerEvents="box-none">
87
+ {Array.from(portals.entries()).map(([id, content]) => (
88
+ <React.Fragment key={id}>{content}</React.Fragment>
89
+ ))}
90
+ </View>
91
+ );
92
+ };
93
+
94
+ export const useTooltipPortal = () => {
95
+ const context = useContext(TooltipPortalContext);
96
+
97
+ if (!context) {
98
+ if (__DEV__) {
99
+ console.warn(
100
+ 'useTooltipPortal must be used within TooltipPortalProvider. Tooltips may not appear correctly.',
101
+ );
102
+ }
103
+ // Return no-op functions if context is missing
104
+ return {
105
+ register: () => {},
106
+ unregister: () => {},
107
+ hostRef: null,
108
+ };
109
+ }
110
+
111
+ return context;
112
+ };
113
+
114
+ const styles = StyleSheet.create({
115
+ hostContainer: {
116
+ position: 'absolute',
117
+ top: 0,
118
+ left: 0,
119
+ right: 0,
120
+ bottom: 0,
121
+ zIndex: 9999,
122
+ },
123
+ });
@@ -17,6 +17,7 @@ import { exportHeaderTitle, setAutomationID, useComponentId } from './utils';
17
17
  import Navigation from './Navigation';
18
18
  import Navigator from './Navigator';
19
19
  import ScaleSizeProvider from './ScaleSizeProvider';
20
+ import { useTooltipPortal } from './TooltipPortal';
20
21
 
21
22
  export {
22
23
  NavigationContainer,
@@ -29,4 +30,5 @@ export {
29
30
  Navigator,
30
31
  exportHeaderTitle,
31
32
  ScaleSizeProvider,
33
+ useTooltipPortal,
32
34
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/foundation",
3
- "version": "0.156.1-beta.3",
3
+ "version": "0.156.1-beta.5",
4
4
  "description": "React Native Component Kits",
5
5
  "main": "index.ts",
6
6
  "scripts": {},