UNPKG

@wordpress/components

Version:
1 lines • 3.02 kB
{"version":3,"file":"index.cjs","names":["useRef","Children"],"sources":["../../src/keyboard-shortcuts/index.tsx"],"sourcesContent":["import { useRef, Children } from '@wordpress/element';\nimport { useKeyboardShortcut } from '@wordpress/compose';\nimport type { KeyboardShortcutProps, KeyboardShortcutsProps } from './types';\n\nfunction KeyboardShortcut( {\n\ttarget,\n\tcallback,\n\tshortcut,\n\tbindGlobal,\n\teventName,\n}: KeyboardShortcutProps ) {\n\tuseKeyboardShortcut( shortcut, callback, {\n\t\tbindGlobal,\n\t\ttarget,\n\t\teventName,\n\t} );\n\n\treturn 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\tchildren,\n\tshortcuts,\n\tbindGlobal,\n\teventName,\n}: KeyboardShortcutsProps ) {\n\tconst target = useRef< HTMLDivElement >( null );\n\n\tconst element = Object.entries( shortcuts ?? {} ).map(\n\t\t( [ shortcut, callback ] ) => (\n\t\t\t<KeyboardShortcut\n\t\t\t\tkey={ shortcut }\n\t\t\t\tshortcut={ shortcut }\n\t\t\t\tcallback={ callback }\n\t\t\t\tbindGlobal={ bindGlobal }\n\t\t\t\teventName={ eventName }\n\t\t\t\ttarget={ target }\n\t\t\t/>\n\t\t)\n\t);\n\n\t// Render as non-visual if there are no children pressed. Keyboard\n\t// events will be bound to the document instead.\n\tif ( ! Children.count( children ) ) {\n\t\treturn <>{ element }</>;\n\t}\n\n\treturn (\n\t\t<div ref={ target }>\n\t\t\t{ element }\n\t\t\t{ children }\n\t\t</div>\n\t);\n}\n\nexport default KeyboardShortcuts;\n"],"mappings":";;;;;;;;;AAIA,SAAS,iBAAkB,EAC1B,QACA,UACA,UACA,YACA,aAC0B;CAC1B,CAAA,GAAA,mBAAA,oBAAA,CAAqB,UAAU,UAAU;EACxC;EACA;EACA;CACD,CAAE;CAEF,OAAO;AACR;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,SAAS,kBAAmB,EAC3B,UACA,WACA,YACA,aAC2B;CAC3B,MAAM,UAAA,GAASA,mBAAAA,OAAAA,CAA0B,IAAK;CAE9C,MAAM,UAAU,OAAO,QAAS,aAAa,CAAC,CAAE,CAAC,CAAC,KAC/C,CAAE,UAAU,cACb,iBAAA,GAAA,kBAAA,IAAA,CAAC,kBAAD;EAEY;EACA;EACE;EACD;EACH;CACT,GANM,QAMN,CAEH;CAIA,IAAK,CAAEC,mBAAAA,SAAS,MAAO,QAAS,GAC/B,OAAO,iBAAA,GAAA,kBAAA,IAAA,CAAA,kBAAA,UAAA,EAAA,UAAI,QAAW,CAAA;CAGvB,OACC,iBAAA,GAAA,kBAAA,KAAA,CAAC,OAAD;EAAK,KAAM;EAAX,UAAA,CACG,SACA,QACE;;AAEP"}