diff --git a/.vscode/settings.json b/.vscode/settings.json index c918cba7..cd2ec916 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,6 +7,7 @@ "contextlines", "Endcaps", "fontsource", + "getsentry", "iife", "outro", "pageload", diff --git a/packages/overlay/src/App.tsx b/packages/overlay/src/App.tsx index a1266cdb..5c7a7be0 100644 --- a/packages/overlay/src/App.tsx +++ b/packages/overlay/src/App.tsx @@ -166,7 +166,7 @@ export default function App({ return { clearEvents, onEvent, onOpen, onClose, onNavigate, onToggle }; }, [integrations, navigate, sidecarUrl, contentTypeListeners]); - useKeyPress(['ctrlKey', 'F12'], eventHandlers.onToggle); + useKeyPress('F12', ['ctrlKey'], eventHandlers.onToggle); useEffect(() => { log('useEffect: Adding event listeners'); diff --git a/packages/overlay/src/components/Tabs.tsx b/packages/overlay/src/components/Tabs.tsx index eefc5a27..16010787 100644 --- a/packages/overlay/src/components/Tabs.tsx +++ b/packages/overlay/src/components/Tabs.tsx @@ -34,7 +34,7 @@ export default function Tabs({ tabs, nested, setOpen }: Props) { const navigate = useNavigate(); const location = useLocation(); - useKeyPress(['Escape'], () => { + useKeyPress('Escape', [], () => { if (setOpen && location.pathname.split('/').length === 2) { setOpen(false); } else { diff --git a/packages/overlay/src/lib/useKeyPress.ts b/packages/overlay/src/lib/useKeyPress.ts index 39a4b19a..d5dc9da2 100644 --- a/packages/overlay/src/lib/useKeyPress.ts +++ b/packages/overlay/src/lib/useKeyPress.ts @@ -1,23 +1,28 @@ import { useEffect } from 'react'; +type ModifierKeys = 'altKey' | 'ctrlKey' | 'shiftKey' | 'metaKey'; + /** * useKeyPress - * @param {string[]} keys - an array of keys to respond to, compared against event.key + * @param {string} key - The letter or name of the key to respond to -- this is normalized to lower case + * @param {ModifierKeys[]} modifiers - The modifiers that needs to be activated such as ctrlKey * @param {function} action - the action to perform on key press * @param {boolean} propagate - whether to stop event propagation (default is false) */ -export default function useKeyPress(keys: string[], action: () => void, propagate = false) { +export default function useKeyPress(key: string, modifiers: ModifierKeys[], action: () => void, propagate = false) { + const normalizedKey = key.toLowerCase(); useEffect(() => { - function onKeyup(e: KeyboardEvent) { + function onKeyup(evt: KeyboardEvent) { if (!propagate) { - e.stopPropagation(); + evt.stopPropagation(); + } + + // We don't respond to modifier-only key presses + if (!evt.key) { + return; } - if ( - keys.every((key: string) => - key in e ? e[key as keyof KeyboardEvent] : e.key.toLowerCase() === key.toLowerCase(), - ) - ) { + if (modifiers.every(key => evt[key]) && evt.key.toLowerCase() === normalizedKey) { action(); } } @@ -25,5 +30,5 @@ export default function useKeyPress(keys: string[], action: () => void, propagat window.addEventListener('keyup', onKeyup); return () => window.removeEventListener('keyup', onKeyup) as undefined; - }, [keys, action, propagate]); + }, [normalizedKey, modifiers, action, propagate]); }