feat: implement Phase 9 - Keyboard Shortcuts
All checks were successful
Build and Push Docker Image to Gitea / build-and-push (push) Successful in 1m11s
All checks were successful
Build and Push Docker Image to Gitea / build-and-push (push) Successful in 1m11s
Features added: - Created useKeyboardShortcuts hook for managing keyboard shortcuts - Supports modifier keys (Ctrl, Shift, Alt) - Automatically ignores shortcuts when input fields are focused - ESC key blurs input fields to enable shortcuts - Added global keyboard shortcuts: - / : Focus search field - r : Refresh process list - a : Select all processes (flat view only) - ESC : Clear selection / unfocus - ? (Shift+/) : Show keyboard shortcuts help - Added process navigation shortcuts: - j : Select next process - k : Select previous process - Space : Toggle selection of focused process - Auto-scroll to focused process - Created KeyboardShortcutsHelp modal component: - Organized shortcuts by category - Visual kbd elements for keys - Info about input field behavior - Added keyboard shortcuts button to processes page header - Added isFocused prop to ProcessCard with accent ring styling - Added data-process-id attributes for keyboard navigation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
88
lib/hooks/useKeyboardShortcuts.ts
Normal file
88
lib/hooks/useKeyboardShortcuts.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { useEffect, useCallback } from 'react';
|
||||
|
||||
export interface KeyboardShortcut {
|
||||
key: string;
|
||||
ctrl?: boolean;
|
||||
shift?: boolean;
|
||||
alt?: boolean;
|
||||
description: string;
|
||||
action: () => void;
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
export interface UseKeyboardShortcutsOptions {
|
||||
shortcuts: KeyboardShortcut[];
|
||||
ignoreWhenInputFocused?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for managing keyboard shortcuts
|
||||
* Automatically handles event listeners and cleanup
|
||||
*/
|
||||
export function useKeyboardShortcuts({
|
||||
shortcuts,
|
||||
ignoreWhenInputFocused = true,
|
||||
}: UseKeyboardShortcutsOptions) {
|
||||
const handleKeyDown = useCallback(
|
||||
(event: KeyboardEvent) => {
|
||||
// Ignore if typing in input/textarea/select
|
||||
if (ignoreWhenInputFocused) {
|
||||
const target = event.target as HTMLElement;
|
||||
const tagName = target.tagName.toLowerCase();
|
||||
const isEditable = target.isContentEditable;
|
||||
|
||||
if (
|
||||
tagName === 'input' ||
|
||||
tagName === 'textarea' ||
|
||||
tagName === 'select' ||
|
||||
isEditable
|
||||
) {
|
||||
// Allow ESC to blur inputs
|
||||
if (event.key === 'Escape') {
|
||||
target.blur();
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Find matching shortcut
|
||||
for (const shortcut of shortcuts) {
|
||||
if (shortcut.enabled === false) continue;
|
||||
|
||||
const keyMatches = event.key.toLowerCase() === shortcut.key.toLowerCase();
|
||||
const ctrlMatches = shortcut.ctrl ? event.ctrlKey || event.metaKey : !event.ctrlKey && !event.metaKey;
|
||||
const shiftMatches = shortcut.shift ? event.shiftKey : !event.shiftKey;
|
||||
const altMatches = shortcut.alt ? event.altKey : !event.altKey;
|
||||
|
||||
if (keyMatches && ctrlMatches && shiftMatches && altMatches) {
|
||||
event.preventDefault();
|
||||
shortcut.action();
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
[shortcuts, ignoreWhenInputFocused]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener('keydown', handleKeyDown);
|
||||
return () => window.removeEventListener('keydown', handleKeyDown);
|
||||
}, [handleKeyDown]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Common keyboard shortcuts for reference
|
||||
*/
|
||||
export const COMMON_SHORTCUTS = {
|
||||
SEARCH: { key: '/', description: 'Focus search' },
|
||||
REFRESH: { key: 'r', description: 'Refresh data' },
|
||||
SELECT_ALL: { key: 'a', description: 'Select all' },
|
||||
ESCAPE: { key: 'Escape', description: 'Clear selection / Close modal' },
|
||||
HELP: { key: '?', shift: true, description: 'Show keyboard shortcuts' },
|
||||
NEXT: { key: 'j', description: 'Next item' },
|
||||
PREVIOUS: { key: 'k', description: 'Previous item' },
|
||||
SELECT: { key: ' ', description: 'Toggle selection' },
|
||||
START: { key: 's', description: 'Start selected' },
|
||||
STOP: { key: 'x', description: 'Stop selected' },
|
||||
RESTART: { key: 't', description: 'Restart selected' },
|
||||
} as const;
|
||||
Reference in New Issue
Block a user