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 library to implement keyboard sequence bindings.
Import
import { KeyboardShortcuts } from '@wordpress/components';
Props
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
children | React.ReactNode | Elements to render, upon whom key events are to be monitored. | ||
shortcuts | Record< string, KeyboardShortcutProps[ 'callback' ] > | Yes | An object of shortcut bindings, where each key is a keyboard combination, the value of which is the callback to be invoked when the key combination is pressed. The value of each shortcut should be a consistent function reference, not an anonymous function. Otherwise, the callback will not be correctly unbound when the component unmounts. The @see {@link https://craig.is/killing/mice Mousetrap documentation} | |
bindGlobal | KeyboardShortcutProps[ 'bindGlobal' ] | By default, a callback will not be invoked if the key combination occurs in an editable field.
Pass Tip: If you need some but not all keyboard events to be observed globally,
simply render two distinct | ||
eventName | KeyboardShortcutProps[ 'eventName' ] | By default, a callback is invoked in response to the |
Examples
Default
const Default = ( props ) => (
<KeyboardShortcuts { ...props } />
);