UNPKG

one

Version:

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

96 lines (95 loc) • 3.61 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var AndroidMenu_exports = {}; __export(AndroidMenu_exports, { ContextMenu: () => ContextMenu, Menu: () => Menu }); module.exports = __toCommonJS(AndroidMenu_exports); var import_react = require("react"); var import_react_native = require("react-native-web"); var import_menuItems = require("./menuItems.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); const NativeContextTrigger = (0, import_react_native.requireNativeComponent)("OneNativeMenuTrigger"); let popupModule; function popup() { if (popupModule === void 0) { popupModule = import_react_native.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 = (0, import_react.useRef)(null); const contextAnchor = (0, import_react.useRef)(null); const nativeItems = (0, import_menuItems.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 = (0, import_react_native.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__ */ (0, import_jsx_runtime.jsx)(NativeContextTrigger, { ref: contextAnchor, collapsable: false, ...viewProps, contextMenuEnabled: !disabled, onNativeMenuLongPress: open, children }); } return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.Pressable, { disabled, onPress: open, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_native.View, { ref: anchor, collapsable: false, ...viewProps, children }) }); } function Menu(props) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MenuPresentation, { ...props, presentation: "menu" }); } function ContextMenu(props) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MenuPresentation, { ...props, accessibilityLabel: props.accessibilityLabel ?? "", presentation: "contextMenu" }); }