From f9917c6aa897f5b1e8d4d50841b77c1a9f9ca70c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20V=C3=ADtor=20Ribeiro=20Martins?= Date: Sat, 1 Jul 2023 13:26:23 -0300 Subject: [PATCH 1/3] feat: avoid modifier keys and modifier key bindings to trigger input focus --- .../src/components/Command/Command.utils.tsx | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/ui/src/components/Command/Command.utils.tsx b/packages/ui/src/components/Command/Command.utils.tsx index ee020395eb9..809b4273c7c 100644 --- a/packages/ui/src/components/Command/Command.utils.tsx +++ b/packages/ui/src/components/Command/Command.utils.tsx @@ -408,14 +408,38 @@ export function useAutoInputFocus() { // Focus the input when typing from anywhere React.useEffect(() => { - function onKeyDown() { - input?.focus() + function isModifierNotShift(event: KeyboardEvent) { + return event.key.length !== 1 && event.key !== 'Shift' + } + + // Keep track of whether the modifier key (except shift) is pressed + let modifierPressed = false + + function onKeyDown(event: KeyboardEvent) { + // If the user is typing a modifier key, don't focus the input and set the modifierPressed flag + if (isModifierNotShift(event)) { + modifierPressed = true + } else { + if (!modifierPressed) { + // Focus the input if no modifier key is pressed (other than shift) + input?.focus() + } + } + } + function onKeyUp(event: KeyboardEvent) { + console.log('keyup') + if (isModifierNotShift(event)) { + // Reset the modifierPressed flag when the modifier key is released + modifierPressed = false + } } window.addEventListener('keydown', onKeyDown) + window.addEventListener('keyup', onKeyUp) return () => { window.removeEventListener('keydown', onKeyDown) + window.removeEventListener('keyup', onKeyUp) } }, [input]) From 02fd34ec3269d95460d457cdde01b062db43908b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20V=C3=ADtor=20Ribeiro=20Martins?= Date: Thu, 6 Jul 2023 19:39:59 -0300 Subject: [PATCH 2/3] feat: set modifiersKey to be alt, meta, crtl and tab --- packages/ui/src/components/Command/Command.utils.tsx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/ui/src/components/Command/Command.utils.tsx b/packages/ui/src/components/Command/Command.utils.tsx index 809b4273c7c..8bff8b8d96d 100644 --- a/packages/ui/src/components/Command/Command.utils.tsx +++ b/packages/ui/src/components/Command/Command.utils.tsx @@ -408,8 +408,10 @@ export function useAutoInputFocus() { // Focus the input when typing from anywhere React.useEffect(() => { - function isModifierNotShift(event: KeyboardEvent) { - return event.key.length !== 1 && event.key !== 'Shift' + function isModifierKey(event: KeyboardEvent) { + const modifierKeys = ['Alt', 'Control', 'Meta', 'Tab'] + const { key } = event + return modifierKeys.includes(key) } // Keep track of whether the modifier key (except shift) is pressed @@ -417,7 +419,7 @@ export function useAutoInputFocus() { function onKeyDown(event: KeyboardEvent) { // If the user is typing a modifier key, don't focus the input and set the modifierPressed flag - if (isModifierNotShift(event)) { + if (isModifierKey(event)) { modifierPressed = true } else { if (!modifierPressed) { @@ -428,7 +430,7 @@ export function useAutoInputFocus() { } function onKeyUp(event: KeyboardEvent) { console.log('keyup') - if (isModifierNotShift(event)) { + if (isModifierKey(event)) { // Reset the modifierPressed flag when the modifier key is released modifierPressed = false } From fee3b33d1d202723ec9a59ea71b059d46bf576b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20V=C3=ADtor=20Ribeiro=20Martins?= Date: Fri, 7 Jul 2023 12:43:48 -0300 Subject: [PATCH 3/3] feat: remove a lot of uneccesary code --- .../src/components/Command/Command.utils.tsx | 30 ++----------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/packages/ui/src/components/Command/Command.utils.tsx b/packages/ui/src/components/Command/Command.utils.tsx index 8bff8b8d96d..b916653ee7e 100644 --- a/packages/ui/src/components/Command/Command.utils.tsx +++ b/packages/ui/src/components/Command/Command.utils.tsx @@ -408,40 +408,16 @@ export function useAutoInputFocus() { // Focus the input when typing from anywhere React.useEffect(() => { - function isModifierKey(event: KeyboardEvent) { - const modifierKeys = ['Alt', 'Control', 'Meta', 'Tab'] - const { key } = event - return modifierKeys.includes(key) - } - - // Keep track of whether the modifier key (except shift) is pressed - let modifierPressed = false - - function onKeyDown(event: KeyboardEvent) { - // If the user is typing a modifier key, don't focus the input and set the modifierPressed flag - if (isModifierKey(event)) { - modifierPressed = true - } else { - if (!modifierPressed) { - // Focus the input if no modifier key is pressed (other than shift) - input?.focus() - } - } - } - function onKeyUp(event: KeyboardEvent) { - console.log('keyup') - if (isModifierKey(event)) { - // Reset the modifierPressed flag when the modifier key is released - modifierPressed = false + function onKeyDown(e: KeyboardEvent) { + if (!e.ctrlKey && !e.altKey && !e.metaKey && e.key !== 'Tab') { + input?.focus() } } window.addEventListener('keydown', onKeyDown) - window.addEventListener('keyup', onKeyUp) return () => { window.removeEventListener('keydown', onKeyDown) - window.removeEventListener('keyup', onKeyUp) } }, [input])