mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-01 00:08:27 +01:00
4167a60588
Follows up on https://github.com/Unleash/unleash/pull/4853 to add Biome to the frontend as well. ![image](https://github.com/Unleash/unleash/assets/14320932/1906faf1-fc29-4172-a4d4-b2716d72cd65) Added a few `biome-ignore` to speed up the process but we may want to check and fix them in the future.
77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
import { useEffect, useMemo } from 'react';
|
|
import { useIsAppleDevice } from './useIsAppleDevice';
|
|
|
|
export const useKeyboardShortcut = (
|
|
{
|
|
key,
|
|
modifiers = [],
|
|
preventDefault = false,
|
|
}: {
|
|
key: string;
|
|
modifiers?: Array<'ctrl' | 'alt' | 'shift'>;
|
|
preventDefault?: boolean;
|
|
},
|
|
callback: () => void,
|
|
) => {
|
|
const isAppleDevice = useIsAppleDevice();
|
|
useEffect(() => {
|
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
if (key !== event.key) {
|
|
return;
|
|
}
|
|
if (modifiers.includes('ctrl')) {
|
|
if (isAppleDevice) {
|
|
if (!event.metaKey) {
|
|
return;
|
|
}
|
|
} else {
|
|
if (!event.ctrlKey) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
if (modifiers.includes('alt') && !event.altKey) {
|
|
return;
|
|
}
|
|
if (modifiers.includes('shift') && !event.shiftKey) {
|
|
return;
|
|
}
|
|
if (preventDefault) {
|
|
event.preventDefault();
|
|
}
|
|
|
|
callback();
|
|
};
|
|
|
|
window.addEventListener('keydown', onKeyDown);
|
|
|
|
return () => {
|
|
window.removeEventListener('keydown', onKeyDown);
|
|
};
|
|
}, [isAppleDevice, key, modifiers, preventDefault, callback]);
|
|
|
|
const formattedModifiers = useMemo(
|
|
() =>
|
|
modifiers.map(
|
|
(modifier) =>
|
|
({
|
|
ctrl: isAppleDevice ? '⌘' : 'Ctrl',
|
|
alt: 'Alt',
|
|
shift: 'Shift',
|
|
})[modifier],
|
|
),
|
|
[isAppleDevice, modifiers],
|
|
);
|
|
|
|
const hotkeyDescription = useMemo(
|
|
() =>
|
|
[
|
|
...formattedModifiers,
|
|
`${key[0].toUpperCase()}${key.slice(1)}`,
|
|
].join('+'),
|
|
[formattedModifiers, key],
|
|
);
|
|
|
|
return hotkeyDescription;
|
|
};
|