UNPKG

@wordpress/components

Version:
65 lines (64 loc) • 2.14 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let _wordpress_compose = require("@wordpress/compose"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/components/src/keyboard-shortcuts/index.tsx function KeyboardShortcut({ target, callback, shortcut, bindGlobal, eventName }) { (0, _wordpress_compose.useKeyboardShortcut)(shortcut, callback, { bindGlobal, target, eventName }); return null; } /** * `KeyboardShortcuts` is a component which handles keyboard sequences during the lifetime of the rendering element. * * When passed children, it will capture key events which occur on or within the children. If no children are passed, events are captured on the document. * * It uses the [Mousetrap](https://craig.is/killing/mice) library to implement keyboard sequence bindings. * * ```jsx * import { KeyboardShortcuts } from '@wordpress/components'; * import { useState } from '@wordpress/element'; * * const MyKeyboardShortcuts = () => { * const [ isAllSelected, setIsAllSelected ] = useState( false ); * const selectAll = () => { * setIsAllSelected( true ); * }; * * return ( * <div> * <KeyboardShortcuts * shortcuts={ { * 'mod+a': selectAll, * } } * /> * [cmd/ctrl + A] Combination pressed? { isAllSelected ? 'Yes' : 'No' } * </div> * ); * }; * ``` */ function KeyboardShortcuts({ children, shortcuts, bindGlobal, eventName }) { const target = (0, _wordpress_element.useRef)(null); const element = Object.entries(shortcuts ?? {}).map(([shortcut, callback]) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(KeyboardShortcut, { shortcut, callback, bindGlobal, eventName, target }, shortcut)); if (!_wordpress_element.Children.count(children)) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: element }); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { ref: target, children: [element, children] }); } exports.default = KeyboardShortcuts; //# sourceMappingURL=index.cjs.map