forked from refined-github/refined-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhotkey.tsx
More file actions
27 lines (21 loc) · 952 Bytes
/
hotkey.tsx
File metadata and controls
27 lines (21 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import React from 'dom-chef';
import {isMac} from './index.js';
export function registerHotkey(hotkey: string, functionOrUrl: VoidFunction | string, {signal}: SignalAsOptions = {}): void {
const element = typeof functionOrUrl === 'string'
? <a hidden href={functionOrUrl} data-hotkey={hotkey} />
: <button hidden type="button" data-hotkey={hotkey} onClick={functionOrUrl} />;
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(',');
}
}
// eslint-disable-next-line unicorn/prevent-abbreviations
export const modKey = isMac ? 'cmd' : 'ctrl';