@wordpress/components
Version:
UI components for WordPress.
8 lines (7 loc) • 3.15 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/keyboard-shortcuts/index.tsx"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useRef, Children } from '@wordpress/element';\nimport { useKeyboardShortcut } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from \"react/jsx-runtime\";\nfunction KeyboardShortcut({\n target,\n callback,\n shortcut,\n bindGlobal,\n eventName\n}) {\n useKeyboardShortcut(shortcut, callback, {\n bindGlobal,\n target,\n eventName\n });\n return null;\n}\n\n/**\n * `KeyboardShortcuts` is a component which handles keyboard sequences during the lifetime of the rendering element.\n *\n * 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.\n *\n * It uses the [Mousetrap](https://craig.is/killing/mice) library to implement keyboard sequence bindings.\n *\n * ```jsx\n * import { KeyboardShortcuts } from '@wordpress/components';\n * import { useState } from '@wordpress/element';\n *\n * const MyKeyboardShortcuts = () => {\n * \tconst [ isAllSelected, setIsAllSelected ] = useState( false );\n * \tconst selectAll = () => {\n * \t\tsetIsAllSelected( true );\n * \t};\n *\n * \treturn (\n * \t\t<div>\n * \t\t\t<KeyboardShortcuts\n * \t\t\t\tshortcuts={ {\n * \t\t\t\t\t'mod+a': selectAll,\n * \t\t\t\t} }\n * \t\t\t/>\n * \t\t\t[cmd/ctrl + A] Combination pressed? { isAllSelected ? 'Yes' : 'No' }\n * \t\t</div>\n * \t);\n * };\n * ```\n */\nfunction KeyboardShortcuts({\n children,\n shortcuts,\n bindGlobal,\n eventName\n}) {\n const target = useRef(null);\n const element = Object.entries(shortcuts ?? {}).map(([shortcut, callback]) => /*#__PURE__*/_jsx(KeyboardShortcut, {\n shortcut: shortcut,\n callback: callback,\n bindGlobal: bindGlobal,\n eventName: eventName,\n target: target\n }, shortcut));\n\n // Render as non-visual if there are no children pressed. Keyboard\n // events will be bound to the document instead.\n if (!Children.count(children)) {\n return /*#__PURE__*/_jsx(_Fragment, {\n children: element\n });\n }\n return /*#__PURE__*/_jsxs(\"div\", {\n ref: target,\n children: [element, children]\n });\n}\nexport default KeyboardShortcuts;"],
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAiC;AACjC,qBAAoC;AAKpC,yBAAkE;AAClE,SAAS,iBAAiB;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAG;AACD,0CAAoB,UAAU,UAAU;AAAA,IACtC;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AACD,SAAO;AACT;AAgCA,SAAS,kBAAkB;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAG;AACD,QAAM,aAAS,uBAAO,IAAI;AAC1B,QAAM,UAAU,OAAO,QAAQ,aAAa,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,UAAU,QAAQ,MAAmB,uCAAAA,KAAK,kBAAkB;AAAA,IAChH;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAAG,QAAQ,CAAC;AAIZ,MAAI,CAAC,wBAAS,MAAM,QAAQ,GAAG;AAC7B,WAAoB,uCAAAA,KAAK,mBAAAC,UAAW;AAAA,MAClC,UAAU;AAAA,IACZ,CAAC;AAAA,EACH;AACA,SAAoB,uCAAAC,MAAM,OAAO;AAAA,IAC/B,KAAK;AAAA,IACL,UAAU,CAAC,SAAS,QAAQ;AAAA,EAC9B,CAAC;AACH;AACA,IAAO,6BAAQ;",
"names": ["_jsx", "_Fragment", "_jsxs"]
}