hybrid-navigation 3.0.1 → 3.0.3

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/dist/hooks.d.ts CHANGED
@@ -1,6 +1,3 @@
1
- import React from 'react';
2
1
  import { Navigator } from './Navigator';
3
2
  export declare function useNavigator(): Navigator;
4
- export declare function useVisibility(): "visible" | "invisible" | "pending";
5
- export declare function useVisible(): boolean;
6
- export declare function useVisibleEffect(effect: React.EffectCallback): void;
3
+ export declare function useVisibleEffect<T extends Function>(callback: T): void;
package/dist/hooks.js CHANGED
@@ -1,31 +1,26 @@
1
- import { useContext, useEffect, useState } from 'react';
1
+ import { useContext, useEffect, useEffectEvent } from 'react';
2
2
  import Navigation from './Navigation';
3
3
  import { NavigationContext } from './Navigator';
4
4
  export function useNavigator() {
5
5
  return useContext(NavigationContext);
6
6
  }
7
- export function useVisibility() {
7
+ export function useVisibleEffect(callback) {
8
8
  const navigator = useNavigator();
9
- const [visibility, setVisibility] = useState(navigator.visibility);
9
+ const event = useEffectEvent(callback);
10
10
  useEffect(() => {
11
+ let destructor;
11
12
  const subscription = Navigation.addVisibilityEventListener(navigator.sceneId, v => {
12
- setVisibility(v);
13
+ if (v === 'visible') {
14
+ destructor = event();
15
+ }
16
+ else {
17
+ destructor && destructor();
18
+ destructor = undefined;
19
+ }
13
20
  });
14
- return () => subscription.remove();
21
+ return () => {
22
+ subscription.remove();
23
+ destructor && destructor();
24
+ };
15
25
  }, [navigator]);
16
- return visibility;
17
- }
18
- export function useVisible() {
19
- const visibility = useVisibility();
20
- return visibility === 'visible';
21
- }
22
- export function useVisibleEffect(effect) {
23
- const visible = useVisible();
24
- useEffect(() => {
25
- if (!visible) {
26
- return;
27
- }
28
- const destructor = effect();
29
- return () => destructor && destructor();
30
- }, [effect, visible]);
31
26
  }
@@ -1,7 +1,25 @@
1
- #import <React/RCTSurfaceHostingProxyRootView.h>
1
+ #import <UIKit/UIKit.h>
2
2
 
3
- @interface HBDRootView : UIView
3
+ #import <React/RCTSurfaceDelegate.h>
4
+ #import <React/RCTSurfaceProtocol.h>
5
+ #import <React/RCTSurfaceSizeMeasureMode.h>
6
+ #import <React/RCTSurfaceStage.h>
7
+
8
+ @class RCTSurface;
9
+
10
+ NS_ASSUME_NONNULL_BEGIN
11
+
12
+ @interface HBDRootView : UIView <RCTSurfaceDelegate>
13
+
14
+ - (instancetype)initWithSurface:(id<RCTSurfaceProtocol>)surface;
15
+
16
+ @property (nonatomic, strong, readonly) id<RCTSurfaceProtocol> surface;
17
+
18
+ @property (nonatomic, assign) BOOL passThroughTouches;
19
+
20
+ @property (nonatomic, copy, readwrite) NSDictionary *appProperties;
4
21
 
5
- - (instancetype)initWithRootView:(RCTSurfaceHostingProxyRootView *)rootView;
6
22
 
7
23
  @end
24
+
25
+ NS_ASSUME_NONNULL_END
@@ -1,32 +1,203 @@
1
1
  #import "HBDRootView.h"
2
2
 
3
+ #import "RCTConstants.h"
4
+ #import "RCTDefines.h"
5
+ #import "RCTSurface.h"
6
+ #import "RCTSurfaceDelegate.h"
7
+ #import "RCTSurfaceView.h"
8
+ #import "RCTUtils.h"
9
+
3
10
  @interface HBDRootView ()
4
11
 
5
- @property (nonatomic, strong, readonly) RCTSurfaceHostingProxyRootView *rootView;
12
+ @property (nonatomic, assign) BOOL isSurfaceViewVisible;
13
+ @property (nonatomic, assign) RCTSurfaceSizeMeasureMode sizeMeasureMode;
6
14
 
7
15
  @end
8
16
 
9
- @implementation HBDRootView
17
+ @implementation HBDRootView {
18
+ UIView *_Nullable _surfaceView;
19
+ RCTSurfaceStage _stage;
20
+ }
21
+
22
+ RCT_NOT_IMPLEMENTED(-(instancetype)init)
23
+ RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
24
+ RCT_NOT_IMPLEMENTED(-(nullable instancetype)initWithCoder : (NSCoder *)coder)
25
+
26
+ - (instancetype)initWithSurface:(id<RCTSurfaceProtocol>)surface {
27
+ return [self initWithSurface:surface sizeMeasureMode:RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightExact];
28
+ }
29
+
30
+ - (instancetype)initWithSurface:(id<RCTSurfaceProtocol>)surface
31
+ sizeMeasureMode:(RCTSurfaceSizeMeasureMode)sizeMeasureMode {
32
+ if (self = [super initWithFrame:CGRectZero]) {
33
+ _surface = surface;
34
+ _sizeMeasureMode = sizeMeasureMode;
35
+ _surface.delegate = self;
36
+ _stage = surface.stage;
37
+ [self _updateViews];
38
+ }
39
+
40
+ return self;
41
+ }
10
42
 
11
- - (instancetype)initWithRootView:(RCTSurfaceHostingProxyRootView *)rootView {
12
- if (self = [super initWithFrame:CGRectZero]) {
13
- _rootView = rootView;
14
- }
15
- return self;
43
+ - (void)dealloc {
44
+ [_surface stop];
16
45
  }
17
46
 
18
47
  - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
19
- UIView *hitView = [super hitTest:point withEvent:event];
20
- if (self.rootView.passThroughTouches) {
21
- if (hitView == self) {
22
- return nil;
23
- }
24
-
25
- if (hitView && CGRectEqualToRect(hitView.frame, self.rootView.bounds)) {
26
- return nil;
27
- }
28
- }
29
- return hitView;
48
+ UIView *hitView = [super hitTest:point withEvent:event];
49
+ if (self.passThroughTouches && [self shouldPassTouches:hitView]) {
50
+ return nil;
51
+ }
52
+ return hitView;
53
+ }
54
+
55
+ - (BOOL)shouldPassTouches:(UIView *)hitView {
56
+ if (!hitView) {
57
+ return true;
58
+ }
59
+
60
+ // RCTRootComponentView
61
+ if (hitView == _surfaceView || hitView == [_surfaceView.subviews firstObject]) {
62
+ return true;
63
+ }
64
+
65
+ return false;
66
+ }
67
+
68
+ - (NSDictionary *)appProperties {
69
+ return _surface.properties;
70
+ }
71
+
72
+ - (void)setAppProperties:(NSDictionary *)appProperties {
73
+ [_surface setProperties:appProperties];
74
+ }
75
+
76
+ - (void)layoutSubviews {
77
+ [super layoutSubviews];
78
+
79
+ CGSize minimumSize;
80
+ CGSize maximumSize;
81
+
82
+ RCTSurfaceMinimumSizeAndMaximumSizeFromSizeAndSizeMeasureMode(
83
+ self.bounds.size, _sizeMeasureMode, &minimumSize, &maximumSize);
84
+ CGRect windowFrame = [self.window convertRect:self.frame fromView:self.superview];
85
+
86
+ [_surface setMinimumSize:minimumSize maximumSize:maximumSize viewportOffset:windowFrame.origin];
87
+ }
88
+
89
+ - (CGSize)intrinsicContentSize {
90
+ if (RCTSurfaceStageIsPreparing(_stage)) {
91
+ return CGSizeZero;
92
+ }
93
+
94
+ return _surface.intrinsicSize;
95
+ }
96
+
97
+ - (CGSize)sizeThatFits:(CGSize)size {
98
+ if (RCTSurfaceStageIsPreparing(_stage)) {
99
+ return CGSizeZero;
100
+ }
101
+
102
+ CGSize minimumSize;
103
+ CGSize maximumSize;
104
+
105
+ RCTSurfaceMinimumSizeAndMaximumSizeFromSizeAndSizeMeasureMode(size, _sizeMeasureMode, &minimumSize, &maximumSize);
106
+
107
+ return [_surface sizeThatFitsMinimumSize:minimumSize maximumSize:maximumSize];
108
+ }
109
+
110
+ - (void)setStage:(RCTSurfaceStage)stage {
111
+ if (stage == _stage) {
112
+ return;
113
+ }
114
+
115
+ BOOL shouldInvalidateLayout = RCTSurfaceStageIsRunning(stage) != RCTSurfaceStageIsRunning(_stage) ||
116
+ RCTSurfaceStageIsPreparing(stage) != RCTSurfaceStageIsPreparing(_stage);
117
+
118
+ _stage = stage;
119
+
120
+ if (shouldInvalidateLayout) {
121
+ [self _invalidateLayout];
122
+ [self _updateViews];
123
+ }
124
+ }
125
+
126
+ - (void)setSizeMeasureMode:(RCTSurfaceSizeMeasureMode)sizeMeasureMode {
127
+ if (sizeMeasureMode == _sizeMeasureMode) {
128
+ return;
129
+ }
130
+
131
+ _sizeMeasureMode = sizeMeasureMode;
132
+ [self _invalidateLayout];
133
+ }
134
+
135
+
136
+ #pragma mark - isSurfaceViewVisible
137
+
138
+ - (void)setIsSurfaceViewVisible:(BOOL)visible {
139
+ if (_isSurfaceViewVisible == visible) {
140
+ return;
141
+ }
142
+
143
+ _isSurfaceViewVisible = visible;
144
+
145
+ if (visible) {
146
+ _surfaceView = _surface.view;
147
+ _surfaceView.frame = self.bounds;
148
+ _surfaceView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
149
+ [self addSubview:_surfaceView];
150
+ } else {
151
+ [_surfaceView removeFromSuperview];
152
+ _surfaceView = nil;
153
+ }
154
+ }
155
+
156
+ #pragma mark - UITraitCollection updates
157
+
158
+ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
159
+ [super traitCollectionDidChange:previousTraitCollection];
160
+
161
+ if (RCTSharedApplication().applicationState == UIApplicationStateBackground) {
162
+ return;
163
+ }
164
+
165
+ [[NSNotificationCenter defaultCenter]
166
+ postNotificationName:RCTUserInterfaceStyleDidChangeNotification
167
+ object:self
168
+ userInfo:@{
169
+ RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey : self.traitCollection,
170
+ }];
171
+ }
172
+
173
+ #pragma mark - Private stuff
174
+
175
+ - (void)_invalidateLayout {
176
+ [self invalidateIntrinsicContentSize];
177
+ [self.superview setNeedsLayout];
178
+ }
179
+
180
+ - (void)_updateViews {
181
+ self.isSurfaceViewVisible = RCTSurfaceStageIsRunning(_stage);
182
+ }
183
+
184
+ - (void)didMoveToWindow {
185
+ [super didMoveToWindow];
186
+ [self _updateViews];
187
+ }
188
+
189
+ #pragma mark - RCTSurfaceDelegate
190
+
191
+ - (void)surface:(__unused RCTSurface *)surface didChangeStage:(RCTSurfaceStage)stage {
192
+ RCTExecuteOnMainQueue(^{
193
+ [self setStage:stage];
194
+ });
195
+ }
196
+
197
+ - (void)surface:(__unused RCTSurface *)surface didChangeIntrinsicSize:(__unused CGSize)intrinsicSize {
198
+ RCTExecuteOnMainQueue(^{
199
+ [self _invalidateLayout];
200
+ });
30
201
  }
31
202
 
32
203
  @end
@@ -1,8 +1,8 @@
1
1
  #import "HBDReactViewController.h"
2
2
 
3
3
  #import "HBDReactBridgeManager.h"
4
- #import "HBDTitleView.h"
5
4
  #import "HBDRootView.h"
5
+ #import "HBDTitleView.h"
6
6
  #import "HBDUtils.h"
7
7
  #import "HBDNativeEvent.h"
8
8
 
@@ -13,7 +13,7 @@
13
13
 
14
14
  @property(nonatomic, assign) BOOL firstRenderCompleted;
15
15
  @property(nonatomic, assign) BOOL viewAppeared;
16
- @property(nonatomic, strong, readwrite) RCTSurfaceHostingProxyRootView *rootView;
16
+ @property(nonatomic, strong, readwrite) HBDRootView *rootView;
17
17
 
18
18
  @end
19
19
 
@@ -39,16 +39,18 @@
39
39
  }
40
40
 
41
41
  - (void)loadView {
42
- RCTHost *host = [HBDReactBridgeManager get].rctHost;
43
- RCTFabricSurface *surface = [host createSurfaceWithModuleName:self.moduleName initialProperties:[self propsWithSceneId]];
44
- RCTSurfaceHostingProxyRootView *rootView = [[RCTSurfaceHostingProxyRootView alloc] initWithSurface:(id)surface];
42
+ RCTHost *rctHost = [HBDReactBridgeManager get].rctHost;
43
+ RCTFabricSurface *surface = [rctHost createSurfaceWithModuleName:self.moduleName initialProperties:[self propsWithSceneId]];
44
+ HBDRootView *rootView = [[HBDRootView alloc] initWithSurface:(id)surface];
45
45
  self.rootView = rootView;
46
46
 
47
47
  BOOL passThroughTouches = [self.options[@"passThroughTouches"] boolValue];
48
48
  rootView.passThroughTouches = passThroughTouches;
49
49
  rootView.backgroundColor = UIColor.clearColor;
50
- rootView.translatesAutoresizingMaskIntoConstraints = NO;
51
- self.view = [[HBDRootView alloc] initWithRootView:rootView];
50
+ rootView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
51
+
52
+ self.view = [[UIView alloc] initWithFrame:UIScreen.mainScreen.bounds];
53
+ [rootView setFrame:self.view.frame];
52
54
  [self.view addSubview:rootView];
53
55
  [self updateReactViewConstraints];
54
56
  RCTLogInfo(@"[Navigation] 加载页面 %@", self.moduleName);
@@ -70,7 +72,7 @@
70
72
  } else {
71
73
  size = UILayoutFittingCompressedSize;
72
74
  }
73
-
75
+
74
76
  RCTHost *host = [HBDReactBridgeManager get].rctHost;
75
77
  RCTFabricSurface *surface = [host createSurfaceWithModuleName:moduleName initialProperties:[self propsWithSceneId]];
76
78
  RCTSurfaceHostingProxyRootView *titleRootView = [[RCTSurfaceHostingProxyRootView alloc] initWithSurface:(id)surface];
@@ -87,20 +89,18 @@
87
89
 
88
90
  - (void)updateReactViewConstraints {
89
91
  if (self.isViewLoaded && self.rootView) {
90
- [NSLayoutConstraint deactivateConstraints:_reactViewConstraints];
91
- _reactViewConstraints = @[
92
- [self.rootView.topAnchor
93
- constraintEqualToAnchor:self.shouldFitWindowInsetTop ? self.view.topAnchor : self.view.safeAreaLayoutGuide.topAnchor],
94
- [self.rootView.bottomAnchor
95
- constraintEqualToAnchor:self.view.bottomAnchor],
96
- [self.rootView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
97
- [self.rootView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor]
98
- ];
99
- [NSLayoutConstraint activateConstraints:_reactViewConstraints];
92
+ CGFloat bottomInset = self.shouldFitWindowInsetsBottom ? 0: self.view.safeAreaInsets.bottom;
93
+ CGFloat topInset = self.shouldFitWindowInsetsTop ? 0 : self.view.safeAreaInsets.top;
94
+ [self.rootView setFrame:CGRectMake(
95
+ 0,
96
+ topInset,
97
+ self.view.frame.size.width,
98
+ self.view.frame.size.height - topInset - bottomInset
99
+ )];
100
100
  }
101
101
  }
102
102
 
103
- - (BOOL)shouldFitWindowInsetTop {
103
+ - (BOOL)shouldFitWindowInsetsTop {
104
104
  if (![self.parentViewController isKindOfClass:UINavigationController.class]) {
105
105
  return YES;
106
106
  }
@@ -108,11 +108,11 @@
108
108
  return isTranslucent || self.extendedLayoutIncludesOpaqueBars;
109
109
  }
110
110
 
111
- - (BOOL)shouldFitSafeAreaLayoutBottom {
111
+ - (BOOL)shouldFitWindowInsetsBottom {
112
112
  if (self.navigationController && self.tabBarController) {
113
- return self == [self.navigationController.viewControllers firstObject];
113
+ return self != [self.navigationController.viewControllers firstObject];
114
114
  }
115
- return NO;
115
+ return YES;
116
116
  }
117
117
 
118
118
  - (NSDictionary *)propsWithSceneId {
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": "3.0.1",
4
+ "version": "3.0.3",
5
5
  "main": "./dist/index.js",
6
6
  "typings": "./dist/index.d.ts",
7
7
  "react-native": "src/index",
@@ -41,8 +41,8 @@
41
41
  "lint": "eslint . --fix --ext .js,.jsx,.ts,.tsx"
42
42
  },
43
43
  "peerDependencies": {
44
- "react": ">=16.8",
45
- "react-native": ">=0.60"
44
+ "react": ">=19.2",
45
+ "react-native": ">=0.83"
46
46
  },
47
47
  "dependencies": {
48
48
  "path-to-regexp": "^6.1.0"
package/src/hooks.ts CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useContext, useEffect, useState } from 'react';
1
+ import { useContext, useEffect, useEffectEvent } from 'react';
2
2
  import Navigation from './Navigation';
3
3
  import { Navigator, NavigationContext } from './Navigator';
4
4
 
@@ -6,32 +6,23 @@ export function useNavigator(): Navigator {
6
6
  return useContext<Navigator>(NavigationContext);
7
7
  }
8
8
 
9
- export function useVisibility() {
9
+ export function useVisibleEffect<T extends Function>(callback: T) {
10
10
  const navigator = useNavigator();
11
- const [visibility, setVisibility] = useState(navigator.visibility);
11
+ const event = useEffectEvent(callback);
12
12
 
13
13
  useEffect(() => {
14
+ let destructor: (() => void) | undefined;
14
15
  const subscription = Navigation.addVisibilityEventListener(navigator.sceneId, v => {
15
- setVisibility(v);
16
+ if (v === 'visible') {
17
+ destructor = event();
18
+ } else {
19
+ destructor && destructor();
20
+ destructor = undefined;
21
+ }
16
22
  });
17
- return () => subscription.remove();
23
+ return () => {
24
+ subscription.remove();
25
+ destructor && destructor();
26
+ };
18
27
  }, [navigator]);
19
-
20
- return visibility;
21
- }
22
-
23
- export function useVisible() {
24
- const visibility = useVisibility();
25
- return visibility === 'visible';
26
- }
27
-
28
- export function useVisibleEffect(effect: React.EffectCallback) {
29
- const visible = useVisible();
30
- useEffect(() => {
31
- if (!visible) {
32
- return;
33
- }
34
- const destructor = effect();
35
- return () => destructor && destructor();
36
- }, [effect, visible]);
37
28
  }