UNPKG

one

Version:

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

104 lines (102 loc) • 4 kB
"use strict"; 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_jsx_runtime = require("react/jsx-runtime"); var import_react = require("react"); var import_react_native = require("react-native"); var import_menuItems = require("./menuItems.native.js"); var NativeContextTrigger = (0, import_react_native.requireNativeComponent)("OneNativeMenuTrigger"); var 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(param) { var { items, onAction, onValueChange, onPickerChange: _onPickerChange, primaryAction: _primaryAction, children, disabled = false, revision: _revision, menuOrder: _menuOrder, menuActionDismissBehavior: _menuActionDismissBehavior, presentation, ...viewProps } = param; var anchor = (0, import_react.useRef)(null); var contextAnchor = (0, import_react.useRef)(null); var nativeItems = (0, import_menuItems.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 = (0, import_react_native.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__ */ (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) { var _props_accessibilityLabel; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(MenuPresentation, { ...props, accessibilityLabel: (_props_accessibilityLabel = props.accessibilityLabel) !== null && _props_accessibilityLabel !== void 0 ? _props_accessibilityLabel : "", presentation: "contextMenu" }); }