Skip to content

Commit c1fa01e

Browse files
committed
Fix for enable/disable shortcut keys globally
1 parent d3434ae commit c1fa01e

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

apps/webapp/app/components/primitives/ShortcutsProvider.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext, useContext, useState, type ReactNode } from "react";
1+
import { createContext, useContext, useState, useCallback, useMemo, type ReactNode } from "react";
22

33
type ShortcutsContextType = {
44
areShortcutsEnabled: boolean;
@@ -15,20 +15,19 @@ type ShortcutsProviderProps = {
1515
export function ShortcutsProvider({ children }: ShortcutsProviderProps) {
1616
const [areShortcutsEnabled, setAreShortcutsEnabled] = useState(true);
1717

18-
const disableShortcuts = () => setAreShortcutsEnabled(false);
19-
const enableShortcuts = () => setAreShortcutsEnabled(true);
20-
21-
return (
22-
<ShortcutsContext.Provider
23-
value={{
24-
areShortcutsEnabled,
25-
disableShortcuts,
26-
enableShortcuts,
27-
}}
28-
>
29-
{children}
30-
</ShortcutsContext.Provider>
18+
const disableShortcuts = useCallback(() => setAreShortcutsEnabled(false), []);
19+
const enableShortcuts = useCallback(() => setAreShortcutsEnabled(true), []);
20+
21+
const value = useMemo(
22+
() => ({
23+
areShortcutsEnabled,
24+
disableShortcuts,
25+
enableShortcuts,
26+
}),
27+
[areShortcutsEnabled, disableShortcuts, enableShortcuts]
3128
);
29+
30+
return <ShortcutsContext.Provider value={value}>{children}</ShortcutsContext.Provider>;
3231
}
3332

3433
const throwIfNoProvider = () => {

apps/webapp/app/hooks/useShortcutKeys.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type Shortcut = {
88
key: string;
99
modifiers?: Modifier[];
1010
enabledOnInputElements?: boolean;
11+
enabled?: boolean;
1112
};
1213

1314
export type ShortcutDefinition =
@@ -37,15 +38,20 @@ export function useShortcutKeys({
3738
shortcut && "mac" in shortcut ? (isMac ? shortcut.mac : shortcut.windows) : shortcut;
3839

3940
const keys = createKeysFromShortcut(relevantShortcut);
41+
42+
const isEnabled = !disabled && areShortcutsEnabled && relevantShortcut?.enabled !== false;
43+
4044
useHotkeys(
4145
keys,
4246
(event, hotkeysEvent) => {
4347
action(event);
4448
},
4549
{
46-
enabled: !disabled && areShortcutsEnabled,
47-
enableOnFormTags: enabledOnInputElements ?? relevantShortcut?.enabledOnInputElements,
48-
enableOnContentEditable: enabledOnInputElements ?? relevantShortcut?.enabledOnInputElements,
50+
enabled: isEnabled,
51+
enableOnFormTags:
52+
isEnabled && (enabledOnInputElements ?? relevantShortcut?.enabledOnInputElements),
53+
enableOnContentEditable:
54+
isEnabled && (enabledOnInputElements ?? relevantShortcut?.enabledOnInputElements),
4955
}
5056
);
5157
}

0 commit comments

Comments
 (0)