UNPKG

@react-md/menu

Version:

Create menus that auto-position themselves within the viewport and adhere to the accessibility guidelines

112 lines 4.62 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useContextMenu = void 0; var react_1 = require("react"); var utils_1 = require("@react-md/utils"); var useMenu_1 = require("./useMenu"); var utils_2 = require("./utils"); /** * This hook controls the visibility and positioning for a context menu. * * @example * Simple Example * ```tsx * import type { ReactElement } from "react"; * import [ Menu, MenuItem, useContextMenu ] from "@react-md/menu": * * function Example(): ReactElement { * const { menuProps, onContextMenu } = useContextMenu(); * * return ( * <div onContextMenu={onContextMenu}> * <textarea /> * <Menu {...menuProps}> * <MenuItem>Cut</MenuItem> * <MenuItem>Copy</MenuItem> * <MenuItem>Paste</MenuItem> * <MenuItem>Undo</MenuItem> * </Menu> * </div> * ); * } * ``` * * * @remarks \@since 5.0.0 * @param options - The {@link ContextMenuHookOptions} * @returns the {@link ContextMenuHookReturnValue} */ function useContextMenu(_a) { if (_a === void 0) { _a = {}; } var _b = _a.anchor, anchor = _b === void 0 ? utils_1.BELOW_INNER_LEFT_ANCHOR : _b, _c = _a.baseId, baseId = _c === void 0 ? "context-menu" : _c, _d = _a.menuLabel, menuLabel = _d === void 0 ? "Context Menu" : _d, fixedPositionOptions = _a.fixedPositionOptions, _e = _a.onContextMenu, onContextMenu = _e === void 0 ? utils_2.noop : _e, _f = _a.preventScroll, preventScroll = _f === void 0 ? true : _f, options = __rest(_a, ["anchor", "baseId", "menuLabel", "fixedPositionOptions", "onContextMenu", "preventScroll"]); var _g = __read((0, react_1.useState)(false), 2), visible = _g[0], setVisible = _g[1]; var _h = __read((0, react_1.useState)({}), 2), coords = _h[0], setCoords = _h[1]; var _j = (0, useMenu_1.useMenu)(__assign(__assign({}, options), { anchor: anchor, baseId: baseId, menuLabel: menuLabel, visible: visible, setVisible: setVisible, fixedPositionOptions: __assign(__assign({}, fixedPositionOptions), coords), preventScroll: preventScroll })), menuRef = _j.menuRef, menuProps = _j.menuProps, menuNodeRef = _j.menuNodeRef; return { menuRef: menuRef, menuProps: menuProps, menuNodeRef: menuNodeRef, visible: visible, setVisible: setVisible, setCoords: setCoords, onContextMenu: function (event) { onContextMenu(event); if (event.isPropagationStopped() || // make it so that if you right click the custom context menu, the // browser's default context menu can appear (mostly for being able to // inspect your custom context menu) /* istanbul ignore next */ (event.target instanceof HTMLElement && (0, utils_1.containsElement)(menuNodeRef.current, event.target))) { return; } event.preventDefault(); event.stopPropagation(); setCoords({ initialX: event.clientX, initialY: event.clientY, }); setVisible(true); }, }; } exports.useContextMenu = useContextMenu; //# sourceMappingURL=useContextMenu.js.map