Email for professionals and teams
fix(shortcuts): don't fire bare-key shortcuts for Cmd/Ctrl/Alt combos (#161)
yi111f297c25a2ba3
diff --git a/src/components/shortcuts/use-hotkeys-utils.ts b/src/components/shortcuts/use-hotkeys-utils.tsindex 3df80e7..f233110 100644--- a/src/components/shortcuts/use-hotkeys-utils.ts+++ b/src/components/shortcuts/use-hotkeys-utils.ts@@ -6,6 +6,12 @@ export function isModifierKey(key: string): boolean {return MODIFIER_KEYS.has(key);}+export function hasCommandModifier(+ e: Pick<KeyboardEvent, "metaKey" | "ctrlKey" | "altKey">,+): boolean {+ return e.metaKey || e.ctrlKey || e.altKey;+}+export function findSequenceMatch(buffer: string[],shortcuts: ShortcutDefinition[],diff --git a/src/components/shortcuts/use-hotkeys.ts b/src/components/shortcuts/use-hotkeys.tsindex db4d693..e3461e3 100644--- a/src/components/shortcuts/use-hotkeys.ts+++ b/src/components/shortcuts/use-hotkeys.ts@@ -2,7 +2,7 @@import { useEffect, useRef } from "react";import type { ShortcutDefinition } from "./types";-import { findSequenceMatch, isModifierKey } from "./use-hotkeys-utils";+import { findSequenceMatch, hasCommandModifier, isModifierKey } from "./use-hotkeys-utils";interface UseHotkeysOptions {enabled?: boolean;@@ -95,6 +95,11 @@ export function useHotkeys(if (isModifierKey(key)) return;+ // Cmd/Ctrl/Alt combos belong to the browser and the OS. Sequences and+ // bare-key shortcuts are matched on the bare key, so buffering "c" out of+ // Cmd+C would fire the compose shortcut instead of copying.+ if (hasCommandModifier(e)) return;+if (e.target instanceof Element && e.target.closest('[role="dialog"], [role="menu"], [role="listbox"]')) return;// Handle sequence bufferdiff --git a/tests/hotkey-sequences.test.mjs b/tests/hotkey-sequences.test.mjsindex eadd9c5..bb45c9c 100644--- a/tests/hotkey-sequences.test.mjs+++ b/tests/hotkey-sequences.test.mjs@@ -10,7 +10,7 @@ const directory = mkdtempSync(join(root, "node_modules", "mailflare-hotkeys-"));after(() => rmSync(directory, { recursive: true, force: true }));await build({stdin: {- contents: `export { findSequenceMatch, isModifierKey } from "./src/components/shortcuts/use-hotkeys-utils.ts";`,+ contents: `export { findSequenceMatch, hasCommandModifier, isModifierKey } from "./src/components/shortcuts/use-hotkeys-utils.ts";`,resolveDir: root,sourcefile: "hotkey-sequences-entry.ts",},@@ -23,7 +23,7 @@ await build({packages: "external",logLevel: "silent",});-const { findSequenceMatch, isModifierKey } = await import(pathToFileURL(join(directory, "entry.mjs")).href);+const { findSequenceMatch, hasCommandModifier, isModifierKey } = await import(pathToFileURL(join(directory, "entry.mjs")).href);const shortcuts = [{ key: "g i" }, { key: "g !" }, { key: "e" }];@@ -43,3 +43,10 @@ test("modifier keys are not part of a sequence", () => {for (const key of ["shift", "control", "alt", "meta"]) assert.equal(isModifierKey(key), true);assert.equal(isModifierKey("g"), false);});++test("a bare key is only handled when no command modifier is held", () => {+ assert.equal(hasCommandModifier({ metaKey: true, ctrlKey: false, altKey: false }), true);+ assert.equal(hasCommandModifier({ metaKey: false, ctrlKey: true, altKey: false }), true);+ assert.equal(hasCommandModifier({ metaKey: false, ctrlKey: false, altKey: true }), true);+ assert.equal(hasCommandModifier({ metaKey: false, ctrlKey: false, altKey: false }), false);+});