fix(use-keyboard-shortcuts): guard against undefined e.key and shortcut.key

Some keyboard events (dead keys, modifier-only presses) have e.key
as undefined. Similarly, malformed shortcut definitions may lack a key.
Added early-return guards to prevent TypeError.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Saravanakumar D 2026-05-30 01:02:00 -07:00
parent 4df134ec96
commit 07c0d304bc
2 changed files with 3 additions and 1 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@bytelyst/use-keyboard-shortcuts", "name": "@bytelyst/use-keyboard-shortcuts",
"version": "0.1.7", "version": "0.1.8",
"type": "module", "type": "module",
"exports": { "exports": {
".": { ".": {

View File

@ -32,9 +32,11 @@ export function useKeyboardShortcuts(shortcuts: ShortcutDef[]): void {
} }
function handleKeyDown(e: KeyboardEvent) { function handleKeyDown(e: KeyboardEvent) {
if (!e.key) return;
const inInput = isInputElement(e.target); const inInput = isInputElement(e.target);
for (const shortcut of shortcuts) { for (const shortcut of shortcuts) {
if (!shortcut.key) continue;
const metaMatch = shortcut.meta ? e.metaKey || e.ctrlKey : !(e.metaKey || e.ctrlKey); const metaMatch = shortcut.meta ? e.metaKey || e.ctrlKey : !(e.metaKey || e.ctrlKey);
const shiftMatch = shortcut.shift ? e.shiftKey : !e.shiftKey; const shiftMatch = shortcut.shift ? e.shiftKey : !e.shiftKey;
const altMatch = shortcut.alt ? e.altKey : !e.altKey; const altMatch = shortcut.alt ? e.altKey : !e.altKey;