Skip to content

Commit

Permalink
fix: Fix error on autocomplete key presses (#517)
Browse files Browse the repository at this point in the history
Fixes #516.
  • Loading branch information
BYK committed Sep 10, 2024
1 parent 16d4282 commit 2fa4041
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"contextlines",
"Endcaps",
"fontsource",
"getsentry",
"iife",
"outro",
"pageload",
Expand Down
2 changes: 1 addition & 1 deletion packages/overlay/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion packages/overlay/src/components/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 15 additions & 10 deletions packages/overlay/src/lib/useKeyPress.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
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();
}
}

window.addEventListener('keyup', onKeyup);

return () => window.removeEventListener('keyup', onKeyup) as undefined;
}, [keys, action, propagate]);
}, [normalizedKey, modifiers, action, propagate]);
}

0 comments on commit 2fa4041

Please sign in to comment.