Open History to see the last commit
1diff --git a/ios/NewArch/MenuView.mm b/ios/NewArch/MenuView.mm
2index a54e619eb39e3402fa63f2ee4f734063d4f8fc58..066b01259a3c8ad9532688606be3d29a94a8f7d4 100644
3--- a/ios/NewArch/MenuView.mm
4+++ b/ios/NewArch/MenuView.mm
5@@ -105,6 +105,27 @@ - (void)onOpenMenu {
6 NSMutableArray<NSDictionary *> *subactionsArray = [NSMutableArray arrayWithCapacity:actions.size()];
7 if (action.subactions.size() > 0) {
8 for (const MenuViewActionsSubactionsStruct &subaction : action.subactions) {
9+ NSMutableArray<NSDictionary *> *subSubactionsArray =
10+ [NSMutableArray arrayWithCapacity:subaction.subactions.size()];
11+ for (const MenuViewActionsSubactionsSubactionsStruct &subSubaction : subaction.subactions) {
12+ NSDictionary *subSubactionDict = @{
13+ @"id": [NSString stringWithUTF8String:subSubaction.id.c_str()],
14+ @"title": [NSString stringWithUTF8String:subSubaction.title.c_str()],
15+ @"titleColor": @(subSubaction.titleColor),
16+ @"subtitle": [NSString stringWithUTF8String:subSubaction.subtitle.c_str()],
17+ @"state": [NSString stringWithUTF8String:subSubaction.state.c_str()],
18+ @"image": [NSString stringWithUTF8String:subSubaction.image.c_str()],
19+ @"imageColor": @(subSubaction.imageColor),
20+ @"displayInline": @(subSubaction.displayInline),
21+ @"attributes": @{
22+ @"destructive": @(subSubaction.attributes.destructive),
23+ @"disabled": @(subSubaction.attributes.disabled),
24+ @"hidden": @(subSubaction.attributes.hidden),
25+ @"keepsMenuPresented": @(subSubaction.attributes.keepsMenuPresented),
26+ },
27+ };
28+ [subSubactionsArray addObject:subSubactionDict];
29+ }
30 NSDictionary *subactionDict = @{
31 @"id": [NSString stringWithUTF8String:subaction.id.c_str()],
32 @"title": [NSString stringWithUTF8String:subaction.title.c_str()],
33@@ -118,7 +139,9 @@ - (void)onOpenMenu {
34 @"destructive": @(subaction.attributes.destructive),
35 @"disabled": @(subaction.attributes.disabled),
36 @"hidden": @(subaction.attributes.hidden),
37+ @"keepsMenuPresented": @(subaction.attributes.keepsMenuPresented),
38 },
39+ @"subactions": subSubactionsArray,
40 };
41 [subactionsArray addObject:subactionDict];
42 }
43@@ -138,6 +161,7 @@ - (void)onOpenMenu {
44 @"destructive": @(action.attributes.destructive),
45 @"disabled": @(action.attributes.disabled),
46 @"hidden": @(action.attributes.hidden),
47+ @"keepsMenuPresented": @(action.attributes.keepsMenuPresented),
48 },
49 @"subactions": subactionsArray,
50 };
51diff --git a/ios/Shared/MenuViewImplementation.swift b/ios/Shared/MenuViewImplementation.swift
52index 5c4e0da4292b15d3a27b5ea1555f11452a470815..db134864676ed83dbcd895d7a1bde38e8037a005 100644
53--- a/ios/Shared/MenuViewImplementation.swift
54+++ b/ios/Shared/MenuViewImplementation.swift
55@@ -59,18 +59,43 @@ public class MenuViewImplementation: UIButton {
56 self.setup()
57 }
59+ // Presentation is tracked from the two delegate methods the class already
60+ // overrode. Overriding willDisplayMenuFor as well (even for bookkeeping)
61+ // shadows UIButton's own implementation and degrades the button-anchored
62+ // presentation into generic context-menu chrome — an empty header row with
63+ // a dismiss chevron appears above the actions.
64 public override func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
65+ // Flush updates deferred by the presented-guard before the action
66+ // provider snapshots self.menu (covers a stuck flag from an
67+ // interaction that never ended cleanly).
68+ if pendingMenu != nil {
69+ pendingMenu = nil
70+ isMenuPresented = false
71+ self.setup()
72+ }
73+ isMenuPresented = true
74 sendMenuOpen()
75 return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { [weak self] _ in
76 guard let self = self else { return nil }
77 return self.menu
78 }
79 }
80-
81+
82 public override func contextMenuInteraction(_ interaction: UIContextMenuInteraction, willEndFor configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionAnimating?) {
83 sendMenuClose()
84+ isMenuPresented = false
85+ if pendingMenu != nil {
86+ // Re-run the full assignment now that presentation is over, so
87+ // any props deferred by the presented-guard (menu contents, press
88+ // mode) land on the button.
89+ pendingMenu = nil
90+ self.setup()
91+ }
92 }
94+ private var isMenuPresented = false
95+ private var pendingMenu: UIMenu?
96+
97 func setup () {
98 let menu = UIMenu(title: _title,
99 identifier: nil,
100@@ -86,8 +111,98 @@ public class MenuViewImplementation: UIButton {
101 }
102 }
104+ if isMenuPresented {
105+ // An action fired with keepsMenuPresented leaves the menu on
106+ // screen, and reassigning self.menu while it is presented makes
107+ // UIKit dismiss it — the update triggered by a selection would
108+ // defeat the keep-presented attribute. Update the visible copy in
109+ // place instead and defer the button assignment to dismissal.
110+ pendingMenu = menu
111+ self.refreshPresentedMenu(menu)
112+ return
113+ }
114+
115 self.menu = menu
116 self.showsMenuAsPrimaryAction = !shouldOpenOnLongPress
117+ // In long-press mode the button must not intercept touches: as a
118+ // full-bounds contentView it sits IN FRONT of the React children of
119+ // the hosting component view, which would swallow their taps (e.g. a
120+ // row Pressable wrapped by MenuView). The context-menu interaction is
121+ // hosted by the superview instead (see updateLongPressInteraction) so
122+ // long-press and pointer secondary-click still present the menu, with
123+ // the wrapped content as the zoom preview.
124+ self.isUserInteractionEnabled = !shouldOpenOnLongPress
125+ self.updateLongPressInteraction()
126+ }
127+
128+ private func refreshPresentedMenu(_ menu: UIMenu) {
129+ // Tap mode presents through the button's built-in interaction,
130+ // long-press mode through the superview-hosted one; cover both (plus
131+ // the interaction added in init) and dedupe by identity.
132+ var candidates = self.interactions.compactMap { $0 as? UIContextMenuInteraction }
133+ if let builtin = self.contextMenuInteraction {
134+ candidates.append(builtin)
135+ }
136+ if let host = longPressInteraction {
137+ candidates.append(host)
138+ }
139+ var visited: Set<ObjectIdentifier> = []
140+ for interaction in candidates where visited.insert(ObjectIdentifier(interaction)).inserted {
141+ // The block receives whichever menu level is currently on screen —
142+ // the navigated submenu when the user picked inside one, not the
143+ // root. Swap in the matching node from the rebuilt tree (stable
144+ // identifiers from the JS action ids) so that level updates in
145+ // place; returning an unrelated menu instead makes UIKit render it
146+ // as navigation into a foreign menu, with a stale or blank
147+ // expanded-submenu header row above the actions. The root carries
148+ // an auto-generated identifier that never matches, so it falls
149+ // through to a children-only replacement.
150+ interaction.updateVisibleMenu { [weak self] visibleMenu in
151+ guard let self = self else { return visibleMenu }
152+ if let replacement = self.menuMatching(visibleMenu.identifier, in: menu) {
153+ return replacement
154+ }
155+ return visibleMenu.replacingChildren(menu.children)
156+ }
157+ }
158+ }
159+
160+ private func menuMatching(_ identifier: UIMenu.Identifier, in menu: UIMenu) -> UIMenu? {
161+ if menu.identifier == identifier {
162+ return menu
163+ }
164+ for element in menu.children {
165+ if let submenu = element as? UIMenu, let match = menuMatching(identifier, in: submenu) {
166+ return match
167+ }
168+ }
169+ return nil
170+ }
171+
172+ private var longPressInteraction: UIContextMenuInteraction?
173+
174+ public override func didMoveToSuperview() {
175+ super.didMoveToSuperview()
176+ self.updateLongPressInteraction()
177+ }
178+
179+ private func updateLongPressInteraction() {
180+ if shouldOpenOnLongPress, let host = self.superview {
181+ if let existing = longPressInteraction, existing.view === host {
182+ return
183+ }
184+ if let existing = longPressInteraction {
185+ existing.view?.removeInteraction(existing)
186+ }
187+ let interaction = UIContextMenuInteraction(delegate: self)
188+ host.addInteraction(interaction)
189+ longPressInteraction = interaction
190+ return
191+ }
192+ if let existing = longPressInteraction {
193+ existing.view?.removeInteraction(existing)
194+ longPressInteraction = nil
195+ }
196 }
198 public override func reactSetFrame(_ frame: CGRect) {
199diff --git a/ios/Shared/RCTMenuItem.swift b/ios/Shared/RCTMenuItem.swift
200index bb6bb2b7ad56135089f267587c974b166760539d..949b3f7ec49af7ba26d966a8923a51f619443d5a 100644
201--- a/ios/Shared/RCTMenuItem.swift
202+++ b/ios/Shared/RCTMenuItem.swift
203@@ -103,10 +103,18 @@ class RCTMenuAction {
204 subMenuActions.append(subaction.createUIMenuElement(handler))
205 }
206 var menu: UIMenu;
207+ // Stable identifiers let updateVisibleMenu match submenu nodes in
208+ // place, so a refresh while the menu is presented keeps the user's
209+ // current submenu level instead of popping back to the root.
210+ // Inline sections don't navigate, so they stay stock.
211+ let menuIdentifier = identifier.map { UIMenu.Identifier(rawValue: $0.rawValue) }
212 if self.displayInline {
213 menu = UIMenu(title: title, image: image, options: .displayInline, children: subMenuActions)
214 } else {
215- menu = UIMenu(title: title, image: image, children: subMenuActions)
216+ menu = UIMenu(title: title, image: image, identifier: menuIdentifier, children: subMenuActions)
217+ }
218+ if #available(iOS 15.0, *) {
219+ menu.subtitle = subtitle
220 }
222 if #available(iOS 16.0, *) {
223diff --git a/src/NativeModuleSpecs/UIMenuNativeComponent.ts b/src/NativeModuleSpecs/UIMenuNativeComponent.ts
224index e6509355275596c451f9d082223cc16bfe504e1a..c783cdaf73f5635cf9835824ca85fba3b46453b4 100644
225--- a/src/NativeModuleSpecs/UIMenuNativeComponent.ts
226+++ b/src/NativeModuleSpecs/UIMenuNativeComponent.ts
227@@ -13,6 +13,22 @@ import codegenNativeComponent from "react-native/Libraries/Utilities/codegenNati
228 types here, to avoid issues while `pod install` takes place.
229 */
231+type SubSubAction = {
232+ id?: string;
233+ title: string;
234+ titleColor?: Int32;
235+ subtitle?: string;
236+ state?: string;
237+ image?: string;
238+ imageColor?: Int32;
239+ displayInline?: boolean;
240+ attributes?: {
241+ destructive?: boolean;
242+ disabled?: boolean;
243+ hidden?: boolean;
244+ keepsMenuPresented?: boolean;
245+ };
246+};
247 type SubAction = {
248 id?: string;
249 title: string;
250@@ -26,7 +42,11 @@ type SubAction = {
251 destructive?: boolean;
252 disabled?: boolean;
253 hidden?: boolean;
254+ keepsMenuPresented?: boolean;
255 };
256+ // One extra nesting level (menu → submenu → nested submenu leaves); the
257+ // codegen structs can't recurse, so depth is capped explicitly.
258+ subactions?: Array<SubSubAction>;
259 };
260 type MenuAction = {
261 id?: string;
262@@ -41,6 +61,7 @@ type MenuAction = {
263 destructive?: boolean;
264 disabled?: boolean;
265 hidden?: boolean;
266+ keepsMenuPresented?: boolean;
267 };
268 subactions?: Array<SubAction>;
269 };