@react-md/menu
Version:
Create menus that auto-position themselves within the viewport and adhere to the accessibility guidelines
108 lines • 4.44 kB
JavaScript
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;
};
import { useState } from "react";
import { BELOW_INNER_LEFT_ANCHOR, containsElement } from "@react-md/utils";
import { useMenu } from "./useMenu";
import { noop } from "./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}
*/
export function useContextMenu(_a) {
if (_a === void 0) { _a = {}; }
var _b = _a.anchor, anchor = _b === void 0 ? 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 ? noop : _e, _f = _a.preventScroll, preventScroll = _f === void 0 ? true : _f, options = __rest(_a, ["anchor", "baseId", "menuLabel", "fixedPositionOptions", "onContextMenu", "preventScroll"]);
var _g = __read(useState(false), 2), visible = _g[0], setVisible = _g[1];
var _h = __read(useState({}), 2), coords = _h[0], setCoords = _h[1];
var _j = 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 &&
containsElement(menuNodeRef.current, event.target))) {
return;
}
event.preventDefault();
event.stopPropagation();
setCoords({
initialX: event.clientX,
initialY: event.clientY,
});
setVisible(true);
},
};
}
//# sourceMappingURL=useContextMenu.js.map