hybrid-navigation 2.8.2 → 2.10.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.
Files changed (95) hide show
  1. package/HybridNavigation.podspec +6 -6
  2. package/LICENSE +1 -1
  3. package/android/.gradle/7.4/checksums/checksums.lock +0 -0
  4. package/android/.gradle/7.4/dependencies-accessors/dependencies-accessors.lock +0 -0
  5. package/android/.gradle/7.4/dependencies-accessors/gc.properties +0 -0
  6. package/android/.gradle/7.4/fileChanges/last-build.bin +0 -0
  7. package/android/.gradle/7.4/fileHashes/fileHashes.lock +0 -0
  8. package/android/.gradle/7.4/gc.properties +0 -0
  9. package/android/.gradle/checksums/checksums.lock +0 -0
  10. package/android/.gradle/configuration-cache/gc.properties +0 -0
  11. package/android/.gradle/vcs-1/gc.properties +0 -0
  12. package/android/.idea/.name +1 -0
  13. package/android/.idea/gradle.xml +16 -0
  14. package/android/.idea/misc.xml +9 -0
  15. package/android/.idea/modules/android.iml +18 -0
  16. package/android/.idea/modules.xml +8 -0
  17. package/android/.idea/vcs.xml +6 -0
  18. package/android/build.gradle +4 -4
  19. package/android/local.properties +8 -0
  20. package/android/src/main/java/com/reactnative/hybridnavigation/Garden.java +27 -40
  21. package/android/src/main/java/com/reactnative/hybridnavigation/GardenModule.java +77 -48
  22. package/android/src/main/java/com/reactnative/hybridnavigation/HBDEventEmitter.java +4 -4
  23. package/android/src/main/java/com/reactnative/hybridnavigation/HBDReactRootView.java +18 -8
  24. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +94 -43
  25. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationModule.java +134 -117
  26. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivity.java +65 -35
  27. package/android/src/main/java/com/reactnative/hybridnavigation/ReactBridgeManager.java +14 -3
  28. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +133 -89
  29. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +161 -126
  30. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +145 -114
  31. package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +40 -28
  32. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +96 -75
  33. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/Navigator.java +1 -1
  34. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +111 -79
  35. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +187 -117
  36. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +137 -96
  37. package/ios/HybridNavigation/ViewController/HBDModalViewController.m +7 -0
  38. package/ios/HybridNavigation/ViewController/HBDViewController+Garden.m +16 -0
  39. package/ios/HybridNavigation.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  40. package/ios/HybridNavigation.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  41. package/ios/HybridNavigation.xcodeproj/project.xcworkspace/xcuserdata/listen.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  42. package/ios/HybridNavigation.xcodeproj/xcuserdata/listen.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
  43. package/lib/Navigator.d.ts +6 -6
  44. package/lib/ReactRegistry.d.ts +5 -3
  45. package/lib/ReactRegistry.js +14 -10
  46. package/lib/typing.d.ts +8 -8
  47. package/package.json +24 -16
  48. package/src/DeepLink.ts +60 -0
  49. package/src/Garden.ts +171 -0
  50. package/src/NavigationModule.ts +33 -0
  51. package/src/Navigator.ts +404 -0
  52. package/src/ReactRegistry.tsx +140 -0
  53. package/src/hooks.ts +83 -0
  54. package/src/index.ts +9 -0
  55. package/src/router/drawer.ts +34 -0
  56. package/src/router/index.ts +166 -0
  57. package/src/router/screen.ts +12 -0
  58. package/src/router/stack.ts +80 -0
  59. package/src/router/tabs.ts +49 -0
  60. package/src/router/typing.ts +45 -0
  61. package/src/store.ts +49 -0
  62. package/src/typing.ts +148 -0
  63. package/src/utils.ts +83 -0
  64. package/.buckconfig +0 -6
  65. package/.editorconfig +0 -16
  66. package/.eslintignore +0 -5
  67. package/.gitattributes +0 -1
  68. package/.github/workflows/docs.yml +0 -25
  69. package/CHANGELOG.md +0 -393
  70. package/babel.config.js +0 -15
  71. package/docs/README.md +0 -109
  72. package/docs/custom-tabbar.md +0 -138
  73. package/docs/deeplink.md +0 -148
  74. package/docs/integration-native.md +0 -390
  75. package/docs/integration-react.md +0 -194
  76. package/docs/lifecycle.md +0 -85
  77. package/docs/navigation.md +0 -589
  78. package/docs/pass-and-return-value.md +0 -163
  79. package/docs/qa.md +0 -57
  80. package/docs/style.md +0 -533
  81. package/jest.config.js +0 -4
  82. package/metro.config.js +0 -17
  83. package/screenshot/android.png +0 -0
  84. package/screenshot/bulge.jpg +0 -0
  85. package/screenshot/controller-base.jpg +0 -0
  86. package/screenshot/demo-qrcode.png +0 -0
  87. package/screenshot/header-search-paths.jpg +0 -0
  88. package/screenshot/ios-react-script.png +0 -0
  89. package/screenshot/ios-run-script.jpg +0 -0
  90. package/screenshot/statusbar-inperfect.png +0 -0
  91. package/screenshot/statusbar-setup-step-1.png +0 -0
  92. package/screenshot/statusbar-setup-step-2.png +0 -0
  93. package/screenshot/tabbar.jpg +0 -0
  94. package/screenshot/topbar-default.png +0 -0
  95. package/screenshot/xcode-linking.png +0 -0
@@ -0,0 +1,404 @@
1
+ import {
2
+ EventEmitter,
3
+ NavigationModule,
4
+ EVENT_WILL_SET_ROOT,
5
+ EVENT_DID_SET_ROOT,
6
+ EVENT_SWITCH_TAB,
7
+ KEY_SCENE_ID,
8
+ KEY_INDEX,
9
+ RESULT_CANCEL,
10
+ EVENT_NAVIGATION,
11
+ KEY_ON,
12
+ ON_COMPONENT_RESULT,
13
+ KEY_REQUEST_CODE,
14
+ KEY_RESULT_CODE,
15
+ KEY_RESULT_DATA,
16
+ } from './NavigationModule'
17
+ import { bindBarButtonItemClickEvent } from './utils'
18
+ import store from './store'
19
+ import { RouteGraph, Route } from './router'
20
+ import { Visibility } from './hooks'
21
+ import {
22
+ IndexType,
23
+ ResultType,
24
+ NavigationInterceptor,
25
+ Layout,
26
+ BuildInLayout,
27
+ NavigationItem,
28
+ PropsType,
29
+ } from './typing'
30
+ import { Garden } from './Garden'
31
+
32
+ interface Params {
33
+ animated?: boolean
34
+ moduleName?: string
35
+ layout?: BuildInLayout | Layout
36
+ popToRoot?: boolean
37
+ requestCode?: number
38
+ props?: IndexType
39
+ options?: NavigationItem
40
+ [index: string]: any
41
+ }
42
+
43
+ interface NavigationState {
44
+ params: { readonly [index: string]: any }
45
+ }
46
+
47
+ let interceptor: NavigationInterceptor
48
+ let shouldCallWillSetRootCallback = 0
49
+ let willSetRootCallback: () => void
50
+ let didSetRootCallback: () => void
51
+ let tag = 0
52
+
53
+ EventEmitter.addListener(EVENT_DID_SET_ROOT, _ => {
54
+ didSetRootCallback && didSetRootCallback()
55
+ shouldCallWillSetRootCallback = 0
56
+ })
57
+
58
+ EventEmitter.addListener(EVENT_WILL_SET_ROOT, _ => {
59
+ if (shouldCallWillSetRootCallback === 0 && willSetRootCallback) {
60
+ willSetRootCallback()
61
+ }
62
+ })
63
+
64
+ EventEmitter.addListener(EVENT_SWITCH_TAB, event => {
65
+ const index = event[KEY_INDEX]
66
+ const [from, to] = index.split('-')
67
+ Navigator.dispatch(event[KEY_SCENE_ID], 'switchTab', {
68
+ from: Number(from),
69
+ to: Number(to),
70
+ })
71
+ })
72
+
73
+ interface ResultListener<T extends ResultType> {
74
+ (resultCode: number, data: T): void
75
+ cancel: () => void
76
+ sceneId: string
77
+ }
78
+
79
+ const resultListeners = new Map<number, ResultListener<any>>()
80
+
81
+ EventEmitter.addListener(EVENT_NAVIGATION, data => {
82
+ if (data[KEY_ON] === ON_COMPONENT_RESULT) {
83
+ const requestCode = data[KEY_REQUEST_CODE]
84
+ const resultCode = data[KEY_RESULT_CODE]
85
+ const resultData = data[KEY_RESULT_DATA]
86
+ const sceneId = data[KEY_SCENE_ID]
87
+
88
+ if (requestCode < 0) {
89
+ const listener = resultListeners.get(requestCode)
90
+ if (listener) {
91
+ resultListeners.delete(requestCode)
92
+ listener(resultCode, resultData)
93
+ }
94
+ } else {
95
+ const navigator = Navigator.of(sceneId)
96
+ navigator.result(resultCode, resultData)
97
+ }
98
+ }
99
+ })
100
+
101
+ export class Navigator {
102
+ static of(sceneId: string) {
103
+ let navigator = store.getNavigator(sceneId)
104
+ if (!navigator) {
105
+ navigator = new Navigator(sceneId)
106
+ store.addNavigator(sceneId, navigator)
107
+ }
108
+ return navigator
109
+ }
110
+
111
+ static async find(moduleName: string) {
112
+ const sceneId = await NavigationModule.findSceneIdByModuleName(moduleName)
113
+ if (sceneId) {
114
+ return Navigator.of(sceneId)
115
+ }
116
+ }
117
+
118
+ static async current(): Promise<Navigator> {
119
+ const route = await Navigator.currentRoute()
120
+ return Navigator.of(route.sceneId)
121
+ }
122
+
123
+ static async currentRoute(): Promise<Route> {
124
+ return await NavigationModule.currentRoute()
125
+ }
126
+
127
+ static async routeGraph(): Promise<RouteGraph[]> {
128
+ return await NavigationModule.routeGraph()
129
+ }
130
+
131
+ static setRoot(layout: BuildInLayout | Layout, sticky = false) {
132
+ const pureLayout = bindBarButtonItemClickEvent(layout, {
133
+ inLayout: true,
134
+ navigatorFactory: (sceneId: string) => {
135
+ return Navigator.of(sceneId)
136
+ },
137
+ })
138
+ if (willSetRootCallback) {
139
+ shouldCallWillSetRootCallback++
140
+ willSetRootCallback()
141
+ }
142
+
143
+ const flag = --tag
144
+ NavigationModule.setRoot(pureLayout, sticky, flag)
145
+
146
+ return new Promise<void>(resolve => {
147
+ const subscription = EventEmitter.addListener(EVENT_DID_SET_ROOT, (data: { tag: number }) => {
148
+ if (data.tag === flag) {
149
+ subscription.remove()
150
+ resolve()
151
+ }
152
+ })
153
+ })
154
+ }
155
+
156
+ static setRootLayoutUpdateListener(willSetRoot = () => {}, didSetRoot = () => {}) {
157
+ willSetRootCallback = willSetRoot
158
+ didSetRootCallback = didSetRoot
159
+ }
160
+
161
+ static async dispatch(sceneId: string, action: string, params: Params = {}): Promise<boolean> {
162
+ let intercepted = false
163
+ const { from, to } = params
164
+ if (interceptor) {
165
+ const result = interceptor(action, {
166
+ sceneId,
167
+ from,
168
+ to,
169
+ })
170
+ if (result instanceof Promise) {
171
+ intercepted = await result
172
+ } else {
173
+ intercepted = result
174
+ }
175
+ }
176
+
177
+ if (!intercepted) {
178
+ return await NavigationModule.dispatch(sceneId, action, params)
179
+ }
180
+
181
+ return false
182
+ }
183
+
184
+ static setInterceptor(interceptFn: NavigationInterceptor) {
185
+ interceptor = interceptFn
186
+ }
187
+
188
+ visibility: Visibility = 'pending'
189
+
190
+ constructor(public sceneId: string, public moduleName?: string) {
191
+ this.dispatch = this.dispatch.bind(this)
192
+ this.setParams = this.setParams.bind(this)
193
+
194
+ this.push = this.push.bind(this)
195
+ this.pop = this.pop.bind(this)
196
+ this.popTo = this.popTo.bind(this)
197
+ this.popToRoot = this.popToRoot.bind(this)
198
+ this.redirectTo = this.redirectTo.bind(this)
199
+ this.isStackRoot = this.isStackRoot.bind(this)
200
+
201
+ this.present = this.present.bind(this)
202
+ this.dismiss = this.dismiss.bind(this)
203
+ this.showModal = this.showModal.bind(this)
204
+ this.hideModal = this.hideModal.bind(this)
205
+ this.setResult = this.setResult.bind(this)
206
+
207
+ this.toggleMenu = this.toggleMenu.bind(this)
208
+ this.openMenu = this.openMenu.bind(this)
209
+ this.closeMenu = this.closeMenu.bind(this)
210
+ }
211
+
212
+ readonly state: NavigationState = {
213
+ params: {},
214
+ }
215
+
216
+ setParams(params: { [index: string]: any }) {
217
+ this.state.params = { ...this.state.params, ...params }
218
+ }
219
+
220
+ dispatch(action: string, params: Params = {}) {
221
+ return Navigator.dispatch(this.sceneId, action, {
222
+ from: this.moduleName,
223
+ to: params.moduleName,
224
+ ...params,
225
+ })
226
+ }
227
+
228
+ result(resultCode: number, data: ResultType) {
229
+ if (this.resultListener) {
230
+ this.resultListener(resultCode, data)
231
+ this.resultListener = null
232
+ }
233
+ }
234
+
235
+ unmount() {
236
+ const codes: number[] = []
237
+ for (const [requestCode, listener] of resultListeners) {
238
+ if (listener.sceneId === this.sceneId) {
239
+ codes.push(requestCode)
240
+ listener.cancel()
241
+ }
242
+ }
243
+ codes.forEach(code => resultListeners.delete(code))
244
+
245
+ if (this.resultListener) {
246
+ this.resultListener.cancel()
247
+ this.resultListener = null
248
+ }
249
+ }
250
+
251
+ private _garden?: Garden
252
+
253
+ get garden() {
254
+ if (!this._garden) {
255
+ this._garden = new Garden(this.sceneId)
256
+ }
257
+ return this._garden
258
+ }
259
+
260
+ private resultListener: ResultListener<any> | null = null
261
+
262
+ private waitResult<T extends ResultType>(requestCode: number, successful: boolean): Promise<[number, T]> {
263
+ if (!successful) {
264
+ return Promise.resolve([RESULT_CANCEL, null as any])
265
+ }
266
+
267
+ if (this.resultListener) {
268
+ this.resultListener.cancel()
269
+ this.resultListener = null
270
+ }
271
+
272
+ return new Promise<[number, T]>(resolve => {
273
+ const listener = (resultCode: number, data: T) => {
274
+ resolve([resultCode, data])
275
+ }
276
+
277
+ listener.cancel = () => {
278
+ resolve([RESULT_CANCEL, null as any])
279
+ }
280
+
281
+ listener.sceneId = this.sceneId
282
+
283
+ if (requestCode < 0) {
284
+ resultListeners.set(requestCode, listener)
285
+ } else {
286
+ this.resultListener = listener
287
+ }
288
+ })
289
+ }
290
+
291
+ async push<T extends ResultType, P extends PropsType = PropsType>(
292
+ moduleName: string,
293
+ props: P = {} as any,
294
+ options: NavigationItem = {},
295
+ ) {
296
+ const success = await this.dispatch('push', { moduleName, props, options })
297
+ return await this.waitResult<T>(0, success)
298
+ }
299
+
300
+ async pushLayout<T extends ResultType>(layout: BuildInLayout | Layout) {
301
+ const success = await this.dispatch('pushLayout', { layout })
302
+ return await this.waitResult<T>(0, success)
303
+ }
304
+
305
+ pop() {
306
+ return this.dispatch('pop')
307
+ }
308
+
309
+ popTo(moduleName: string, inclusive: boolean = false) {
310
+ return this.dispatch('popTo', { moduleName, inclusive })
311
+ }
312
+
313
+ popToRoot() {
314
+ return this.dispatch('popToRoot')
315
+ }
316
+
317
+ redirectTo<P extends PropsType = PropsType>(moduleName: string, props: P = {} as any, options: NavigationItem = {}) {
318
+ return this.dispatch('redirectTo', {
319
+ moduleName,
320
+ props,
321
+ options,
322
+ animated: true,
323
+ })
324
+ }
325
+
326
+ isStackRoot(): Promise<boolean> {
327
+ return NavigationModule.isStackRoot(this.sceneId)
328
+ }
329
+
330
+ async present<T extends ResultType, P extends PropsType = PropsType>(
331
+ moduleName: string,
332
+ props: P = {} as any,
333
+ options: NavigationItem = {},
334
+ ) {
335
+ const requestCode = --tag
336
+ const success = await this.dispatch('present', {
337
+ moduleName,
338
+ props,
339
+ options,
340
+ requestCode,
341
+ })
342
+ return await this.waitResult<T>(requestCode, success)
343
+ }
344
+
345
+ async presentLayout<T extends ResultType>(layout: BuildInLayout | Layout) {
346
+ const requestCode = --tag
347
+ const success = await this.dispatch('presentLayout', { layout, requestCode })
348
+ return await this.waitResult<T>(requestCode, success)
349
+ }
350
+
351
+ dismiss() {
352
+ return this.dispatch('dismiss')
353
+ }
354
+
355
+ async showModal<T extends ResultType, P extends PropsType = PropsType>(
356
+ moduleName: string,
357
+ props: P = {} as any,
358
+ options: NavigationItem = {},
359
+ ) {
360
+ const requestCode = --tag
361
+ const success = await this.dispatch('showModal', {
362
+ moduleName,
363
+ props,
364
+ options,
365
+ requestCode,
366
+ })
367
+ return await this.waitResult<T>(requestCode, success)
368
+ }
369
+
370
+ async showModalLayout<T extends ResultType>(layout: BuildInLayout | Layout) {
371
+ const requestCode = --tag
372
+ const success = await this.dispatch('showModalLayout', { layout, requestCode })
373
+ return await this.waitResult<T>(requestCode, success)
374
+ }
375
+
376
+ hideModal() {
377
+ return this.dispatch('hideModal')
378
+ }
379
+
380
+ setResult<T extends ResultType>(resultCode: number, data: T = null as any): void {
381
+ NavigationModule.setResult(this.sceneId, resultCode, data)
382
+ }
383
+
384
+ async switchTab(index: number, popToRoot: boolean = false) {
385
+ const from = await NavigationModule.currentTab(this.sceneId)
386
+ return this.dispatch('switchTab', { from, to: index, popToRoot })
387
+ }
388
+
389
+ toggleMenu() {
390
+ return this.dispatch('toggleMenu')
391
+ }
392
+
393
+ openMenu() {
394
+ return this.dispatch('openMenu')
395
+ }
396
+
397
+ closeMenu() {
398
+ return this.dispatch('closeMenu')
399
+ }
400
+
401
+ signalFirstRenderComplete(): void {
402
+ NavigationModule.signalFirstRenderComplete(this.sceneId)
403
+ }
404
+ }
@@ -0,0 +1,140 @@
1
+ import { AppRegistry, ComponentProvider } from 'react-native'
2
+ import React, { useEffect, createContext } from 'react'
3
+ import { Navigator } from './Navigator'
4
+ import {
5
+ NavigationModule,
6
+ EventEmitter,
7
+ EVENT_NAVIGATION,
8
+ KEY_SCENE_ID,
9
+ KEY_ON,
10
+ ON_COMPONENT_APPEAR,
11
+ ON_COMPONENT_DISAPPEAR,
12
+ } from './NavigationModule'
13
+ import { Garden } from './Garden'
14
+ import { RouteConfig, router } from './router'
15
+ import store from './store'
16
+ import { bindBarButtonItemClickEvent, removeBarButtonItemClickEvent } from './utils'
17
+ import { NavigationItem } from './typing'
18
+
19
+ export interface InjectedProps {
20
+ navigator: Navigator
21
+ garden: Garden
22
+ sceneId: string
23
+ }
24
+
25
+ interface Props {
26
+ sceneId: string
27
+ }
28
+
29
+ type ComponentType = ReturnType<ComponentProvider>
30
+
31
+ function getDisplayName(WrappedComponent: ComponentType) {
32
+ return WrappedComponent.displayName || WrappedComponent.name || 'Component'
33
+ }
34
+
35
+ export const NavigationContext = createContext<any>(null)
36
+
37
+ function withNavigator(moduleName: string) {
38
+ return function (WrappedComponent: ComponentType) {
39
+ const FC = React.forwardRef((props: Props, ref: React.Ref<ComponentType>) => {
40
+ const { sceneId } = props
41
+
42
+ const navigator = Navigator.of(sceneId)
43
+ if (navigator.moduleName === undefined) {
44
+ navigator.moduleName = moduleName
45
+ }
46
+
47
+ const garden = navigator.garden
48
+
49
+ useEffect(() => {
50
+ navigator.signalFirstRenderComplete()
51
+ return () => {
52
+ removeBarButtonItemClickEvent(navigator.sceneId)
53
+ store.removeNavigator(navigator.sceneId)
54
+ navigator.unmount()
55
+ }
56
+ }, [navigator])
57
+
58
+ useEffect(() => {
59
+ const subscription = EventEmitter.addListener(EVENT_NAVIGATION, data => {
60
+ if (navigator.sceneId === data[KEY_SCENE_ID]) {
61
+ if (data[KEY_ON] === ON_COMPONENT_APPEAR) {
62
+ navigator.visibility = 'visible'
63
+ } else if (data[KEY_ON] === ON_COMPONENT_DISAPPEAR) {
64
+ navigator.visibility = 'invisible'
65
+ }
66
+ }
67
+ })
68
+ return () => {
69
+ subscription.remove()
70
+ }
71
+ }, [navigator])
72
+
73
+ const injected = {
74
+ garden,
75
+ navigator,
76
+ }
77
+
78
+ return (
79
+ <NavigationContext.Provider value={navigator}>
80
+ <WrappedComponent ref={ref} {...props} {...injected} />
81
+ </NavigationContext.Provider>
82
+ )
83
+ })
84
+
85
+ FC.displayName = `withNavigator(${getDisplayName(WrappedComponent)})`
86
+ return FC
87
+ }
88
+ }
89
+
90
+ export type HOC = (WrappedComponent: ComponentType) => ComponentType
91
+ let wrap: HOC | undefined
92
+
93
+ export class ReactRegistry {
94
+ static registerEnded: boolean
95
+ static startRegisterComponent(hoc?: HOC) {
96
+ store.clear()
97
+ wrap = hoc
98
+ ReactRegistry.registerEnded = false
99
+ NavigationModule.startRegisterReactComponent()
100
+ }
101
+
102
+ static endRegisterComponent() {
103
+ if (ReactRegistry.registerEnded) {
104
+ console.warn(`Please don't call ReactRegistry#endRegisterComponent multiple times.`)
105
+ return
106
+ }
107
+ ReactRegistry.registerEnded = true
108
+ NavigationModule.endRegisterReactComponent()
109
+ }
110
+
111
+ static registerComponent(appKey: string, getComponentFunc: ComponentProvider, routeConfig?: RouteConfig) {
112
+ if (routeConfig) {
113
+ router.registerRoute(appKey, routeConfig)
114
+ }
115
+
116
+ let WrappedComponent = getComponentFunc()
117
+ if (wrap) {
118
+ WrappedComponent = wrap(WrappedComponent)
119
+ }
120
+
121
+ // build static options
122
+ let options: object = bindBarButtonItemClickEvent((WrappedComponent as any).navigationItem) || {}
123
+ NavigationModule.registerReactComponent(appKey, options)
124
+
125
+ let RootComponent = withNavigator(appKey)(WrappedComponent)
126
+ AppRegistry.registerComponent(appKey, () => RootComponent)
127
+ }
128
+ }
129
+
130
+ export function withNavigationItem(item: NavigationItem) {
131
+ return function (WrappedComponent: ComponentType): ComponentType {
132
+ let navigationItem = (WrappedComponent as any).navigationItem
133
+ if (navigationItem) {
134
+ ;(WrappedComponent as any).navigationItem = { ...navigationItem, ...item }
135
+ } else {
136
+ ;(WrappedComponent as any).navigationItem = item
137
+ }
138
+ return WrappedComponent
139
+ }
140
+ }
package/src/hooks.ts ADDED
@@ -0,0 +1,83 @@
1
+ import React, { useContext, useEffect, useRef, useState } from 'react'
2
+
3
+ import {
4
+ EVENT_NAVIGATION,
5
+ EventEmitter,
6
+ KEY_ON,
7
+ KEY_SCENE_ID,
8
+ ON_COMPONENT_APPEAR,
9
+ ON_COMPONENT_DISAPPEAR,
10
+ } from './NavigationModule'
11
+ import { Navigator } from './Navigator'
12
+ import { NavigationContext } from './ReactRegistry'
13
+
14
+ export type Visibility = 'visible' | 'invisible' | 'pending'
15
+
16
+ export function useVisible() {
17
+ const visibility = useVisibility()
18
+ return visibility === 'visible'
19
+ }
20
+
21
+ export function useVisibility() {
22
+ const navigator = useNavigator()
23
+ const [visibility, setVisibility] = useState(navigator.visibility)
24
+
25
+ useEffect(() => {
26
+ const subscription = EventEmitter.addListener(EVENT_NAVIGATION, data => {
27
+ if (navigator.sceneId === data[KEY_SCENE_ID]) {
28
+ if (data[KEY_ON] === ON_COMPONENT_APPEAR) {
29
+ setVisibility('visible')
30
+ } else if (data[KEY_ON] === ON_COMPONENT_DISAPPEAR) {
31
+ setVisibility('invisible')
32
+ }
33
+ }
34
+ })
35
+
36
+ return () => {
37
+ subscription.remove()
38
+ }
39
+ }, [navigator])
40
+
41
+ return visibility
42
+ }
43
+
44
+ export function useVisibleEffect(effect: React.EffectCallback) {
45
+ const navigator = useNavigator()
46
+ const destructor = useRef<ReturnType<React.EffectCallback>>()
47
+
48
+ useEffect(() => {
49
+ if (navigator.visibility === 'visible') {
50
+ destructor.current = effect()
51
+ }
52
+
53
+ const subscription = EventEmitter.addListener(EVENT_NAVIGATION, data => {
54
+ if (navigator.sceneId === data[KEY_SCENE_ID]) {
55
+ if (data[KEY_ON] === ON_COMPONENT_APPEAR) {
56
+ destructor.current = effect()
57
+ } else if (data[KEY_ON] === ON_COMPONENT_DISAPPEAR) {
58
+ if (destructor.current) {
59
+ destructor.current()
60
+ destructor.current = undefined
61
+ }
62
+ }
63
+ }
64
+ })
65
+
66
+ return () => {
67
+ if (destructor.current) {
68
+ destructor.current()
69
+ destructor.current = undefined
70
+ }
71
+ subscription.remove()
72
+ }
73
+ }, [effect, navigator])
74
+ }
75
+
76
+ export function useNavigator(): Navigator {
77
+ return useContext<Navigator>(NavigationContext)
78
+ }
79
+
80
+ export function useGarden() {
81
+ const ctx = useContext<Navigator>(NavigationContext)
82
+ return ctx.garden
83
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export * from './Navigator'
2
+ export * from './Garden'
3
+ export * from './ReactRegistry'
4
+ export * from './hooks'
5
+ export * from './router'
6
+ export * from './DeepLink'
7
+ export * from './typing'
8
+
9
+ export { RESULT_OK, RESULT_CANCEL, TOOLBAR_HEIGHT, toolbarHeight } from './NavigationModule'
@@ -0,0 +1,34 @@
1
+ import { RouteGraph, LayoutMode, RouteInfo, RouteHandler } from './typing'
2
+ import { ScreenGraph } from './screen'
3
+ import { Navigator } from '../Navigator'
4
+
5
+ export interface DrawerGraph extends RouteGraph {
6
+ layout: 'drawer'
7
+ sceneId: string
8
+ mode: LayoutMode
9
+ children: [RouteGraph, ScreenGraph]
10
+ }
11
+
12
+ export function isDrawerGraph(graph: RouteGraph): graph is DrawerGraph {
13
+ return graph.layout === 'drawer'
14
+ }
15
+
16
+ export async function drawerRouteHandler(graph: RouteGraph, route: RouteInfo, next: RouteHandler) {
17
+ if (!isDrawerGraph(graph)) {
18
+ return false
19
+ }
20
+
21
+ const { moduleName, sceneId } = graph.children[1]
22
+ if (moduleName === route.moduleName) {
23
+ const navigator = Navigator.of(sceneId)
24
+ navigator.openMenu()
25
+ return true
26
+ } else {
27
+ let result = await next(graph.children[0], route, next)
28
+ if (result) {
29
+ const navigator = Navigator.of(sceneId)
30
+ navigator.closeMenu()
31
+ }
32
+ return result
33
+ }
34
+ }