UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

80 lines (77 loc) • 2.87 kB
import { jsx } from "react/jsx-runtime"; import { useRef } from "react"; import { Pressable, TurboModuleRegistry, View, findNodeHandle, requireNativeComponent } from "react-native"; import { flattenMenuItems } from "./menuItems.native.js"; var NativeContextTrigger = requireNativeComponent("OneNativeMenuTrigger"); var 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(param) { var { items, onAction, onValueChange, onPickerChange: _onPickerChange, primaryAction: _primaryAction, children, disabled = false, revision: _revision, menuOrder: _menuOrder, menuActionDismissBehavior: _menuActionDismissBehavior, presentation, ...viewProps } = param; var anchor = useRef(null); var contextAnchor = useRef(null); var nativeItems = flattenMenuItems(items); if (nativeItems.some(function(item) { return item.type === "picker"; })) { throw new Error("Menu picker items are not supported on Android"); } if (!onValueChange && nativeItems.some(function(item) { return item.type === "toggle"; })) { throw new Error("Menu with toggles requires onValueChange"); } var open = async function open2() { if (disabled) return; var anchorTag = findNodeHandle(presentation === "contextMenu" ? contextAnchor.current : anchor.current); if (anchorTag == null) throw new Error("Menu trigger has no native view"); var selection = await popup().show(anchorTag, nativeItems); if ((selection === null || selection === void 0 ? void 0 : selection.type) === "action") onAction(selection.id); if ((selection === null || selection === void 0 ? void 0 : selection.type) === "toggle") { onValueChange === null || onValueChange === void 0 ? void 0 : 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) { var _props_accessibilityLabel; return /* @__PURE__ */ jsx(MenuPresentation, { ...props, accessibilityLabel: (_props_accessibilityLabel = props.accessibilityLabel) !== null && _props_accessibilityLabel !== void 0 ? _props_accessibilityLabel : "", presentation: "contextMenu" }); } export { ContextMenu, Menu }; //# sourceMappingURL=AndroidMenu.native.js.map