@wordpress/keyboard-shortcuts
Version:
Handling keyboard shortcuts.
26 lines • 709 B
JavaScript
/**
* WordPress dependencies
*/
import { createContext } from '@wordpress/element';
const globalShortcuts = new Set();
const globalListener = event => {
for (const keyboardShortcut of globalShortcuts) {
keyboardShortcut(event);
}
};
export const context = createContext({
add: shortcut => {
if (globalShortcuts.size === 0) {
document.addEventListener('keydown', globalListener);
}
globalShortcuts.add(shortcut);
},
delete: shortcut => {
globalShortcuts.delete(shortcut);
if (globalShortcuts.size === 0) {
document.removeEventListener('keydown', globalListener);
}
}
});
context.displayName = 'KeyboardShortcutsContext';
//# sourceMappingURL=context.js.map