Skip to content

Email for professionals and teams

Make the "g" navigation shortcuts match reliably (#89)

Can K.c105dedd436b
diff --git a/src/components/shortcuts/use-hotkeys-utils.ts b/src/components/shortcuts/use-hotkeys-utils.ts
new file mode 100644
index 0000000..3df80e7
--- /dev/null
+++ b/src/components/shortcuts/use-hotkeys-utils.ts
@@ -0,0 +1,20 @@
+import type { ShortcutDefinition } from "./types";
+
+const MODIFIER_KEYS = new Set(["shift", "control", "alt", "meta"]);
+
+export function isModifierKey(key: string): boolean {
+ return MODIFIER_KEYS.has(key);
+}
+
+export function findSequenceMatch(
+ buffer: string[],
+ shortcuts: ShortcutDefinition[],
+): ShortcutDefinition | undefined {
+ for (let start = 0; start < buffer.length; start++) {
+ if (buffer.length > 1 && start === buffer.length - 1) break;
+ const sequence = buffer.slice(start).join(" ");
+ const match = shortcuts.find((shortcut) => shortcut.key.toLowerCase() === sequence);
+ if (match) return match;
+ }
+ return undefined;
+}
diff --git a/src/components/shortcuts/use-hotkeys.ts b/src/components/shortcuts/use-hotkeys.ts
index baa1a50..1264df9 100644
--- a/src/components/shortcuts/use-hotkeys.ts
+++ b/src/components/shortcuts/use-hotkeys.ts
@@ -2,6 +2,7 @@
import { useEffect, useRef } from "react";
import type { ShortcutDefinition } from "./types";
+import { findSequenceMatch, isModifierKey } from "./use-hotkeys-utils";
interface UseHotkeysOptions {
enabled?: boolean;
@@ -92,6 +93,8 @@ export function useHotkeys(
return;
}
+ if (isModifierKey(key)) return;
+
// Handle sequence buffer
sequenceBufferRef.current.push(key);
@@ -103,10 +106,7 @@ export function useHotkeys(
sequenceBufferRef.current = [];
}, 800);
- const currentSequence = sequenceBufferRef.current.join(" ");
- const sequenceMatch = shortcutsRef.current.find(
- (s) => s.key.toLowerCase() === currentSequence
- );
+ const sequenceMatch = findSequenceMatch(sequenceBufferRef.current, shortcutsRef.current);
if (sequenceMatch) {
e.preventDefault();
diff --git a/tests/hotkey-sequences.test.mjs b/tests/hotkey-sequences.test.mjs
new file mode 100644
index 0000000..eadd9c5
--- /dev/null
+++ b/tests/hotkey-sequences.test.mjs
@@ -0,0 +1,45 @@
+import assert from "node:assert/strict";
+import { mkdtempSync, rmSync } from "node:fs";
+import { dirname, join } from "node:path";
+import test, { after } from "node:test";
+import { fileURLToPath, pathToFileURL } from "node:url";
+import { build } from "esbuild";
+
+const root = join(dirname(fileURLToPath(import.meta.url)), "..");
+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";`,
+ resolveDir: root,
+ sourcefile: "hotkey-sequences-entry.ts",
+ },
+ outfile: join(directory, "entry.mjs"),
+ bundle: true,
+ platform: "node",
+ format: "esm",
+ target: "node24",
+ tsconfig: join(root, "tsconfig.json"),
+ packages: "external",
+ logLevel: "silent",
+});
+const { findSequenceMatch, isModifierKey } = await import(pathToFileURL(join(directory, "entry.mjs")).href);
+
+const shortcuts = [{ key: "g i" }, { key: "g !" }, { key: "e" }];
+
+test("a sequence matches when it ends the buffer", () => {
+ assert.equal(findSequenceMatch(["g", "i"], shortcuts), shortcuts[0]);
+ assert.equal(findSequenceMatch(["j", "g", "i"], shortcuts), shortcuts[0]);
+ assert.equal(findSequenceMatch(["g", "!"], shortcuts), shortcuts[1]);
+});
+
+test("a lone key only matches when it is the whole buffer", () => {
+ assert.equal(findSequenceMatch(["e"], shortcuts), shortcuts[2]);
+ assert.equal(findSequenceMatch(["j", "e"], shortcuts), undefined);
+ assert.equal(findSequenceMatch([], shortcuts), undefined);
+});
+
+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);
+});