hybrid-navigation 5.0.0 → 5.1.0
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/android/src/main/java/com/navigation/androidx/AwesomeActivity.java +2 -4
- package/android/src/main/java/com/navigation/androidx/AwesomeFragment.java +48 -2
- package/android/src/main/java/com/navigation/androidx/DefaultTabBarProvider.java +2 -2
- package/android/src/main/java/com/navigation/androidx/FragmentHelper.java +7 -0
- package/android/src/main/java/com/navigation/androidx/PresentationDelegate.java +2 -4
- package/android/src/main/java/com/navigation/androidx/StackFragment.java +3 -0
- package/android/src/main/java/com/navigation/androidx/TabBarFragment.java +50 -0
- package/android/src/main/java/com/navigation/androidx/TabBarItem.java +4 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/DispatchResult.java +16 -4
- package/android/src/main/java/com/reactnative/hybridnavigation/GlobalStyle.java +19 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/HBDRootView.java +17 -2
- package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +66 -7
- package/android/src/main/java/com/reactnative/hybridnavigation/NativeNavigation.java +116 -18
- package/android/src/main/java/com/reactnative/hybridnavigation/NavigationCommandQueue.java +8 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +376 -8
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactManager.java +72 -2
- package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +26 -0
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +134 -12
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +35 -3
- package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +29 -4
- package/dist/NativeEvent.d.ts +15 -0
- package/dist/NativeNavigation.d.ts +8 -0
- package/dist/Navigation.d.ts +14 -1
- package/dist/Navigation.js +153 -12
- package/dist/NavigationComponentWrap.d.ts +3 -2
- package/dist/Navigator.d.ts +3 -2
- package/dist/Navigator.js +28 -9
- package/dist/Options.d.ts +12 -2
- package/dist/Route.d.ts +70 -5
- package/dist/defineRoutes.d.ts +19 -0
- package/dist/defineRoutes.js +6 -0
- package/dist/handler/DispatchCommandHandler.d.ts +16 -1
- package/dist/handler/DispatchCommandHandler.js +63 -5
- package/dist/handler/LayoutCommandHandler.d.ts +1 -0
- package/dist/handler/LayoutCommandHandler.js +3 -0
- package/dist/handler/LeaveGuardHandler.d.ts +17 -0
- package/dist/handler/LeaveGuardHandler.js +350 -0
- package/dist/handler/LifecycleEventHandler.d.ts +7 -3
- package/dist/handler/LifecycleEventHandler.js +36 -3
- package/dist/handler/PresentationCoordinator.d.ts +24 -0
- package/dist/handler/PresentationCoordinator.js +449 -0
- package/dist/handler/ResultEventHandler.d.ts +7 -0
- package/dist/handler/ResultEventHandler.js +13 -5
- package/dist/handler/TabEventHandler.d.ts +8 -0
- package/dist/handler/TabEventHandler.js +25 -0
- package/dist/hooks.d.ts +3 -1
- package/dist/hooks.js +20 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +3 -0
- package/dist/reveal.d.ts +46 -0
- package/dist/reveal.js +431 -0
- package/dist/routeGraph.d.ts +12 -0
- package/dist/routeGraph.js +68 -0
- package/dist/router/index.js +5 -41
- package/dist/router/tabs.d.ts +1 -0
- package/dist/store.d.ts +2 -0
- package/dist/store.js +4 -0
- package/dist/testing.d.ts +79 -0
- package/dist/testing.js +251 -0
- package/ios/HBDGlobalStyle.h +3 -0
- package/ios/HBDGlobalStyle.mm +13 -0
- package/ios/HybridNavigation.h +1 -0
- package/ios/Navigator/HBDScreenNavigator.mm +177 -11
- package/ios/Navigator/HBDStackNavigator.mm +50 -7
- package/ios/Navigator/HBDTabNavigator.mm +33 -5
- package/ios/React/HBDNativeNavigation.mm +73 -12
- package/ios/React/HBDNavigator.h +8 -0
- package/ios/React/HBDOrientationCoordinator.h +2 -0
- package/ios/React/HBDOrientationCoordinator.mm +27 -0
- package/ios/React/HBDReactBridgeManager.h +10 -0
- package/ios/React/HBDReactBridgeManager.mm +48 -2
- package/ios/ViewController/HBDNavigationController.mm +14 -0
- package/ios/ViewController/HBDReactViewController.h +6 -0
- package/ios/ViewController/HBDReactViewController.mm +177 -1
- package/ios/ViewController/HBDTabBarController.h +11 -0
- package/ios/ViewController/HBDTabBarController.mm +170 -3
- package/ios/ViewController/HBDTabBarRenderer.h +28 -0
- package/ios/ViewController/HBDViewController.h +1 -0
- package/ios/ViewController/HBDViewController.mm +57 -9
- package/ios/ViewController/UIViewController+HBD.h +1 -0
- package/ios/ViewController/UIViewController+HBD.mm +13 -4
- package/package.json +3 -1
- package/src/NativeEvent.ts +17 -0
- package/src/NativeNavigation.ts +9 -0
- package/src/Navigation.ts +228 -12
- package/src/NavigationComponentWrap.tsx +11 -2
- package/src/Navigator.ts +38 -9
- package/src/Options.ts +9 -2
- package/src/Route.ts +102 -6
- package/src/defineRoutes.ts +46 -0
- package/src/handler/DispatchCommandHandler.ts +110 -11
- package/src/handler/LayoutCommandHandler.ts +4 -0
- package/src/handler/LeaveGuardHandler.ts +436 -0
- package/src/handler/LifecycleEventHandler.ts +85 -38
- package/src/handler/PresentationCoordinator.ts +560 -0
- package/src/handler/ResultEventHandler.ts +23 -9
- package/src/handler/TabEventHandler.ts +31 -0
- package/src/hooks.ts +37 -1
- package/src/index.ts +11 -0
- package/src/reveal.ts +565 -0
- package/src/routeGraph.ts +87 -0
- package/src/router/index.ts +5 -40
- package/src/router/tabs.ts +1 -0
- package/src/store.ts +5 -0
- package/src/testing.ts +395 -0
- package/testing.d.ts +1 -0
- package/testing.js +3 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hybrid-navigation",
|
|
3
3
|
"description": "React Native Navigation that supports seamless navigation between Native and React.",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.1.0",
|
|
5
5
|
"main": "./dist/index",
|
|
6
6
|
"module": "./dist/index",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
"files": [
|
|
12
12
|
"src",
|
|
13
13
|
"dist",
|
|
14
|
+
"testing.js",
|
|
15
|
+
"testing.d.ts",
|
|
14
16
|
"android",
|
|
15
17
|
"ios",
|
|
16
18
|
"HybridNavigation.podspec",
|
package/src/NativeEvent.ts
CHANGED
|
@@ -5,6 +5,9 @@ export type SceneStateChangeEvent = {
|
|
|
5
5
|
sceneId: string;
|
|
6
6
|
state: string;
|
|
7
7
|
prevState: string;
|
|
8
|
+
moduleName?: string;
|
|
9
|
+
mode?: string;
|
|
10
|
+
presentingId?: string;
|
|
8
11
|
};
|
|
9
12
|
|
|
10
13
|
export type ResultEvent = {
|
|
@@ -14,11 +17,25 @@ export type ResultEvent = {
|
|
|
14
17
|
resultData: {};
|
|
15
18
|
};
|
|
16
19
|
|
|
20
|
+
export type BeforeRemoveRequestEvent = {
|
|
21
|
+
sceneId: string;
|
|
22
|
+
action: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type TabPressEventWire = {
|
|
26
|
+
index: number;
|
|
27
|
+
tabKey?: string;
|
|
28
|
+
previousIndex: number;
|
|
29
|
+
reselected: boolean;
|
|
30
|
+
};
|
|
31
|
+
|
|
17
32
|
export interface Spec extends TurboModule {
|
|
18
33
|
readonly onResult: CodegenTypes.EventEmitter<ResultEvent>;
|
|
19
34
|
readonly willSetRoot: CodegenTypes.EventEmitter<void>;
|
|
20
35
|
readonly didSetRoot: CodegenTypes.EventEmitter<void>;
|
|
21
36
|
readonly onSceneStateChange: CodegenTypes.EventEmitter<SceneStateChangeEvent>;
|
|
37
|
+
readonly onBeforeRemoveRequest: CodegenTypes.EventEmitter<BeforeRemoveRequestEvent>;
|
|
38
|
+
readonly onTabPress: CodegenTypes.EventEmitter<TabPressEventWire>;
|
|
22
39
|
}
|
|
23
40
|
|
|
24
41
|
export default TurboModuleRegistry.getEnforcing<Spec>('HBDNativeEvent');
|
package/src/NativeNavigation.ts
CHANGED
|
@@ -7,6 +7,7 @@ export interface DispatchResultWire {
|
|
|
7
7
|
ok: boolean;
|
|
8
8
|
code: string;
|
|
9
9
|
message?: string;
|
|
10
|
+
sceneId?: string;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export interface Route {
|
|
@@ -24,6 +25,12 @@ export interface RouteGraph {
|
|
|
24
25
|
children?: RouteGraph[];
|
|
25
26
|
}
|
|
26
27
|
|
|
28
|
+
export interface NavigationSnapshot {
|
|
29
|
+
rootGeneration: number;
|
|
30
|
+
currentRoute: Route | null;
|
|
31
|
+
layers: RouteGraph[];
|
|
32
|
+
}
|
|
33
|
+
|
|
27
34
|
export interface Spec extends TurboModule {
|
|
28
35
|
getConstants: () => {
|
|
29
36
|
RESULT_OK: number;
|
|
@@ -51,9 +58,11 @@ export interface Spec extends TurboModule {
|
|
|
51
58
|
findSceneIdByModuleName: (moduleName: string) => Promise<string | null>;
|
|
52
59
|
currentRoute: () => Promise<Route | null>;
|
|
53
60
|
routeGraph: () => Promise<RouteGraph[]>;
|
|
61
|
+
navigationSnapshot: () => Promise<NavigationSnapshot>;
|
|
54
62
|
|
|
55
63
|
setDefaultOptions: (options: CodegenTypes.UnsafeObject) => void;
|
|
56
64
|
updateOptions: (sceneId: string, options: CodegenTypes.UnsafeObject) => void;
|
|
65
|
+
setBeforeRemoveEnabled: (sceneId: string, enabled: boolean) => void;
|
|
57
66
|
updateTabBar: (sceneId: string, item: CodegenTypes.UnsafeObject) => void;
|
|
58
67
|
setTabItem: (sceneId: string, items: Array<CodegenTypes.UnsafeObject>) => void;
|
|
59
68
|
}
|
package/src/Navigation.ts
CHANGED
|
@@ -3,21 +3,33 @@ import { AppRegistry, ComponentProvider } from 'react-native';
|
|
|
3
3
|
|
|
4
4
|
import type { DefaultOptions, NavigationOption, TabBarStyle, TabItemInfo } from './Options';
|
|
5
5
|
import type {
|
|
6
|
+
BeforeRemoveListener,
|
|
7
|
+
BeforeRemoveOptions,
|
|
6
8
|
BuildInLayout,
|
|
9
|
+
CloseSelfOptions,
|
|
7
10
|
DispatchResult,
|
|
8
11
|
Layout,
|
|
9
12
|
NavigationResult,
|
|
13
|
+
NavigationSnapshot,
|
|
10
14
|
ResultMap,
|
|
11
15
|
Route,
|
|
12
16
|
RouteConfig,
|
|
13
17
|
RouteGraph,
|
|
14
18
|
RouteName,
|
|
19
|
+
SceneExitHandler,
|
|
20
|
+
SceneInfo,
|
|
15
21
|
SceneLifecycleState,
|
|
16
22
|
SetRootOptions,
|
|
23
|
+
TabPressListener,
|
|
17
24
|
} from './Route';
|
|
18
25
|
|
|
19
26
|
import { layoutHandler, RootLayoutListener } from './handler/LayoutCommandHandler';
|
|
20
27
|
import { dispatchHandler } from './handler/DispatchCommandHandler';
|
|
28
|
+
import { leaveGuardHandler } from './handler/LeaveGuardHandler';
|
|
29
|
+
import { presentationCoordinator } from './handler/PresentationCoordinator';
|
|
30
|
+
import { tabEventHandler } from './handler/TabEventHandler';
|
|
31
|
+
import { executeReveal, type RevealOptions, type RevealResult, type RevealTarget } from './reveal';
|
|
32
|
+
import type { RouteDefinition } from './defineRoutes';
|
|
21
33
|
import {
|
|
22
34
|
RESULT_BLOCK,
|
|
23
35
|
RESULT_CANCEL,
|
|
@@ -38,11 +50,15 @@ import { withNavigation } from './NavigationComponentWrap';
|
|
|
38
50
|
import {
|
|
39
51
|
bumpRouteConfigVersion,
|
|
40
52
|
clearRouteConfigs,
|
|
53
|
+
getRouteConfigs,
|
|
41
54
|
setRouteConfig,
|
|
42
55
|
} from './routeConfigRegistry';
|
|
43
56
|
|
|
44
57
|
export type { ResultType } from './handler/ResultEventHandler';
|
|
45
|
-
import type {
|
|
58
|
+
import type {
|
|
59
|
+
DispatchParams,
|
|
60
|
+
NavigationInterceptor,
|
|
61
|
+
} from './handler/DispatchCommandHandler';
|
|
46
62
|
export type {
|
|
47
63
|
DispatchParams,
|
|
48
64
|
InterceptParams,
|
|
@@ -72,10 +88,13 @@ export interface Navigation {
|
|
|
72
88
|
getComponentFunc: ComponentProvider,
|
|
73
89
|
routeConfig?: RouteConfig,
|
|
74
90
|
): void;
|
|
91
|
+
registerRoutes(routes: readonly RouteDefinition<any, any, any>[]): void;
|
|
92
|
+
preloadRoute(moduleName: RouteName): void;
|
|
75
93
|
|
|
76
94
|
addLifecycleListener(sceneId: string, listener: SceneLifecycleListener): NavigationSubscription;
|
|
77
95
|
addGlobalLifecycleListener(listener: GlobalSceneLifecycleListener): NavigationSubscription;
|
|
78
96
|
getSceneState(sceneId: string): SceneLifecycleState;
|
|
97
|
+
getSceneInfo(sceneId: string): SceneInfo | undefined;
|
|
79
98
|
addGlobalVisibilityEventListener(
|
|
80
99
|
listener: GlobalVisibilityEventListener,
|
|
81
100
|
): NavigationSubscription;
|
|
@@ -83,9 +102,24 @@ export interface Navigation {
|
|
|
83
102
|
sceneId: string,
|
|
84
103
|
listener: VisibilityEventListener,
|
|
85
104
|
): NavigationSubscription;
|
|
105
|
+
addTabPressListener(listener: TabPressListener): NavigationSubscription;
|
|
86
106
|
|
|
87
107
|
dispatch(sceneId: string, action: string, params?: DispatchParams): Promise<DispatchResult>;
|
|
88
108
|
addInterceptor(interceptor: NavigationInterceptor): NavigationSubscription;
|
|
109
|
+
registerSceneExitHandler(
|
|
110
|
+
sceneId: string,
|
|
111
|
+
handler: SceneExitHandler,
|
|
112
|
+
): NavigationSubscription;
|
|
113
|
+
addBeforeRemoveListener(
|
|
114
|
+
sceneId: string,
|
|
115
|
+
listener: BeforeRemoveListener,
|
|
116
|
+
options?: BeforeRemoveOptions,
|
|
117
|
+
): NavigationSubscription;
|
|
118
|
+
closeScene<T extends ResultMap = ResultMap>(
|
|
119
|
+
sceneId: string,
|
|
120
|
+
options?: CloseSelfOptions<T>,
|
|
121
|
+
): Promise<DispatchResult>;
|
|
122
|
+
reveal(target: RevealTarget, options?: RevealOptions): Promise<RevealResult>;
|
|
89
123
|
setResult<T extends ResultMap = ResultMap>(
|
|
90
124
|
sceneId: string,
|
|
91
125
|
resultCode: number,
|
|
@@ -103,32 +137,55 @@ export interface Navigation {
|
|
|
103
137
|
isStackRoot(sceneId: string): Promise<boolean>;
|
|
104
138
|
currentRoute(): Promise<Route | null>;
|
|
105
139
|
routeGraph(): Promise<RouteGraph[]>;
|
|
140
|
+
navigationSnapshot(): Promise<NavigationSnapshot>;
|
|
106
141
|
|
|
107
142
|
setDefaultOptions(options: DefaultOptions): void;
|
|
108
143
|
updateOptions(sceneId: string, options: NavigationOption): void;
|
|
144
|
+
updateTabBar(options: TabBarStyle): void;
|
|
109
145
|
updateTabBar(sceneId: string, options: TabBarStyle): void;
|
|
146
|
+
setTabItem(item: TabItemInfo | TabItemInfo[]): void;
|
|
110
147
|
setTabItem(sceneId: string, item: TabItemInfo | TabItemInfo[]): void;
|
|
111
148
|
setMenuInteractive(sceneId: string, enabled: boolean): Promise<DispatchResult>;
|
|
112
149
|
}
|
|
113
150
|
|
|
114
151
|
class NavigationImpl implements Navigation {
|
|
115
152
|
constructor() {
|
|
153
|
+
leaveGuardHandler.setDispatcher((sceneId, action, params) =>
|
|
154
|
+
dispatchHandler.dispatch(sceneId, action, params),
|
|
155
|
+
);
|
|
156
|
+
dispatchHandler.addMiddleware(leaveGuardHandler.middleware);
|
|
157
|
+
dispatchHandler.addMiddleware(dispatchHandler.interceptorMiddleware);
|
|
158
|
+
dispatchHandler.addMiddleware(presentationCoordinator.middleware);
|
|
116
159
|
layoutHandler.handleRootLayoutChange(() => {
|
|
160
|
+
lifecycleHandler.onRootReset();
|
|
161
|
+
leaveGuardHandler.onRootReset();
|
|
162
|
+
presentationCoordinator.onRootReset();
|
|
117
163
|
resultHandler.invalidateAll('root_reset');
|
|
118
164
|
});
|
|
119
165
|
lifecycleHandler.handleSceneLifecycle(sceneId => {
|
|
166
|
+
leaveGuardHandler.onSceneDestroyed(sceneId);
|
|
167
|
+
presentationCoordinator.onSceneDestroyed(sceneId);
|
|
120
168
|
resultHandler.invalidateResultEventListener(sceneId, 'unmounted');
|
|
121
169
|
store.removeNavigator(sceneId);
|
|
122
170
|
});
|
|
123
171
|
resultHandler.handleComponentResult();
|
|
172
|
+
leaveGuardHandler.handleNativeBeforeRemoveRequests();
|
|
173
|
+
tabEventHandler.handleTabPress();
|
|
124
174
|
}
|
|
125
175
|
|
|
126
176
|
private hoc?: HOC;
|
|
127
177
|
private registerEnded = false;
|
|
178
|
+
private registeredNames = new Set<string>();
|
|
179
|
+
private registeredPaths = new Map<string, string>();
|
|
180
|
+
private routeLoaders = new Map<string, () => React.ComponentType<any>>();
|
|
181
|
+
private revealQueue: Promise<unknown> = Promise.resolve();
|
|
128
182
|
|
|
129
183
|
startRegisterComponent(hoc?: HOC) {
|
|
130
184
|
this.hoc = hoc;
|
|
131
185
|
this.registerEnded = false;
|
|
186
|
+
this.registeredNames.clear();
|
|
187
|
+
this.registeredPaths.clear();
|
|
188
|
+
this.routeLoaders.clear();
|
|
132
189
|
clearRouteConfigs();
|
|
133
190
|
NativeNavigation.startRegisterReactComponent();
|
|
134
191
|
}
|
|
@@ -139,25 +196,78 @@ class NavigationImpl implements Navigation {
|
|
|
139
196
|
return;
|
|
140
197
|
}
|
|
141
198
|
this.registerEnded = true;
|
|
199
|
+
if (__DEV__) {
|
|
200
|
+
this.validateRouteDependencies();
|
|
201
|
+
}
|
|
142
202
|
bumpRouteConfigVersion();
|
|
143
203
|
NativeNavigation.endRegisterReactComponent();
|
|
144
204
|
}
|
|
145
205
|
|
|
206
|
+
private validateRouteDependencies() {
|
|
207
|
+
const configs = getRouteConfigs();
|
|
208
|
+
const state = new Map<string, 'visiting' | 'visited'>();
|
|
209
|
+
const stack: string[] = [];
|
|
210
|
+
|
|
211
|
+
const visit = (name: string) => {
|
|
212
|
+
state.set(name, 'visiting');
|
|
213
|
+
stack.push(name);
|
|
214
|
+
const dependency = configs.get(name)?.dependency;
|
|
215
|
+
if (dependency) {
|
|
216
|
+
const depState = state.get(dependency);
|
|
217
|
+
if (depState === 'visiting') {
|
|
218
|
+
const cycleStart = stack.indexOf(dependency);
|
|
219
|
+
const cycle = [...stack.slice(cycleStart), dependency].join(' -> ');
|
|
220
|
+
console.warn(`[hybrid-navigation] Circular route dependency detected: ${cycle}`);
|
|
221
|
+
} else if (!depState && configs.has(dependency)) {
|
|
222
|
+
visit(dependency);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
stack.pop();
|
|
226
|
+
state.set(name, 'visited');
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
for (const name of configs.keys()) {
|
|
230
|
+
if (!state.has(name)) {
|
|
231
|
+
visit(name);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
146
236
|
registerComponent<Name extends RouteName = RouteName>(
|
|
147
237
|
appKey: Name,
|
|
148
238
|
getComponentFunc: ComponentProvider,
|
|
149
239
|
routeConfig?: RouteConfig,
|
|
150
240
|
) {
|
|
241
|
+
if (__DEV__) {
|
|
242
|
+
if (this.registeredNames.has(appKey)) {
|
|
243
|
+
console.warn(
|
|
244
|
+
`[hybrid-navigation] Duplicate route registration for "${appKey}". Later registration overrides earlier one.`,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
if (routeConfig?.path) {
|
|
248
|
+
const existing = this.registeredPaths.get(routeConfig.path);
|
|
249
|
+
if (existing && existing !== appKey) {
|
|
250
|
+
console.warn(
|
|
251
|
+
`[hybrid-navigation] Duplicate route path "${routeConfig.path}" registered by "${appKey}" and "${existing}".`,
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
this.registeredPaths.set(routeConfig.path, appKey);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
this.registeredNames.add(appKey);
|
|
258
|
+
|
|
151
259
|
if (routeConfig && (routeConfig.path || routeConfig.dependency || routeConfig.mode)) {
|
|
152
260
|
setRouteConfig(appKey, routeConfig);
|
|
153
261
|
}
|
|
154
262
|
|
|
155
263
|
const hoc = this.hoc;
|
|
156
264
|
|
|
157
|
-
|
|
158
|
-
|
|
265
|
+
const isLazy = routeConfig?.lazy === true;
|
|
266
|
+
|
|
267
|
+
if (isLazy) {
|
|
268
|
+
NativeNavigation.registerReactComponent(appKey, routeConfig?.options ?? {});
|
|
159
269
|
let CachedRootComponent: React.ComponentType<any> | undefined;
|
|
160
|
-
|
|
270
|
+
const loader = () => {
|
|
161
271
|
if (!CachedRootComponent) {
|
|
162
272
|
let WrappedComponent = getComponentFunc();
|
|
163
273
|
if (__DEV__ && (WrappedComponent as any)?.navigationItem) {
|
|
@@ -171,22 +281,57 @@ class NavigationImpl implements Navigation {
|
|
|
171
281
|
CachedRootComponent = withNavigation(appKey)(WrappedComponent);
|
|
172
282
|
}
|
|
173
283
|
return CachedRootComponent;
|
|
174
|
-
}
|
|
284
|
+
};
|
|
285
|
+
this.routeLoaders.set(appKey, loader);
|
|
286
|
+
AppRegistry.registerComponent(appKey, loader);
|
|
175
287
|
return;
|
|
176
288
|
}
|
|
177
289
|
|
|
178
|
-
|
|
290
|
+
const RawComponent = getComponentFunc();
|
|
291
|
+
let WrappedComponent = RawComponent;
|
|
179
292
|
if (hoc) {
|
|
180
293
|
WrappedComponent = hoc(WrappedComponent);
|
|
181
294
|
}
|
|
182
295
|
|
|
183
|
-
const options: object =
|
|
296
|
+
const options: object =
|
|
297
|
+
routeConfig?.options ??
|
|
298
|
+
(RawComponent as any)?.navigationItem ??
|
|
299
|
+
(WrappedComponent as any)?.navigationItem ??
|
|
300
|
+
{};
|
|
184
301
|
NativeNavigation.registerReactComponent(appKey, options);
|
|
185
302
|
|
|
186
303
|
const RootComponent = withNavigation(appKey)(WrappedComponent);
|
|
187
304
|
AppRegistry.registerComponent(appKey, () => RootComponent);
|
|
188
305
|
}
|
|
189
306
|
|
|
307
|
+
registerRoutes(routes: readonly RouteDefinition<any, any, any>[]) {
|
|
308
|
+
for (const route of routes) {
|
|
309
|
+
const hasConfig =
|
|
310
|
+
route.path !== undefined ||
|
|
311
|
+
route.dependency !== undefined ||
|
|
312
|
+
route.mode !== undefined ||
|
|
313
|
+
route.options !== undefined ||
|
|
314
|
+
route.lazy !== undefined;
|
|
315
|
+
const config: RouteConfig | undefined = hasConfig
|
|
316
|
+
? {
|
|
317
|
+
path: route.path,
|
|
318
|
+
dependency: route.dependency as RouteName | undefined,
|
|
319
|
+
mode: route.mode,
|
|
320
|
+
options: route.options,
|
|
321
|
+
lazy: route.lazy,
|
|
322
|
+
}
|
|
323
|
+
: undefined;
|
|
324
|
+
this.registerComponent(route.name as RouteName, route.load, config);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
preloadRoute(moduleName: RouteName) {
|
|
329
|
+
const loader = this.routeLoaders.get(moduleName);
|
|
330
|
+
if (loader) {
|
|
331
|
+
loader();
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
190
335
|
addLifecycleListener(
|
|
191
336
|
sceneId: string,
|
|
192
337
|
listener: SceneLifecycleListener,
|
|
@@ -208,6 +353,10 @@ class NavigationImpl implements Navigation {
|
|
|
208
353
|
return lifecycleHandler.getSceneState(sceneId);
|
|
209
354
|
}
|
|
210
355
|
|
|
356
|
+
getSceneInfo(sceneId: string): SceneInfo | undefined {
|
|
357
|
+
return lifecycleHandler.getSceneInfo(sceneId);
|
|
358
|
+
}
|
|
359
|
+
|
|
211
360
|
addGlobalVisibilityEventListener(
|
|
212
361
|
listener: GlobalVisibilityEventListener,
|
|
213
362
|
): NavigationSubscription {
|
|
@@ -227,6 +376,11 @@ class NavigationImpl implements Navigation {
|
|
|
227
376
|
};
|
|
228
377
|
}
|
|
229
378
|
|
|
379
|
+
addTabPressListener(listener: TabPressListener): NavigationSubscription {
|
|
380
|
+
const remove = tabEventHandler.addListener(listener);
|
|
381
|
+
return { remove };
|
|
382
|
+
}
|
|
383
|
+
|
|
230
384
|
dispatch(sceneId: string, action: string, params: DispatchParams = {}) {
|
|
231
385
|
return dispatchHandler.dispatch(sceneId, action, params);
|
|
232
386
|
}
|
|
@@ -238,6 +392,46 @@ class NavigationImpl implements Navigation {
|
|
|
238
392
|
};
|
|
239
393
|
}
|
|
240
394
|
|
|
395
|
+
registerSceneExitHandler(
|
|
396
|
+
sceneId: string,
|
|
397
|
+
handler: SceneExitHandler,
|
|
398
|
+
): NavigationSubscription {
|
|
399
|
+
const remove = presentationCoordinator.registerExitHandler(sceneId, handler);
|
|
400
|
+
return { remove };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
addBeforeRemoveListener(
|
|
404
|
+
sceneId: string,
|
|
405
|
+
listener: BeforeRemoveListener,
|
|
406
|
+
options?: BeforeRemoveOptions,
|
|
407
|
+
): NavigationSubscription {
|
|
408
|
+
const remove = leaveGuardHandler.addListener(sceneId, listener, options);
|
|
409
|
+
return { remove };
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
closeScene<T extends ResultMap = ResultMap>(
|
|
413
|
+
sceneId: string,
|
|
414
|
+
options?: CloseSelfOptions<T>,
|
|
415
|
+
): Promise<DispatchResult> {
|
|
416
|
+
return this.dispatch(sceneId, 'close', {
|
|
417
|
+
target: 'self',
|
|
418
|
+
onCovered: options?.onCovered,
|
|
419
|
+
...(options?.result !== undefined ? { result: options.result } : {}),
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
reveal(target: RevealTarget, options?: RevealOptions): Promise<RevealResult> {
|
|
424
|
+
const task = this.revealQueue.then(
|
|
425
|
+
() => executeReveal(this, target, options),
|
|
426
|
+
() => executeReveal(this, target, options),
|
|
427
|
+
);
|
|
428
|
+
this.revealQueue = task.then(
|
|
429
|
+
() => undefined,
|
|
430
|
+
() => undefined,
|
|
431
|
+
);
|
|
432
|
+
return task;
|
|
433
|
+
}
|
|
434
|
+
|
|
241
435
|
setResult = <T extends ResultMap = ResultMap>(
|
|
242
436
|
sceneId: string,
|
|
243
437
|
resultCode: number,
|
|
@@ -282,6 +476,10 @@ class NavigationImpl implements Navigation {
|
|
|
282
476
|
return NativeNavigation.routeGraph();
|
|
283
477
|
}
|
|
284
478
|
|
|
479
|
+
navigationSnapshot(): Promise<NavigationSnapshot> {
|
|
480
|
+
return NativeNavigation.navigationSnapshot();
|
|
481
|
+
}
|
|
482
|
+
|
|
285
483
|
setDefaultOptions(options: DefaultOptions) {
|
|
286
484
|
NativeNavigation.setDefaultOptions(options);
|
|
287
485
|
}
|
|
@@ -290,13 +488,31 @@ class NavigationImpl implements Navigation {
|
|
|
290
488
|
NativeNavigation.updateOptions(sceneId, options);
|
|
291
489
|
}
|
|
292
490
|
|
|
293
|
-
updateTabBar(
|
|
294
|
-
|
|
491
|
+
updateTabBar(options: TabBarStyle): void;
|
|
492
|
+
updateTabBar(sceneId: string, options: TabBarStyle): void;
|
|
493
|
+
updateTabBar(sceneIdOrOptions: string | TabBarStyle, options?: TabBarStyle) {
|
|
494
|
+
if (typeof sceneIdOrOptions === 'string') {
|
|
495
|
+
NativeNavigation.updateTabBar(sceneIdOrOptions, options ?? {});
|
|
496
|
+
} else {
|
|
497
|
+
NativeNavigation.updateTabBar('', sceneIdOrOptions);
|
|
498
|
+
}
|
|
295
499
|
}
|
|
296
500
|
|
|
297
|
-
setTabItem(
|
|
298
|
-
|
|
299
|
-
|
|
501
|
+
setTabItem(item: TabItemInfo | TabItemInfo[]): void;
|
|
502
|
+
setTabItem(sceneId: string, item: TabItemInfo | TabItemInfo[]): void;
|
|
503
|
+
setTabItem(
|
|
504
|
+
sceneIdOrItem: string | TabItemInfo | TabItemInfo[],
|
|
505
|
+
item?: TabItemInfo | TabItemInfo[],
|
|
506
|
+
) {
|
|
507
|
+
if (typeof sceneIdOrItem === 'string') {
|
|
508
|
+
const items = Array.isArray(item) ? item : item ? [item] : [];
|
|
509
|
+
if (items.length > 0) {
|
|
510
|
+
NativeNavigation.setTabItem(sceneIdOrItem, items);
|
|
511
|
+
}
|
|
512
|
+
} else {
|
|
513
|
+
const items = Array.isArray(sceneIdOrItem) ? sceneIdOrItem : [sceneIdOrItem];
|
|
514
|
+
NativeNavigation.setTabItem('', items);
|
|
515
|
+
}
|
|
300
516
|
}
|
|
301
517
|
|
|
302
518
|
setMenuInteractive(sceneId: string, enabled: boolean) {
|
|
@@ -1,14 +1,23 @@
|
|
|
1
1
|
import React, { useEffect } from 'react';
|
|
2
2
|
import type { NavigationItem } from './Options';
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
HasTypedRoutes,
|
|
5
|
+
InitialMetrics,
|
|
6
|
+
RegisteredRouteName,
|
|
7
|
+
RouteName,
|
|
8
|
+
RoutePropsOf,
|
|
9
|
+
} from './Route';
|
|
4
10
|
import { Navigator, NavigationContext } from './Navigator';
|
|
5
11
|
import NativeNavigation from './NativeNavigation';
|
|
6
12
|
|
|
7
13
|
export type NavigationProps<Name extends RouteName = RouteName> = (HasTypedRoutes extends true
|
|
8
|
-
?
|
|
14
|
+
? [Name] extends [RegisteredRouteName]
|
|
15
|
+
? RoutePropsOf<Name>
|
|
16
|
+
: Record<string, any>
|
|
9
17
|
: Record<string, any>) & {
|
|
10
18
|
navigator: Navigator<Name>;
|
|
11
19
|
sceneId: string;
|
|
20
|
+
initialMetrics?: InitialMetrics;
|
|
12
21
|
};
|
|
13
22
|
|
|
14
23
|
interface NativeProps {
|
package/src/Navigator.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { RESULT_BLOCK, resultHandler } from './handler/ResultEventHandler';
|
|
|
8
8
|
import type { NavigationOption, TabBarStyle, TabItemInfo } from './Options';
|
|
9
9
|
import type {
|
|
10
10
|
BuildInLayout,
|
|
11
|
+
CloseSelfOptions,
|
|
11
12
|
DispatchResult,
|
|
12
13
|
Layout,
|
|
13
14
|
NavigationResult,
|
|
@@ -75,10 +76,13 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
|
|
|
75
76
|
warnOnFailure?: (code: DispatchResult['code']) => string,
|
|
76
77
|
): Promise<NavigationResult<T>> => {
|
|
77
78
|
const requestCode = --tagGenerator;
|
|
78
|
-
const { promise, cancel } = resultHandler.prepareResult<T>(
|
|
79
|
-
|
|
79
|
+
const { promise: resultPromise, cancel } = resultHandler.prepareResult<T>(
|
|
80
|
+
this.sceneId,
|
|
81
|
+
requestCode,
|
|
82
|
+
);
|
|
83
|
+
let dispatchResult: DispatchResult;
|
|
80
84
|
try {
|
|
81
|
-
|
|
85
|
+
dispatchResult = await dispatchHandler.dispatch(this.sceneId, action, {
|
|
82
86
|
...params,
|
|
83
87
|
requestCode,
|
|
84
88
|
});
|
|
@@ -86,14 +90,18 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
|
|
|
86
90
|
cancel();
|
|
87
91
|
throw error;
|
|
88
92
|
}
|
|
89
|
-
if (!
|
|
93
|
+
if (!dispatchResult.ok) {
|
|
90
94
|
cancel();
|
|
91
95
|
if (warnOnFailure) {
|
|
92
|
-
console.warn(warnOnFailure(
|
|
96
|
+
console.warn(warnOnFailure(dispatchResult.code));
|
|
93
97
|
}
|
|
94
|
-
return {
|
|
98
|
+
return {
|
|
99
|
+
status: 'blocked',
|
|
100
|
+
code: RESULT_BLOCK,
|
|
101
|
+
reason: dispatchResult.code,
|
|
102
|
+
};
|
|
95
103
|
}
|
|
96
|
-
return
|
|
104
|
+
return resultPromise;
|
|
97
105
|
};
|
|
98
106
|
|
|
99
107
|
push = async <Name extends RouteName = RouteName, T = RouteResultOf<Name>>(
|
|
@@ -184,6 +192,17 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
|
|
|
184
192
|
dismiss = (): Promise<DispatchResult> =>
|
|
185
193
|
dispatchHandler.dispatch(this.sceneId, 'dismiss', { from: this.moduleName });
|
|
186
194
|
|
|
195
|
+
closeSelf = (
|
|
196
|
+
options?: CloseSelfOptions<SetResultData<CurrentRoute>>,
|
|
197
|
+
): Promise<DispatchResult> => {
|
|
198
|
+
return dispatchHandler.dispatch(this.sceneId, 'close', {
|
|
199
|
+
from: this.moduleName,
|
|
200
|
+
target: 'self',
|
|
201
|
+
onCovered: options?.onCovered,
|
|
202
|
+
...(options?.result !== undefined ? { result: options.result } : {}),
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
|
|
187
206
|
showModal = async <Name extends RouteName = RouteName, T = RouteResultOf<Name>>(
|
|
188
207
|
moduleName: Name,
|
|
189
208
|
...args: ScreenPropsArgs<Name>
|
|
@@ -223,11 +242,21 @@ export class Navigator<CurrentRoute extends RouteName = RouteName> {
|
|
|
223
242
|
data: SetResultData<CurrentRoute> | null = null,
|
|
224
243
|
): void => resultHandler.setResult(this.sceneId, resultCode, data);
|
|
225
244
|
|
|
226
|
-
switchTab = async (
|
|
245
|
+
switchTab = async (
|
|
246
|
+
target: number | string,
|
|
247
|
+
popToRoot: boolean = false,
|
|
248
|
+
): Promise<DispatchResult> => {
|
|
227
249
|
const from = await NativeNavigation.currentTab(this.sceneId);
|
|
250
|
+
if (typeof target === 'string') {
|
|
251
|
+
return dispatchHandler.dispatch(this.sceneId, 'switchTab', {
|
|
252
|
+
from,
|
|
253
|
+
tabKey: target,
|
|
254
|
+
popToRoot,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
228
257
|
return dispatchHandler.dispatch(this.sceneId, 'switchTab', {
|
|
229
258
|
from,
|
|
230
|
-
to:
|
|
259
|
+
to: target,
|
|
231
260
|
popToRoot,
|
|
232
261
|
});
|
|
233
262
|
};
|
package/src/Options.ts
CHANGED
|
@@ -31,6 +31,7 @@ export interface NavigationItem extends NavigationOption {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
export interface TabItem {
|
|
34
|
+
key?: string;
|
|
34
35
|
title: string;
|
|
35
36
|
icon?: ImageSource;
|
|
36
37
|
unselectedIcon?: ImageSource;
|
|
@@ -43,6 +44,9 @@ export interface ShadowImage {
|
|
|
43
44
|
|
|
44
45
|
export interface DefaultOptions {
|
|
45
46
|
screenBackgroundColor?: Color; // 页面背景,默认是白色
|
|
47
|
+
modalBackgroundColor?: Color; // 弹层默认背景,默认透明
|
|
48
|
+
injectInitialMetrics?: boolean; // 是否为同向全屏场景注入首帧安全区 initialMetrics(默认 false)
|
|
49
|
+
prewarmPush?: boolean; // 是否对有动画的同向全屏主栈 push 启用离屏 Fabric 首帧预热(默认 false)
|
|
46
50
|
statusBarStyle?: BarStyle; // 状态栏样式,可选项有 `BarStyleLightContent` 和 `BarStyleDarkContent`
|
|
47
51
|
navigationBarColorAndroid?: Color; // 底部虚拟键背景颜色,仅对 Android 8.0 以上版本生效
|
|
48
52
|
displayCutoutWhenLandscapeAndroid?: boolean; // 横屏时,是否将界面延伸至刘海区域,默认 true
|
|
@@ -63,8 +67,7 @@ export type TabBarStyle = Pick<
|
|
|
63
67
|
| 'tabBarItemNormalColor'
|
|
64
68
|
>;
|
|
65
69
|
|
|
66
|
-
export interface
|
|
67
|
-
index: number;
|
|
70
|
+
export interface TabItemInfoBase {
|
|
68
71
|
title?: string;
|
|
69
72
|
badge?: {
|
|
70
73
|
text?: string;
|
|
@@ -76,3 +79,7 @@ export interface TabItemInfo {
|
|
|
76
79
|
unselected?: ImageSource;
|
|
77
80
|
};
|
|
78
81
|
}
|
|
82
|
+
|
|
83
|
+
export type TabItemInfo =
|
|
84
|
+
| (TabItemInfoBase & { index: number; key?: string })
|
|
85
|
+
| (TabItemInfoBase & { key: string; index?: number });
|