Skip to content

Commit e810321

Browse files
authored
fix: add Fabric Commands Support for Android MenuView Component (#1152)
* feat: add Fabric support for MenuView commands * refactor(UIMenuView): make Commands constant local
1 parent a89c9b5 commit e810321

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

android/src/main/java/com/reactnativemenu/MenuViewManagerBase.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,15 @@ abstract class MenuViewManagerBase : ReactClippingViewManager<MenuView>() {
233233
}
234234
}
235235

236+
override fun receiveCommand(root: MenuView, commandId: String, args: ReadableArray?) {
237+
when (commandId) {
238+
NEW_ARCH_COMMAND_SHOW -> root.show()
239+
}
240+
}
241+
236242
companion object {
237-
val COMMAND_SHOW = 1
243+
const val COMMAND_SHOW = 1
244+
const val NEW_ARCH_COMMAND_SHOW = "show"
238245
val SPACING_TYPES =
239246
arrayOf(
240247
Spacing.ALL,

src/UIMenuView.android.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@ import {
66
} from "react-native";
77
import type { MenuComponentRef, NativeMenuComponentProps } from "./types";
88
import { forwardRef, useImperativeHandle, useRef } from "react";
9+
import codegenNativeCommands from "react-native/Libraries/Utilities/codegenNativeCommands";
910

1011
const NativeMenuComponent = requireNativeComponent(
1112
"MenuView",
1213
) as HostComponent<NativeMenuComponentProps>;
1314

15+
type RefT = React.ElementRef<typeof NativeMenuComponent>;
16+
17+
const Commands = codegenNativeCommands<{
18+
show: (viewRef: RefT) => void;
19+
}>({
20+
supportedCommands: ["show"],
21+
});
22+
1423
const MenuComponent = forwardRef<MenuComponentRef, NativeMenuComponentProps>(
1524
(props, ref) => {
1625
const nativeRef = useRef(null);
@@ -20,11 +29,18 @@ const MenuComponent = forwardRef<MenuComponentRef, NativeMenuComponentProps>(
2029
() => ({
2130
show: () => {
2231
if (nativeRef.current) {
23-
const node = findNodeHandle(nativeRef.current);
24-
const command =
25-
UIManager.getViewManagerConfig("MenuView").Commands.show;
32+
// biome-ignore lint/suspicious/noExplicitAny: React Native runtime flags like nativeFabricUIManager are not in TypeScript types. Using `any` here is intentional and safe.
33+
const isFabric = !!(global as any).nativeFabricUIManager;
34+
35+
if (isFabric) {
36+
Commands.show(nativeRef.current);
37+
} else {
38+
const node = findNodeHandle(nativeRef.current);
39+
const command =
40+
UIManager.getViewManagerConfig("MenuView").Commands.show;
2641

27-
UIManager.dispatchViewManagerCommand(node, command, undefined);
42+
UIManager.dispatchViewManagerCommand(node, command, undefined);
43+
}
2844
}
2945
},
3046
}),

0 commit comments

Comments
 (0)