Introduce focus stack to handle hotkeys (#12166)
# Introduce focus stack to handle hotkeys This PR introduces a focus stack to track the order in which the elements are focused: - Each focused element has a unique focus id - When an element is focused, it is pushed on top of the stack - When an element loses focus, we remove it from the stack This focus stack is then used to determine which hotkeys are available. The previous implementation lead to many regressions because of race conditions, of wrong order of open and close operations and by overwriting previous states. This implementation should be way more robust than the previous one. The new api can be incrementally implemented since it preserves backwards compatibility by writing to the old hotkey scopes states. For now, it has been implemented on the modal components. To test this PR, verify that the shortcuts still work correctly, especially for the modal components.
This commit is contained in:
@ -9,7 +9,7 @@ import { CommandMenuHotkeyScope } from '@/command-menu/types/CommandMenuHotkeySc
|
||||
import { CommandMenuPages } from '@/command-menu/types/CommandMenuPages';
|
||||
import { contextStoreTargetedRecordsRuleComponentState } from '@/context-store/states/contextStoreTargetedRecordsRuleComponentState';
|
||||
import { useKeyboardShortcutMenu } from '@/keyboard-shortcut-menu/hooks/useKeyboardShortcutMenu';
|
||||
import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys';
|
||||
import { useGlobalHotkeys } from '@/ui/utilities/hotkey/hooks/useGlobalHotkeys';
|
||||
import { AppHotkeyScope } from '@/ui/utilities/hotkey/types/AppHotkeyScope';
|
||||
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
|
||||
import { isNonEmptyString } from '@sniptt/guards';
|
||||
@ -36,21 +36,23 @@ export const useCommandMenuHotKeys = () => {
|
||||
COMMAND_MENU_COMPONENT_INSTANCE_ID,
|
||||
);
|
||||
|
||||
useScopedHotkeys(
|
||||
useGlobalHotkeys(
|
||||
'ctrl+k,meta+k',
|
||||
() => {
|
||||
closeKeyboardShortcutMenu();
|
||||
toggleCommandMenu();
|
||||
},
|
||||
true,
|
||||
AppHotkeyScope.CommandMenu,
|
||||
[closeKeyboardShortcutMenu, toggleCommandMenu],
|
||||
);
|
||||
|
||||
useScopedHotkeys(
|
||||
useGlobalHotkeys(
|
||||
['/'],
|
||||
() => {
|
||||
openRecordsSearchPage();
|
||||
},
|
||||
false,
|
||||
AppHotkeyScope.KeyboardShortcutMenu,
|
||||
[openRecordsSearchPage],
|
||||
{
|
||||
@ -58,16 +60,17 @@ export const useCommandMenuHotKeys = () => {
|
||||
},
|
||||
);
|
||||
|
||||
useScopedHotkeys(
|
||||
useGlobalHotkeys(
|
||||
[Key.Escape],
|
||||
() => {
|
||||
goBackFromCommandMenu();
|
||||
},
|
||||
true,
|
||||
CommandMenuHotkeyScope.CommandMenuFocused,
|
||||
[goBackFromCommandMenu],
|
||||
);
|
||||
|
||||
useScopedHotkeys(
|
||||
useGlobalHotkeys(
|
||||
[Key.Backspace, Key.Delete],
|
||||
() => {
|
||||
if (isNonEmptyString(commandMenuSearch)) {
|
||||
@ -88,6 +91,7 @@ export const useCommandMenuHotKeys = () => {
|
||||
goBackFromCommandMenu();
|
||||
}
|
||||
},
|
||||
true,
|
||||
CommandMenuHotkeyScope.CommandMenuFocused,
|
||||
[
|
||||
commandMenuPage,
|
||||
|
||||
Reference in New Issue
Block a user