hybrid-navigation 2.8.3 → 2.10.1

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 (79) hide show
  1. package/HybridNavigation.podspec +6 -6
  2. package/LICENSE +1 -1
  3. package/android/build.gradle +4 -4
  4. package/android/src/main/java/com/reactnative/hybridnavigation/Garden.java +27 -40
  5. package/android/src/main/java/com/reactnative/hybridnavigation/GardenModule.java +77 -48
  6. package/android/src/main/java/com/reactnative/hybridnavigation/HBDEventEmitter.java +4 -4
  7. package/android/src/main/java/com/reactnative/hybridnavigation/HBDReactRootView.java +1 -8
  8. package/android/src/main/java/com/reactnative/hybridnavigation/HybridFragment.java +94 -43
  9. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationModule.java +134 -117
  10. package/android/src/main/java/com/reactnative/hybridnavigation/ReactAppCompatActivity.java +65 -35
  11. package/android/src/main/java/com/reactnative/hybridnavigation/ReactBridgeManager.java +14 -3
  12. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +133 -89
  13. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarFragment.java +161 -126
  14. package/android/src/main/java/com/reactnative/hybridnavigation/ReactTabBarProvider.java +145 -114
  15. package/android/src/main/java/com/reactnative/hybridnavigation/Utils.java +40 -28
  16. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/DrawerNavigator.java +96 -75
  17. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/Navigator.java +1 -1
  18. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/ScreenNavigator.java +111 -79
  19. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/StackNavigator.java +187 -117
  20. package/android/src/main/java/com/reactnative/hybridnavigation/navigator/TabNavigator.java +137 -96
  21. package/ios/HybridNavigation/ViewController/HBDModalViewController.m +7 -0
  22. package/ios/HybridNavigation/ViewController/HBDViewController+Garden.m +16 -0
  23. package/ios/HybridNavigation.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  24. package/ios/HybridNavigation.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  25. package/ios/HybridNavigation.xcodeproj/project.xcworkspace/xcuserdata/listen.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  26. package/ios/HybridNavigation.xcodeproj/xcuserdata/listen.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
  27. package/lib/Navigator.d.ts +6 -6
  28. package/lib/ReactRegistry.d.ts +3 -3
  29. package/lib/ReactRegistry.js +14 -10
  30. package/lib/typing.d.ts +8 -8
  31. package/package.json +27 -16
  32. package/src/DeepLink.ts +60 -0
  33. package/src/Garden.ts +171 -0
  34. package/src/NavigationModule.ts +33 -0
  35. package/src/Navigator.ts +404 -0
  36. package/src/ReactRegistry.tsx +138 -0
  37. package/src/hooks.ts +83 -0
  38. package/src/index.ts +9 -0
  39. package/src/router/drawer.ts +34 -0
  40. package/src/router/index.ts +166 -0
  41. package/src/router/screen.ts +12 -0
  42. package/src/router/stack.ts +80 -0
  43. package/src/router/tabs.ts +49 -0
  44. package/src/router/typing.ts +45 -0
  45. package/src/store.ts +49 -0
  46. package/src/typing.ts +148 -0
  47. package/src/utils.ts +83 -0
  48. package/.buckconfig +0 -6
  49. package/.editorconfig +0 -16
  50. package/.eslintignore +0 -5
  51. package/.gitattributes +0 -1
  52. package/.github/workflows/docs.yml +0 -25
  53. package/CHANGELOG.md +0 -399
  54. package/babel.config.js +0 -15
  55. package/docs/README.md +0 -109
  56. package/docs/custom-tabbar.md +0 -138
  57. package/docs/deeplink.md +0 -148
  58. package/docs/integration-native.md +0 -390
  59. package/docs/integration-react.md +0 -194
  60. package/docs/lifecycle.md +0 -85
  61. package/docs/navigation.md +0 -589
  62. package/docs/pass-and-return-value.md +0 -163
  63. package/docs/qa.md +0 -57
  64. package/docs/style.md +0 -533
  65. package/jest.config.js +0 -4
  66. package/metro.config.js +0 -17
  67. package/screenshot/android.png +0 -0
  68. package/screenshot/bulge.jpg +0 -0
  69. package/screenshot/controller-base.jpg +0 -0
  70. package/screenshot/demo-qrcode.png +0 -0
  71. package/screenshot/header-search-paths.jpg +0 -0
  72. package/screenshot/ios-react-script.png +0 -0
  73. package/screenshot/ios-run-script.jpg +0 -0
  74. package/screenshot/statusbar-inperfect.png +0 -0
  75. package/screenshot/statusbar-setup-step-1.png +0 -0
  76. package/screenshot/statusbar-setup-step-2.png +0 -0
  77. package/screenshot/tabbar.jpg +0 -0
  78. package/screenshot/topbar-default.png +0 -0
  79. 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,138 @@
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
+ function getDisplayName(WrappedComponent: React.ComponentType<any>) {
30
+ return WrappedComponent.displayName || WrappedComponent.name || 'Component'
31
+ }
32
+
33
+ export const NavigationContext = createContext<any>(null)
34
+
35
+ function withNavigator(moduleName: string) {
36
+ return function (WrappedComponent: React.ComponentType<any>) {
37
+ const FC = React.forwardRef((props: Props, ref: React.Ref<React.ComponentType<any>>) => {
38
+ const { sceneId } = props
39
+
40
+ const navigator = Navigator.of(sceneId)
41
+ if (navigator.moduleName === undefined) {
42
+ navigator.moduleName = moduleName
43
+ }
44
+
45
+ const garden = navigator.garden
46
+
47
+ useEffect(() => {
48
+ navigator.signalFirstRenderComplete()
49
+ return () => {
50
+ removeBarButtonItemClickEvent(navigator.sceneId)
51
+ store.removeNavigator(navigator.sceneId)
52
+ navigator.unmount()
53
+ }
54
+ }, [navigator])
55
+
56
+ useEffect(() => {
57
+ const subscription = EventEmitter.addListener(EVENT_NAVIGATION, data => {
58
+ if (navigator.sceneId === data[KEY_SCENE_ID]) {
59
+ if (data[KEY_ON] === ON_COMPONENT_APPEAR) {
60
+ navigator.visibility = 'visible'
61
+ } else if (data[KEY_ON] === ON_COMPONENT_DISAPPEAR) {
62
+ navigator.visibility = 'invisible'
63
+ }
64
+ }
65
+ })
66
+ return () => {
67
+ subscription.remove()
68
+ }
69
+ }, [navigator])
70
+
71
+ const injected = {
72
+ garden,
73
+ navigator,
74
+ }
75
+
76
+ return (
77
+ <NavigationContext.Provider value={navigator}>
78
+ <WrappedComponent ref={ref} {...props} {...injected} />
79
+ </NavigationContext.Provider>
80
+ )
81
+ })
82
+
83
+ FC.displayName = `withNavigator(${getDisplayName(WrappedComponent)})`
84
+ return FC
85
+ }
86
+ }
87
+
88
+ export type HOC = (WrappedComponent: React.ComponentType<any>) => React.ComponentType<any>
89
+ let wrap: HOC | undefined
90
+
91
+ export class ReactRegistry {
92
+ static registerEnded: boolean
93
+ static startRegisterComponent(hoc?: HOC) {
94
+ store.clear()
95
+ wrap = hoc
96
+ ReactRegistry.registerEnded = false
97
+ NavigationModule.startRegisterReactComponent()
98
+ }
99
+
100
+ static endRegisterComponent() {
101
+ if (ReactRegistry.registerEnded) {
102
+ console.warn(`Please don't call ReactRegistry#endRegisterComponent multiple times.`)
103
+ return
104
+ }
105
+ ReactRegistry.registerEnded = true
106
+ NavigationModule.endRegisterReactComponent()
107
+ }
108
+
109
+ static registerComponent(appKey: string, getComponentFunc: ComponentProvider, routeConfig?: RouteConfig) {
110
+ if (routeConfig) {
111
+ router.registerRoute(appKey, routeConfig)
112
+ }
113
+
114
+ let WrappedComponent = getComponentFunc()
115
+ if (wrap) {
116
+ WrappedComponent = wrap(WrappedComponent)
117
+ }
118
+
119
+ // build static options
120
+ let options: object = bindBarButtonItemClickEvent((WrappedComponent as any).navigationItem) || {}
121
+ NavigationModule.registerReactComponent(appKey, options)
122
+
123
+ let RootComponent = withNavigator(appKey)(WrappedComponent)
124
+ AppRegistry.registerComponent(appKey, () => RootComponent)
125
+ }
126
+ }
127
+
128
+ export function withNavigationItem(item: NavigationItem) {
129
+ return function (WrappedComponent: React.ComponentType<any>): React.ComponentType<any> {
130
+ let navigationItem = (WrappedComponent as any).navigationItem
131
+ if (navigationItem) {
132
+ ;(WrappedComponent as any).navigationItem = { ...navigationItem, ...item }
133
+ } else {
134
+ ;(WrappedComponent as any).navigationItem = item
135
+ }
136
+ return WrappedComponent
137
+ }
138
+ }
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
+ }