blob: 03a842d3cc6ca49e001a4831d569366db4858fb8 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import React from 'dom-chef';
export function registerHotkey(hotkey: string, action: VoidFunction | string, {signal}: SignalAsOptions): void {
const element = typeof action === 'string'
? <a hidden href={action} data-hotkey={hotkey}/>
: <button hidden type="button" data-hotkey={hotkey} onClick={action}/>;
document.body.append(element);
signal?.addEventListener('abort', () => {
element.remove();
});
}
/** Safely add a hotkey to an element, preserving any existing ones and avoiding duplicates */
export function addHotkey(button: HTMLAnchorElement | HTMLButtonElement | undefined, hotkey: string): void {
if (button) {
const hotkeys = new Set(button.dataset.hotkey?.split(','));
hotkeys.add(hotkey);
button.dataset.hotkey = [...hotkeys].join(',');
}
}
|