@wordpress/compose
Version:
WordPress higher-order components (HOCs).
92 lines (90 loc) • 3.88 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/compose/src/hooks/use-keyboard-shortcut/index.js
var use_keyboard_shortcut_exports = {};
__export(use_keyboard_shortcut_exports, {
default: () => use_keyboard_shortcut_default
});
module.exports = __toCommonJS(use_keyboard_shortcut_exports);
var import_mousetrap = __toESM(require("mousetrap"));
var import_mousetrap_global_bind = require("mousetrap/plugins/global-bind/mousetrap-global-bind");
var import_element = require("@wordpress/element");
var import_keycodes = require("@wordpress/keycodes");
function useKeyboardShortcut(shortcuts, callback, {
bindGlobal = false,
eventName = "keydown",
isDisabled = false,
// This is important for performance considerations.
target
} = {}) {
const currentCallbackRef = (0, import_element.useRef)(callback);
(0, import_element.useEffect)(() => {
currentCallbackRef.current = callback;
}, [callback]);
(0, import_element.useEffect)(() => {
if (isDisabled) {
return;
}
const mousetrap = new import_mousetrap.default(
target && target.current ? target.current : (
// We were passing `document` here previously, so to successfully cast it to Element we must cast it first to `unknown`.
// Not sure if this is a mistake but it was the behavior previous to the addition of types so we're just doing what's
// necessary to maintain the existing behavior.
/** @type {Element} */
/** @type {unknown} */
document
)
);
const shortcutsArray = Array.isArray(shortcuts) ? shortcuts : [shortcuts];
shortcutsArray.forEach((shortcut) => {
const keys = shortcut.split("+");
const modifiers = new Set(
keys.filter((value) => value.length > 1)
);
const hasAlt = modifiers.has("alt");
const hasShift = modifiers.has("shift");
if ((0, import_keycodes.isAppleOS)() && (modifiers.size === 1 && hasAlt || modifiers.size === 2 && hasAlt && hasShift)) {
throw new Error(
`Cannot bind ${shortcut}. Alt and Shift+Alt modifiers are reserved for character input.`
);
}
const bindFn = bindGlobal ? "bindGlobal" : "bind";
mousetrap[bindFn](
shortcut,
(...args) => currentCallbackRef.current(...args),
eventName
);
});
return () => {
mousetrap.reset();
};
}, [shortcuts, bindGlobal, eventName, target, isDisabled]);
}
var use_keyboard_shortcut_default = useKeyboardShortcut;
//# sourceMappingURL=index.js.map