@ng-native/components 0.22.2 → 0.23.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 (138) hide show
  1. package/android/build.gradle +22 -0
  2. package/android/src/main/java/dev/ngnative/components/ContextMenuAccessibility.kt +71 -0
  3. package/android/src/main/java/dev/ngnative/components/ContextMenuGesture.kt +161 -0
  4. package/android/src/main/java/dev/ngnative/components/ContextMenuPopup.kt +197 -0
  5. package/android/src/main/java/dev/ngnative/components/MenuHost.kt +45 -0
  6. package/android/src/main/java/dev/ngnative/components/MenuWindowCallback.kt +76 -0
  7. package/android/src/main/java/dev/ngnative/components/MenusModule.kt +85 -0
  8. package/android/src/main/java/dev/ngnative/components/Shortcut.kt +144 -0
  9. package/android/src/main/java/dev/ngnative/components/WindowWrapper.kt +22 -0
  10. package/dist/activity-indicator.d.ts +4 -1
  11. package/dist/activity-indicator.js +6 -2
  12. package/dist/activity-indicator.js.map +1 -1
  13. package/dist/context-menu.d.ts +52 -0
  14. package/dist/context-menu.js +101 -0
  15. package/dist/context-menu.js.map +1 -0
  16. package/dist/drag-and-drop.d.ts +52 -0
  17. package/dist/drag-and-drop.js +112 -0
  18. package/dist/drag-and-drop.js.map +1 -0
  19. package/dist/gesture-backend.d.ts +32 -2
  20. package/dist/gesture-backend.js.map +1 -1
  21. package/dist/gesture-dispatch.d.ts +8 -1
  22. package/dist/gesture-dispatch.js +60 -16
  23. package/dist/gesture-dispatch.js.map +1 -1
  24. package/dist/gesture-interaction.d.ts +13 -0
  25. package/dist/gesture-interaction.js +97 -0
  26. package/dist/gesture-interaction.js.map +1 -0
  27. package/dist/gesture-tracking.d.ts +18 -0
  28. package/dist/gesture-tracking.js +28 -0
  29. package/dist/gesture-tracking.js.map +1 -0
  30. package/dist/gestures-web.d.ts +3 -1
  31. package/dist/gestures-web.js +5 -2
  32. package/dist/gestures-web.js.map +1 -1
  33. package/dist/gestures.d.ts +1 -0
  34. package/dist/gestures.js +45 -3
  35. package/dist/gestures.js.map +1 -1
  36. package/dist/image.d.ts +1 -1
  37. package/dist/index.d.ts +12 -1
  38. package/dist/index.js +12 -1
  39. package/dist/index.js.map +1 -1
  40. package/dist/interaction-observer.d.ts +11 -0
  41. package/dist/interaction-observer.js +12 -0
  42. package/dist/interaction-observer.js.map +1 -0
  43. package/dist/keyboard-avoiding-view.js +3 -3
  44. package/dist/keyboard-avoiding-view.js.map +1 -1
  45. package/dist/markdown-stream.d.ts +3 -1
  46. package/dist/markdown-stream.js +14 -2
  47. package/dist/markdown-stream.js.map +1 -1
  48. package/dist/markdown.d.ts +2 -0
  49. package/dist/markdown.js +5 -3
  50. package/dist/markdown.js.map +1 -1
  51. package/dist/menu.d.ts +71 -0
  52. package/dist/menu.js +193 -0
  53. package/dist/menu.js.map +1 -0
  54. package/dist/modal.d.ts +30 -1
  55. package/dist/modal.js +67 -6
  56. package/dist/modal.js.map +1 -1
  57. package/dist/native-button.d.ts +54 -0
  58. package/dist/native-button.js +100 -0
  59. package/dist/native-button.js.map +1 -0
  60. package/dist/native-gesture.d.ts +9 -1
  61. package/dist/native-gesture.js +32 -3
  62. package/dist/native-gesture.js.map +1 -1
  63. package/dist/native-id.d.ts +10 -0
  64. package/dist/native-id.js +70 -0
  65. package/dist/native-id.js.map +1 -0
  66. package/dist/pressable.d.ts +31 -0
  67. package/dist/pressable.js +102 -9
  68. package/dist/pressable.js.map +1 -1
  69. package/dist/safe-area-provider.js +15 -8
  70. package/dist/safe-area-provider.js.map +1 -1
  71. package/dist/scroll-view-props.d.ts +7 -1
  72. package/dist/scroll-view-props.js +54 -3
  73. package/dist/scroll-view-props.js.map +1 -1
  74. package/dist/section-list.js +2 -2
  75. package/dist/section-list.js.map +1 -1
  76. package/dist/shortcut.d.ts +21 -0
  77. package/dist/shortcut.js +34 -0
  78. package/dist/shortcut.js.map +1 -0
  79. package/dist/sidebar-item.d.ts +53 -0
  80. package/dist/sidebar-item.js +152 -0
  81. package/dist/sidebar-item.js.map +1 -0
  82. package/dist/sidebar-section.d.ts +21 -0
  83. package/dist/sidebar-section.js +74 -0
  84. package/dist/sidebar-section.js.map +1 -0
  85. package/dist/sidebar.d.ts +47 -0
  86. package/dist/sidebar.js +110 -0
  87. package/dist/sidebar.js.map +1 -0
  88. package/dist/split-view.d.ts +63 -0
  89. package/dist/split-view.js +121 -0
  90. package/dist/split-view.js.map +1 -0
  91. package/dist/switch.d.ts +9 -1
  92. package/dist/switch.js +23 -2
  93. package/dist/switch.js.map +1 -1
  94. package/dist/text-input.d.ts +8 -3
  95. package/dist/text-input.js +10 -4
  96. package/dist/text-input.js.map +1 -1
  97. package/dist/transforms.d.ts +5 -0
  98. package/dist/transforms.js +7 -0
  99. package/dist/transforms.js.map +1 -1
  100. package/dist/tray-icon.d.ts +18 -0
  101. package/dist/tray-icon.js +46 -0
  102. package/dist/tray-icon.js.map +1 -0
  103. package/dist/view-base.d.ts +42 -1
  104. package/dist/view-base.js +67 -39
  105. package/dist/view-base.js.map +1 -1
  106. package/dist/virtual-list.js +12 -1
  107. package/dist/virtual-list.js.map +1 -1
  108. package/dist/written-props.js +25 -0
  109. package/dist/written-props.js.map +1 -1
  110. package/expo-module.config.json +6 -0
  111. package/ios/NgNativeButtonComponentView.mm +132 -0
  112. package/ios/NgNativeButtonMetrics.h +28 -0
  113. package/ios/NgNativeButtonMetrics.mm +68 -0
  114. package/ios/NgNativeButtonShadowNode.h +65 -0
  115. package/ios/NgNativeButtonShadowNode.mm +40 -0
  116. package/ios/NgNativeComponents.podspec +28 -0
  117. package/ios/NgNativeDragAndDrop.mm +342 -0
  118. package/ios/NgNativeDropItems.h +29 -0
  119. package/ios/NgNativeDropItems.mm +55 -0
  120. package/ios/NgNativeMenuBuilding.h +40 -0
  121. package/ios/NgNativeMenuBuilding.mm +217 -0
  122. package/ios/NgNativeMenus.mm +320 -0
  123. package/ios/NgNativeResponderHooks.h +53 -0
  124. package/ios/NgNativeResponderHooks.mm +215 -0
  125. package/ios/NgNativeSceneKeys.h +36 -0
  126. package/ios/NgNativeSceneKeys.mm +56 -0
  127. package/ios/NgNativeSplitView.mm +433 -0
  128. package/ios/NgNativeSplitViewDelegate.h +29 -0
  129. package/ios/NgNativeSplitViewDelegate.mm +122 -0
  130. package/ios/NgNativeSplitViewShadowNodes.h +115 -0
  131. package/ios/NgNativeSplitViewShadowNodes.mm +31 -0
  132. package/ios/NgNativeUndo.mm +107 -0
  133. package/ios/NgNativeUndoSteps.h +17 -0
  134. package/ios/NgNativeUndoSteps.mm +22 -0
  135. package/ios/NgNativeViewBinder.h +54 -0
  136. package/ios/NgNativeViewBinder.mm +225 -0
  137. package/ios/NgNativeWindows.mm +1126 -0
  138. package/package.json +13 -5
@@ -0,0 +1,433 @@
1
+ #import <React/RCTBridgeModule.h>
2
+ #import <React/RCTComponentViewFactory.h>
3
+ #import <React/RCTViewComponentView.h>
4
+ #import <UIKit/UIKit.h>
5
+
6
+ #import "NgNativeSplitViewDelegate.h"
7
+ #import "NgNativeSplitViewShadowNodes.h"
8
+
9
+ #include <optional>
10
+
11
+ using namespace facebook::react;
12
+
13
+ /**
14
+ `<split-view>` on an iPad: a UISplitViewController with a sidebar column and a content column.
15
+
16
+ - `NgNativeSplitView` holds the controller, a child of the view controller the split view is in,
17
+ and puts its two Fabric children in the columns: `NgNativeSidebar` in the primary one, with the
18
+ sidebar's material behind it, and `NgNativeSplitContent` in the secondary one. Each column's
19
+ frame in the split view goes into its node's state, so Yoga lays out what is in it at that
20
+ size, and a measurement inside it counts the column's offset and its navigation bar.
21
+ - The sidebar's `collapsed` hides or shows the primary column, and `paneMinWidth` and
22
+ `paneMaxWidth` bound its width. The sidebar button, and the swipe that opens or shuts it, send
23
+ `topSidebarCollapse` to the sidebar; UIKit hiding the sidebar at a narrower width does not, and
24
+ it shows again at a wider one. `NgNativeSplitViewDelegate` tells the two apart.
25
+ - At a compact width (Slide Over, a narrow Split View) UIKit collapses the columns into a
26
+ navigation stack. Its root is the sidebar, unless the app had shown the content; the command
27
+ `showContent` pushes the content there and `showSidebar` pops it. Both columns stay mounted, so
28
+ what the content shows, the router's page among it, is kept as the width changes.
29
+ - The `NgNativeSplitViews` module's constant `available` is true on an iPad, where JavaScript then
30
+ names these views; an iPhone draws the split view from views.
31
+ */
32
+ @class NgNativeSplitPaneView;
33
+
34
+ /** A column's view controller: it hosts one pane, placed under the column's navigation bar. */
35
+ @interface NgNativeSplitColumn : UIViewController
36
+ @property (nonatomic, weak, nullable) NgNativeSplitPaneView *pane;
37
+ /** The split view's Fabric view, whose coordinates the pane's node is laid out in. */
38
+ @property (nonatomic, weak, nullable) UIView *splitView;
39
+ - (CGRect)paneArea;
40
+ - (void)host:(NgNativeSplitPaneView *)pane;
41
+ @end
42
+
43
+ /** A column's Fabric view, whose frame is the column's rather than Yoga's. */
44
+ @interface NgNativeSplitPaneView : RCTViewComponentView
45
+ @property (nonatomic, weak, nullable) NgNativeSplitColumn *column;
46
+ - (void)ngn_columnPlaced:(CGRect)frame;
47
+ @end
48
+
49
+ @interface NgNativeSplitViewComponentView : RCTViewComponentView
50
+ - (void)ngn_sidebarChanged;
51
+ @end
52
+
53
+ @interface NgNativeSidebarComponentView : NgNativeSplitPaneView
54
+ @property (nonatomic, weak, nullable) NgNativeSplitViewComponentView *splitView;
55
+ @property (nonatomic, readonly) const NgNativeSidebarProps &sidebarProps;
56
+ - (void)ngn_sendCollapsed:(BOOL)collapsed;
57
+ @end
58
+
59
+ @interface NgNativeSplitContentComponentView : NgNativeSplitPaneView
60
+ @end
61
+
62
+ #pragma mark - Columns
63
+
64
+ @implementation NgNativeSplitColumn
65
+
66
+ - (void)host:(NgNativeSplitPaneView *)pane
67
+ {
68
+ self.pane = pane;
69
+ pane.column = self;
70
+ [self.view addSubview:pane];
71
+ [self.view setNeedsLayout];
72
+ }
73
+
74
+ /**
75
+ Inside the column's safe area but for its bottom edge: below its navigation bar, and beside the
76
+ sidebar, which from iOS 26 floats over the content column rather than beside it.
77
+ */
78
+ - (CGRect)paneArea
79
+ {
80
+ CGRect bounds = self.view.bounds;
81
+ UIEdgeInsets insets = self.view.safeAreaInsets;
82
+ insets.bottom = 0;
83
+ CGRect area = UIEdgeInsetsInsetRect(bounds, insets);
84
+ return area.size.width > 0 && area.size.height > 0 ? area : bounds;
85
+ }
86
+
87
+ - (void)viewDidLayoutSubviews
88
+ {
89
+ [super viewDidLayoutSubviews];
90
+ NgNativeSplitPaneView *pane = self.pane;
91
+ if (pane == nil || pane.superview != self.view) {
92
+ return;
93
+ }
94
+ CGRect area = [self paneArea];
95
+ pane.frame = area;
96
+ // Where the split view's node lays the pane's out: across from the sidebar, below the bar.
97
+ UIView *splitView = self.splitView;
98
+ CGRect placed = area;
99
+ if (splitView != nil && splitView.window != nil && splitView.window == self.view.window) {
100
+ placed = [self.view convertRect:area toView:splitView];
101
+ }
102
+ [pane ngn_columnPlaced:placed];
103
+ id delegate = self.splitViewController.delegate;
104
+ if ([delegate isKindOfClass:[NgNativeSplitViewDelegate class]]) {
105
+ [(NgNativeSplitViewDelegate *)delegate columnLaidOut];
106
+ }
107
+ }
108
+
109
+ - (void)viewSafeAreaInsetsDidChange
110
+ {
111
+ [super viewSafeAreaInsetsDidChange];
112
+ [self.view setNeedsLayout];
113
+ }
114
+
115
+ @end
116
+
117
+ @implementation NgNativeSplitPaneView
118
+
119
+ + (BOOL)shouldBeRecycled
120
+ {
121
+ // The view is placed in a column controller, not in the view Fabric mounts it in.
122
+ return NO;
123
+ }
124
+
125
+ - (void)updateLayoutMetrics:(const LayoutMetrics &)layoutMetrics oldLayoutMetrics:(const LayoutMetrics &)oldLayoutMetrics
126
+ {
127
+ [super updateLayoutMetrics:layoutMetrics oldLayoutMetrics:oldLayoutMetrics];
128
+ NgNativeSplitColumn *column = self.column;
129
+ if (column != nil && self.superview == column.view) {
130
+ // The node has the column's frame in the split view; the view sits in the column itself.
131
+ self.frame = [column paneArea];
132
+ }
133
+ }
134
+
135
+ - (void)ngn_columnPlaced:(CGRect)frame
136
+ {
137
+ }
138
+
139
+ @end
140
+
141
+ #pragma mark - The sidebar and content columns
142
+
143
+ /**
144
+ A column's frame into its node's state, where it changed, its origin alone among it: Yoga then
145
+ lays the column out at its size, and the node sits at its origin. The state updates later, so a
146
+ frame is compared with the last one asked for, not the last committed: a column that goes from A
147
+ to B and back to A before B commits still asks for A, after B.
148
+ */
149
+ template <class StateT>
150
+ static void putColumnFrame(
151
+ const std::shared_ptr<const StateT> &state,
152
+ std::optional<facebook::react::Rect> &requested,
153
+ CGRect frame)
154
+ {
155
+ if (state == nullptr) {
156
+ return;
157
+ }
158
+ facebook::react::Rect next{
159
+ .origin = {.x = (Float)frame.origin.x, .y = (Float)frame.origin.y},
160
+ .size = {.width = (Float)frame.size.width, .height = (Float)frame.size.height}};
161
+ const auto &committed = state->getData();
162
+ if (next == requested.value_or(facebook::react::Rect{.origin = committed.origin, .size = committed.size})) {
163
+ return;
164
+ }
165
+ requested = next;
166
+ state->updateState(NgNativeSplitPaneState(next));
167
+ }
168
+
169
+ @implementation NgNativeSidebarComponentView {
170
+ NgNativeSidebarShadowNode::ConcreteState::Shared _state;
171
+ std::optional<facebook::react::Rect> _requestedFrame;
172
+ }
173
+
174
+ + (void)load
175
+ {
176
+ [[RCTComponentViewFactory currentComponentViewFactory] registerComponentViewClass:self];
177
+ }
178
+
179
+ + (ComponentDescriptorProvider)componentDescriptorProvider
180
+ {
181
+ return concreteComponentDescriptorProvider<NgNativeSidebarComponentDescriptor>();
182
+ }
183
+
184
+ - (instancetype)initWithFrame:(CGRect)frame
185
+ {
186
+ if (self = [super initWithFrame:frame]) {
187
+ _props = NgNativeSidebarShadowNode::defaultSharedProps();
188
+ }
189
+ return self;
190
+ }
191
+
192
+ - (const NgNativeSidebarProps &)sidebarProps
193
+ {
194
+ return static_cast<const NgNativeSidebarProps &>(*_props);
195
+ }
196
+
197
+ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps
198
+ {
199
+ [super updateProps:props oldProps:oldProps];
200
+ [self.splitView ngn_sidebarChanged];
201
+ }
202
+
203
+ - (void)updateState:(const State::Shared &)state oldState:(const State::Shared &)oldState
204
+ {
205
+ _state = std::static_pointer_cast<const NgNativeSidebarShadowNode::ConcreteState>(state);
206
+ }
207
+
208
+ - (void)ngn_columnPlaced:(CGRect)frame
209
+ {
210
+ putColumnFrame(_state, _requestedFrame, frame);
211
+ }
212
+
213
+ - (void)ngn_sendCollapsed:(BOOL)collapsed
214
+ {
215
+ if (_eventEmitter) {
216
+ std::static_pointer_cast<const NgNativeSidebarEventEmitter>(_eventEmitter)->onSidebarCollapse(collapsed);
217
+ }
218
+ }
219
+
220
+ @end
221
+
222
+ @implementation NgNativeSplitContentComponentView {
223
+ NgNativeSplitContentShadowNode::ConcreteState::Shared _state;
224
+ std::optional<facebook::react::Rect> _requestedFrame;
225
+ }
226
+
227
+ + (void)load
228
+ {
229
+ [[RCTComponentViewFactory currentComponentViewFactory] registerComponentViewClass:self];
230
+ }
231
+
232
+ + (ComponentDescriptorProvider)componentDescriptorProvider
233
+ {
234
+ return concreteComponentDescriptorProvider<NgNativeSplitContentComponentDescriptor>();
235
+ }
236
+
237
+ - (instancetype)initWithFrame:(CGRect)frame
238
+ {
239
+ if (self = [super initWithFrame:frame]) {
240
+ _props = NgNativeSplitContentShadowNode::defaultSharedProps();
241
+ }
242
+ return self;
243
+ }
244
+
245
+ - (void)updateState:(const State::Shared &)state oldState:(const State::Shared &)oldState
246
+ {
247
+ _state = std::static_pointer_cast<const NgNativeSplitContentShadowNode::ConcreteState>(state);
248
+ }
249
+
250
+ - (void)ngn_columnPlaced:(CGRect)frame
251
+ {
252
+ putColumnFrame(_state, _requestedFrame, frame);
253
+ }
254
+
255
+ @end
256
+
257
+ #pragma mark - The split view
258
+
259
+ @implementation NgNativeSplitViewComponentView {
260
+ UISplitViewController *_controller;
261
+ NgNativeSplitColumn *_sidebarColumn;
262
+ NgNativeSplitColumn *_contentColumn;
263
+ NgNativeSplitViewDelegate *_delegate;
264
+ __weak NgNativeSidebarComponentView *_sidebar;
265
+ }
266
+
267
+ + (void)load
268
+ {
269
+ [[RCTComponentViewFactory currentComponentViewFactory] registerComponentViewClass:self];
270
+ }
271
+
272
+ + (ComponentDescriptorProvider)componentDescriptorProvider
273
+ {
274
+ return concreteComponentDescriptorProvider<NgNativeSplitViewComponentDescriptor>();
275
+ }
276
+
277
+ + (BOOL)shouldBeRecycled
278
+ {
279
+ return NO;
280
+ }
281
+
282
+ - (instancetype)initWithFrame:(CGRect)frame
283
+ {
284
+ if (self = [super initWithFrame:frame]) {
285
+ _props = NgNativeSplitViewShadowNode::defaultSharedProps();
286
+ _controller = [[UISplitViewController alloc] initWithStyle:UISplitViewControllerStyleDoubleColumn];
287
+ _delegate = [[NgNativeSplitViewDelegate alloc] initWithController:_controller];
288
+ __weak NgNativeSplitViewComponentView *weakSelf = self;
289
+ _delegate.onCollapse = ^(BOOL collapsed) {
290
+ NgNativeSplitViewComponentView *strongSelf = weakSelf;
291
+ if (strongSelf != nil) {
292
+ [strongSelf->_sidebar ngn_sendCollapsed:collapsed];
293
+ }
294
+ };
295
+ _controller.delegate = _delegate;
296
+ _controller.primaryBackgroundStyle = UISplitViewControllerBackgroundStyleSidebar;
297
+ _controller.preferredSplitBehavior = UISplitViewControllerSplitBehaviorTile;
298
+ _controller.preferredDisplayMode = UISplitViewControllerDisplayModeOneBesideSecondary;
299
+ _sidebarColumn = [NgNativeSplitColumn new];
300
+ _sidebarColumn.splitView = self;
301
+ _contentColumn = [NgNativeSplitColumn new];
302
+ _contentColumn.splitView = self;
303
+ [_controller setViewController:_sidebarColumn forColumn:UISplitViewControllerColumnPrimary];
304
+ [_controller setViewController:_contentColumn forColumn:UISplitViewControllerColumnSecondary];
305
+ self.contentView = _controller.view;
306
+ }
307
+ return self;
308
+ }
309
+
310
+ #pragma mark Containment
311
+
312
+ - (void)didMoveToWindow
313
+ {
314
+ [super didMoveToWindow];
315
+ if (self.window == nil) {
316
+ [self ngn_leaveParent];
317
+ return;
318
+ }
319
+ UIViewController *parent = [self ngn_parentController];
320
+ if (parent == nil || _controller.parentViewController == parent) {
321
+ return;
322
+ }
323
+ [self ngn_leaveParent];
324
+ [parent addChildViewController:_controller];
325
+ [_controller didMoveToParentViewController:parent];
326
+ }
327
+
328
+ - (void)ngn_leaveParent
329
+ {
330
+ if (_controller.parentViewController == nil) {
331
+ return;
332
+ }
333
+ [_controller willMoveToParentViewController:nil];
334
+ [_controller removeFromParentViewController];
335
+ }
336
+
337
+ /** The nearest view controller above this view, which the split view controller joins. */
338
+ - (nullable UIViewController *)ngn_parentController
339
+ {
340
+ for (UIResponder *responder = self.superview; responder != nil; responder = responder.nextResponder) {
341
+ if ([responder isKindOfClass:[UIViewController class]]) {
342
+ return (UIViewController *)responder;
343
+ }
344
+ }
345
+ return nil;
346
+ }
347
+
348
+ #pragma mark Children
349
+
350
+ - (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
351
+ {
352
+ if ([childComponentView isKindOfClass:[NgNativeSidebarComponentView class]]) {
353
+ NgNativeSidebarComponentView *sidebar = (NgNativeSidebarComponentView *)childComponentView;
354
+ _sidebar = sidebar;
355
+ sidebar.splitView = self;
356
+ [_sidebarColumn host:sidebar];
357
+ [self ngn_sidebarChanged];
358
+ return;
359
+ }
360
+ if ([childComponentView isKindOfClass:[NgNativeSplitContentComponentView class]]) {
361
+ [_contentColumn host:(NgNativeSplitPaneView *)childComponentView];
362
+ return;
363
+ }
364
+ [super mountChildComponentView:childComponentView index:index];
365
+ }
366
+
367
+ - (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
368
+ {
369
+ if ([childComponentView isKindOfClass:[NgNativeSplitPaneView class]]) {
370
+ NgNativeSplitPaneView *pane = (NgNativeSplitPaneView *)childComponentView;
371
+ if (pane.column.pane == pane) {
372
+ pane.column.pane = nil;
373
+ }
374
+ pane.column = nil;
375
+ if (pane == _sidebar) {
376
+ _sidebar.splitView = nil;
377
+ _sidebar = nil;
378
+ }
379
+ [pane removeFromSuperview];
380
+ return;
381
+ }
382
+ [super unmountChildComponentView:childComponentView index:index];
383
+ }
384
+
385
+ #pragma mark The sidebar's props
386
+
387
+ - (void)ngn_sidebarChanged
388
+ {
389
+ NgNativeSidebarComponentView *sidebar = _sidebar;
390
+ if (sidebar == nil) {
391
+ return;
392
+ }
393
+ const auto &props = sidebar.sidebarProps;
394
+ _controller.minimumPrimaryColumnWidth =
395
+ props.paneMinWidth > 0 ? props.paneMinWidth : UISplitViewControllerAutomaticDimension;
396
+ _controller.maximumPrimaryColumnWidth =
397
+ props.paneMaxWidth > 0 ? props.paneMaxWidth : UISplitViewControllerAutomaticDimension;
398
+ [_delegate setCollapsed:props.collapsed];
399
+ }
400
+
401
+ #pragma mark Commands
402
+
403
+ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args
404
+ {
405
+ if ([commandName isEqualToString:@"showContent"]) {
406
+ [_delegate showContent];
407
+ } else if ([commandName isEqualToString:@"showSidebar"]) {
408
+ [_delegate showSidebar];
409
+ }
410
+ }
411
+
412
+ @end
413
+
414
+ #pragma mark - The module
415
+
416
+ @interface NgNativeSplitViews : NSObject <RCTBridgeModule>
417
+ @end
418
+
419
+ @implementation NgNativeSplitViews
420
+
421
+ RCT_EXPORT_MODULE()
422
+
423
+ + (BOOL)requiresMainQueueSetup
424
+ {
425
+ return YES;
426
+ }
427
+
428
+ - (NSDictionary *)constantsToExport
429
+ {
430
+ return @{@"available" : @(UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)};
431
+ }
432
+
433
+ @end
@@ -0,0 +1,29 @@
1
+ // The delegate of `<split-view>`'s UISplitViewController on an iPad, apart from the component view
2
+ // so the unit tests reach it without React Native: which column a collapse leaves on top, which
3
+ // display mode an expansion goes back to, and which display mode changes are the user's.
4
+ #pragma once
5
+
6
+ #import <UIKit/UIKit.h>
7
+
8
+ NS_ASSUME_NONNULL_BEGIN
9
+
10
+ @interface NgNativeSplitViewDelegate : NSObject <UISplitViewControllerDelegate>
11
+
12
+ // Called when the user shuts (YES) or opens (NO) the sidebar, with its button or a swipe.
13
+ @property (nonatomic, copy, nullable) void (^onCollapse)(BOOL collapsed);
14
+
15
+ - (instancetype)initWithController:(UISplitViewController *)controller NS_DESIGNATED_INITIALIZER;
16
+ - (instancetype)init NS_UNAVAILABLE;
17
+
18
+ // The app's `collapsed`: hides or shows the primary column, and is kept as the size changes.
19
+ - (void)setCollapsed:(BOOL)collapsed;
20
+ // The `showContent` and `showSidebar` commands.
21
+ - (void)showContent;
22
+ - (void)showSidebar;
23
+ // A column was laid out: the controller's size is settled, so a display mode change at that width
24
+ // and size class is the user's, and one at another is UIKit fitting the columns to a new size.
25
+ - (void)columnLaidOut;
26
+
27
+ @end
28
+
29
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,122 @@
1
+ #import "NgNativeSplitViewDelegate.h"
2
+
3
+ @implementation NgNativeSplitViewDelegate {
4
+ __weak UISplitViewController *_controller;
5
+ // Whether the app has shown the content: the top of the stack the columns collapse into.
6
+ BOOL _contentShown;
7
+ // The collapsed state last applied or reported, so a change is sent once.
8
+ BOOL _collapsed;
9
+ // While the columns expand out of the stack: the sidebar showing then is no step back.
10
+ BOOL _expanding;
11
+ // The controller's width and size class when a column was last laid out.
12
+ CGFloat _settledWidth;
13
+ UIUserInterfaceSizeClass _settledSizeClass;
14
+ }
15
+
16
+ - (instancetype)initWithController:(UISplitViewController *)controller
17
+ {
18
+ if (self = [super init]) {
19
+ _controller = controller;
20
+ }
21
+ return self;
22
+ }
23
+
24
+ - (void)setCollapsed:(BOOL)collapsed
25
+ {
26
+ if (collapsed == _collapsed) {
27
+ return;
28
+ }
29
+ _collapsed = collapsed;
30
+ UISplitViewController *controller = _controller;
31
+ controller.preferredDisplayMode = collapsed ? UISplitViewControllerDisplayModeSecondaryOnly
32
+ : UISplitViewControllerDisplayModeOneBesideSecondary;
33
+ if (!controller.isCollapsed) {
34
+ collapsed ? [controller hideColumn:UISplitViewControllerColumnPrimary]
35
+ : [controller showColumn:UISplitViewControllerColumnPrimary];
36
+ }
37
+ }
38
+
39
+ - (void)showContent
40
+ {
41
+ _contentShown = YES;
42
+ UISplitViewController *controller = _controller;
43
+ if (controller.isCollapsed || controller.displayMode == UISplitViewControllerDisplayModeOneOverSecondary) {
44
+ [controller showColumn:UISplitViewControllerColumnSecondary];
45
+ }
46
+ }
47
+
48
+ - (void)showSidebar
49
+ {
50
+ _contentShown = NO;
51
+ UISplitViewController *controller = _controller;
52
+ if (controller.isCollapsed) {
53
+ [controller showColumn:UISplitViewControllerColumnPrimary];
54
+ }
55
+ }
56
+
57
+ - (void)columnLaidOut
58
+ {
59
+ UISplitViewController *controller = _controller;
60
+ _settledWidth = controller.view.bounds.size.width;
61
+ _settledSizeClass = controller.traitCollection.horizontalSizeClass;
62
+ }
63
+
64
+ #pragma mark UISplitViewControllerDelegate
65
+
66
+ - (void)splitViewController:(UISplitViewController *)svc willChangeToDisplayMode:(UISplitViewControllerDisplayMode)displayMode
67
+ {
68
+ if (svc.isCollapsed || displayMode == UISplitViewControllerDisplayModeAutomatic) {
69
+ return;
70
+ }
71
+ BOOL collapsed = displayMode == UISplitViewControllerDisplayModeSecondaryOnly;
72
+ if (collapsed == _collapsed) {
73
+ return;
74
+ }
75
+ // A narrower window hides the sidebar, and a wider one brings it back: UIKit's, not the user's.
76
+ if (svc.view.bounds.size.width != _settledWidth
77
+ || svc.traitCollection.horizontalSizeClass != _settledSizeClass) {
78
+ return;
79
+ }
80
+ // The user's choice, kept as the size changes, until the app says otherwise.
81
+ _collapsed = collapsed;
82
+ svc.preferredDisplayMode = collapsed ? UISplitViewControllerDisplayModeSecondaryOnly
83
+ : UISplitViewControllerDisplayModeOneBesideSecondary;
84
+ if (self.onCollapse) {
85
+ self.onCollapse(collapsed);
86
+ }
87
+ }
88
+
89
+ - (UISplitViewControllerColumn)splitViewController:(UISplitViewController *)svc
90
+ topColumnForCollapsingToProposedTopColumn:(UISplitViewControllerColumn)proposedTopColumn
91
+ {
92
+ return _contentShown ? UISplitViewControllerColumnSecondary : UISplitViewControllerColumnPrimary;
93
+ }
94
+
95
+ - (UISplitViewControllerDisplayMode)splitViewController:(UISplitViewController *)svc
96
+ displayModeForExpandingToProposedDisplayMode:(UISplitViewControllerDisplayMode)proposedDisplayMode
97
+ {
98
+ _expanding = YES;
99
+ return _collapsed ? UISplitViewControllerDisplayModeSecondaryOnly
100
+ : UISplitViewControllerDisplayModeOneBesideSecondary;
101
+ }
102
+
103
+ - (void)splitViewControllerDidExpand:(UISplitViewController *)svc
104
+ {
105
+ _expanding = NO;
106
+ }
107
+
108
+ - (void)splitViewControllerDidCollapse:(UISplitViewController *)svc
109
+ {
110
+ _expanding = NO;
111
+ }
112
+
113
+ - (void)splitViewController:(UISplitViewController *)svc willShowColumn:(UISplitViewControllerColumn)column
114
+ {
115
+ // Back from the content, in the stack the columns collapsed into. Expanding shows the sidebar
116
+ // too, while the controller still says it is collapsed, and keeps what the app had shown.
117
+ if (svc.isCollapsed && !_expanding && column == UISplitViewControllerColumnPrimary) {
118
+ _contentShown = NO;
119
+ }
120
+ }
121
+
122
+ @end
@@ -0,0 +1,115 @@
1
+ // `<split-view>` on an iPad: the C++ side of the three Fabric components the host draws it with.
2
+ // `NgNativeSplitView` is the UISplitViewController; `NgNativeSidebar` (props `collapsed`,
3
+ // `paneMinWidth`, `paneMaxWidth`, the event `topSidebarCollapse`) and `NgNativeSplitContent` are
4
+ // its two columns. Each column's frame is the column controller's, which the view puts in the
5
+ // node's state: the descriptor gives Yoga its size, so what is inside a column lays out at the
6
+ // width UIKit gives it, and the node's layout takes its origin, so a measurement of what is inside
7
+ // a column counts where the column is.
8
+ #pragma once
9
+
10
+ #include <react/renderer/components/view/ConcreteViewShadowNode.h>
11
+ #include <react/renderer/components/view/ViewEventEmitter.h>
12
+ #include <react/renderer/components/view/ViewProps.h>
13
+ #include <react/renderer/components/view/YogaLayoutableShadowNode.h>
14
+ #include <react/renderer/core/ConcreteComponentDescriptor.h>
15
+ #include <react/renderer/core/LayoutContext.h>
16
+ #include <react/renderer/core/PropsParserContext.h>
17
+
18
+ namespace facebook::react {
19
+
20
+ extern const char NgNativeSplitViewComponentName[];
21
+ extern const char NgNativeSidebarComponentName[];
22
+ extern const char NgNativeSplitContentComponentName[];
23
+
24
+ // A column's frame in the split view, as UIKit laid it out: zero until the view has been placed in
25
+ // its column.
26
+ class NgNativeSplitPaneState final {
27
+ public:
28
+ NgNativeSplitPaneState() = default;
29
+ explicit NgNativeSplitPaneState(Rect frame) : origin(frame.origin), size(frame.size) {}
30
+
31
+ Point origin{};
32
+ Size size{};
33
+ };
34
+
35
+ // The split view's and the content column's props, which are a view's. A type of their own, as
36
+ // React Native asks of a view subclass.
37
+ class NgNativeSplitProps final : public ViewProps {
38
+ public:
39
+ using ViewProps::ViewProps;
40
+ };
41
+
42
+ class NgNativeSidebarProps final : public ViewProps {
43
+ public:
44
+ NgNativeSidebarProps() = default;
45
+ NgNativeSidebarProps(
46
+ const PropsParserContext &context,
47
+ const NgNativeSidebarProps &sourceProps,
48
+ const RawProps &rawProps);
49
+
50
+ bool collapsed{false};
51
+ // In points; zero leaves the bound to UIKit.
52
+ Float paneMinWidth{0};
53
+ Float paneMaxWidth{0};
54
+ };
55
+
56
+ class NgNativeSidebarEventEmitter : public ViewEventEmitter {
57
+ public:
58
+ using ViewEventEmitter::ViewEventEmitter;
59
+
60
+ void onSidebarCollapse(bool collapsed) const;
61
+ };
62
+
63
+ using NgNativeSplitViewShadowNode = ConcreteViewShadowNode<NgNativeSplitViewComponentName, NgNativeSplitProps>;
64
+
65
+ // A column's node, which Yoga sizes and which sits where UIKit put the column. The origin is set on
66
+ // the frame, not as Yoga's `left` and `top`, which a right-to-left layout swaps.
67
+ template <const char *Name, class PropsT, class EventEmitterT>
68
+ class NgNativeSplitPaneShadowNode final
69
+ : public ConcreteViewShadowNode<Name, PropsT, EventEmitterT, NgNativeSplitPaneState> {
70
+ public:
71
+ using ConcreteViewShadowNode<Name, PropsT, EventEmitterT, NgNativeSplitPaneState>::ConcreteViewShadowNode;
72
+
73
+ void layout(LayoutContext layoutContext) override
74
+ {
75
+ const auto &column = this->getStateData();
76
+ if (column.size.width > 0 && column.size.height > 0) {
77
+ auto metrics = this->getLayoutMetrics();
78
+ metrics.frame.origin = column.origin;
79
+ this->setLayoutMetrics(metrics);
80
+ }
81
+ ConcreteViewShadowNode<Name, PropsT, EventEmitterT, NgNativeSplitPaneState>::layout(layoutContext);
82
+ }
83
+ };
84
+
85
+ using NgNativeSidebarShadowNode =
86
+ NgNativeSplitPaneShadowNode<NgNativeSidebarComponentName, NgNativeSidebarProps, NgNativeSidebarEventEmitter>;
87
+
88
+ using NgNativeSplitContentShadowNode =
89
+ NgNativeSplitPaneShadowNode<NgNativeSplitContentComponentName, NgNativeSplitProps, ViewEventEmitter>;
90
+
91
+ // A column: once UIKit has sized it, that size is the node's, out of the split view's flow. Setting
92
+ // the size dirties the node, so a new frame, its origin alone among it, is laid out again.
93
+ template <class ShadowNodeT>
94
+ class NgNativeSplitPaneComponentDescriptor final : public ConcreteComponentDescriptor<ShadowNodeT> {
95
+ public:
96
+ using ConcreteComponentDescriptor<ShadowNodeT>::ConcreteComponentDescriptor;
97
+
98
+ void adopt(ShadowNode &shadowNode) const override
99
+ {
100
+ auto &layoutable = static_cast<YogaLayoutableShadowNode &>(shadowNode);
101
+ const auto &state = static_cast<const typename ShadowNodeT::ConcreteState &>(*shadowNode.getState());
102
+ Size size = state.getData().size;
103
+ if (size.width > 0 && size.height > 0) {
104
+ layoutable.setSize(size);
105
+ layoutable.setPositionType(YGPositionTypeAbsolute);
106
+ }
107
+ ConcreteComponentDescriptor<ShadowNodeT>::adopt(shadowNode);
108
+ }
109
+ };
110
+
111
+ using NgNativeSplitViewComponentDescriptor = ConcreteComponentDescriptor<NgNativeSplitViewShadowNode>;
112
+ using NgNativeSidebarComponentDescriptor = NgNativeSplitPaneComponentDescriptor<NgNativeSidebarShadowNode>;
113
+ using NgNativeSplitContentComponentDescriptor = NgNativeSplitPaneComponentDescriptor<NgNativeSplitContentShadowNode>;
114
+
115
+ } // namespace facebook::react