one
Version:
One is a React framework for web, iOS, and Android apps.
74 lines (71 loc) • 2.5 kB
JavaScript
import { useRef } from "react";
import { Pressable, TurboModuleRegistry, View, findNodeHandle, requireNativeComponent } from "react-native-web";
import { flattenMenuItems } from "./menuItems.mjs";
import { jsx } from "react/jsx-runtime";
const NativeContextTrigger = requireNativeComponent("OneNativeMenuTrigger");
let popupModule;
function popup() {
if (popupModule === void 0) {
popupModule = TurboModuleRegistry.get("OneNativeMenuPopup");
}
if (!popupModule) {
throw new Error("Menu needs an Android build");
}
return popupModule;
}
function MenuPresentation({ items, onAction, onValueChange, onPickerChange: _onPickerChange, primaryAction: _primaryAction, children, disabled = false, revision: _revision, menuOrder: _menuOrder, menuActionDismissBehavior: _menuActionDismissBehavior, presentation, ...viewProps }) {
const anchor = useRef(null);
const contextAnchor = useRef(null);
const nativeItems = flattenMenuItems(items);
if (nativeItems.some((item) => item.type === "picker")) {
throw new Error("Menu picker items are not supported on Android");
}
if (!onValueChange && nativeItems.some((item) => item.type === "toggle")) {
throw new Error("Menu with toggles requires onValueChange");
}
const open = async () => {
if (disabled) return;
const anchorTag = findNodeHandle(presentation === "contextMenu" ? contextAnchor.current : anchor.current);
if (anchorTag == null) throw new Error("Menu trigger has no native view");
const selection = await popup().show(anchorTag, nativeItems);
if (selection?.type === "action") onAction(selection.id);
if (selection?.type === "toggle") {
onValueChange?.(selection.id, selection.value, selection.sourceIndex);
}
};
if (presentation === "contextMenu") {
return /* @__PURE__ */ jsx(NativeContextTrigger, {
ref: contextAnchor,
collapsable: false,
...viewProps,
contextMenuEnabled: !disabled,
onNativeMenuLongPress: open,
children
});
}
return /* @__PURE__ */ jsx(Pressable, {
disabled,
onPress: open,
children: /* @__PURE__ */ jsx(View, {
ref: anchor,
collapsable: false,
...viewProps,
children
})
});
}
function Menu(props) {
return /* @__PURE__ */ jsx(MenuPresentation, {
...props,
presentation: "menu"
});
}
function ContextMenu(props) {
return /* @__PURE__ */ jsx(MenuPresentation, {
...props,
accessibilityLabel: props.accessibilityLabel ?? "",
presentation: "contextMenu"
});
}
export { ContextMenu, Menu };
//# sourceMappingURL=AndroidMenu.mjs.map