mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-14 01:50:39 +09:00
Shows a beautiful categorized cheatsheet of all keyboard shortcuts (inbox, issue detail, global) when the user presses ? with keyboard shortcuts enabled. Respects text input focus detection — won't trigger in text fields. Uses the existing Dialog component and Radix UI. Co-Authored-By: Paperclip <noreply@paperclip.ing>
57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import { useEffect } from "react";
|
|
import { isKeyboardShortcutTextInputTarget } from "../lib/keyboardShortcuts";
|
|
|
|
interface ShortcutHandlers {
|
|
enabled?: boolean;
|
|
onNewIssue?: () => void;
|
|
onToggleSidebar?: () => void;
|
|
onTogglePanel?: () => void;
|
|
onShowShortcuts?: () => void;
|
|
}
|
|
|
|
export function useKeyboardShortcuts({
|
|
enabled = true,
|
|
onNewIssue,
|
|
onToggleSidebar,
|
|
onTogglePanel,
|
|
onShowShortcuts,
|
|
}: ShortcutHandlers) {
|
|
useEffect(() => {
|
|
if (!enabled) return;
|
|
|
|
function handleKeyDown(e: KeyboardEvent) {
|
|
// Don't fire shortcuts when typing in inputs
|
|
if (isKeyboardShortcutTextInputTarget(e.target)) {
|
|
return;
|
|
}
|
|
|
|
// ? → Show keyboard shortcuts cheatsheet
|
|
if (e.key === "?" && !e.metaKey && !e.ctrlKey && !e.altKey) {
|
|
e.preventDefault();
|
|
onShowShortcuts?.();
|
|
return;
|
|
}
|
|
|
|
// C → New Issue
|
|
if (e.key === "c" && !e.metaKey && !e.ctrlKey && !e.altKey) {
|
|
e.preventDefault();
|
|
onNewIssue?.();
|
|
}
|
|
|
|
// [ → Toggle Sidebar
|
|
if (e.key === "[" && !e.metaKey && !e.ctrlKey) {
|
|
e.preventDefault();
|
|
onToggleSidebar?.();
|
|
}
|
|
|
|
// ] → Toggle Panel
|
|
if (e.key === "]" && !e.metaKey && !e.ctrlKey) {
|
|
e.preventDefault();
|
|
onTogglePanel?.();
|
|
}
|
|
}
|
|
|
|
document.addEventListener("keydown", handleKeyDown);
|
|
return () => document.removeEventListener("keydown", handleKeyDown);
|
|
}, [enabled, onNewIssue, onToggleSidebar, onTogglePanel, onShowShortcuts]);
|
|
}
|