Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ It's also possible to obtain the `action` is a more React-ish, declarative fashi

### Props

#### `ref` (Android only)
#### `ref`

Ref to the menu component.
Ref to the menu component. Call `ref.current?.show()` to open the menu from code.

`show()` works on Android only. On iOS it does nothing (a warning is logged in development), because iOS has no public API to present a menu from code.

| Type | Required |
|------|----------|
Expand Down
10 changes: 6 additions & 4 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ export const App = () => {
</Pressable>
</View>
</MenuView>
<Button
title="Show Menu with ref (Android only)"
onPress={() => menuRef.current?.show()}
/>
{Platform.OS === "android" && (
<Button
title="Show Menu with ref (Android only)"
onPress={() => menuRef.current?.show()}
/>
)}
<MenuView
ref={menuRef}
title="Menu Title"
Expand Down
2 changes: 1 addition & 1 deletion src/UIMenuView.android.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const MenuComponent = forwardRef<MenuComponentRef, NativeMenuComponentProps>(
const command =
UIManager.getViewManagerConfig("MenuView").Commands.show;

UIManager.dispatchViewManagerCommand(node, command, undefined);
UIManager.dispatchViewManagerCommand(node, command, []);
}
}
},
Expand Down
30 changes: 29 additions & 1 deletion src/UIMenuView.ios.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
import type * as React from "react";
import { forwardRef, useImperativeHandle } from "react";
import UIMenuNativeComponent from "./NativeModuleSpecs/UIMenuNativeComponent";
import type { MenuComponentRef } from "./types";

export default UIMenuNativeComponent;
type NativeProps = Omit<
React.ComponentProps<typeof UIMenuNativeComponent>,
"ref"
>;

const MenuComponent = forwardRef<MenuComponentRef, NativeProps>(
(props, ref) => {
useImperativeHandle(
ref,
() => ({
show: () => {
if (__DEV__) {
console.warn(
"MenuView: show() is only supported on Android and does nothing on iOS.",
);
}
},
}),
[],
);

return <UIMenuNativeComponent {...props} />;
},
);

export default MenuComponent;
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ export type MenuComponentProps =
React.PropsWithChildren<MenuComponentPropsBase>;

export type MenuComponentRef = {
/**
* Opens the menu from code.
*
* Does nothing on iOS, which has no public API to present a menu from code
* (a warning is logged in development).
* @platform Android
*/
show: () => void;
};

Expand Down
Loading