Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | 7x 58x 58x 58x 38x 38x 7x 7x 7x 7x 38x 58x 58x 13x 2x 2x 2x 2x 13x 13x 58x 38x 13x 1x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 58x 13x 2x 7x 7x | /**
* KeyboardShortcutHelp modal component
*
* @module components/common/KeyboardShortcutHelp
*/
import React, { useEffect, useMemo } from 'react';
import { KeyboardShortcutHelpProps, GroupedShortcuts } from '../../types/keyboard';
import { ShortcutBadge } from './ShortcutBadge';
import { SHORTCUT_CATEGORY_LABELS, KEYBOARD_TEST_IDS } from '../../constants/keyboardShortcuts';
import { useKeyboardShortcutContext } from '../../contexts/KeyboardShortcutContext';
/**
* KeyboardShortcutHelp component displays a modal with all available keyboard shortcuts
*
* @example
* ```tsx
* <KeyboardShortcutHelp isOpen={showHelp} onClose={() => setShowHelp(false)} />
* ```
*/
export const KeyboardShortcutHelp: React.FC<KeyboardShortcutHelpProps> = ({
isOpen,
onClose,
shortcuts: providedShortcuts,
}) => {
const context = useKeyboardShortcutContext();
const shortcuts = providedShortcuts || context.shortcuts;
// Group shortcuts by category
const groupedShortcuts = useMemo<GroupedShortcuts>(() => {
const groups: GroupedShortcuts = {};
Object.values(shortcuts).forEach((shortcut) => {
const category = shortcut.category;
Eif (!groups[category]) {
groups[category] = [];
}
groups[category].push(shortcut);
});
return groups;
}, [shortcuts]);
// Handle Escape key to close modal
// Note: This local handler coexists with the global keyboard shortcut handler.
// The Escape key is marked as a bypass key in BYPASS_INPUT_CHECK_KEYS, ensuring
// it works correctly in both contexts without conflicts.
useEffect(() => {
if (!isOpen) return;
const handleEscape = (e: KeyboardEvent): void => {
Eif (e.key === 'Escape') {
onClose();
e.preventDefault();
e.stopPropagation();
}
};
window.addEventListener('keydown', handleEscape);
return () => window.removeEventListener('keydown', handleEscape);
}, [isOpen, onClose]);
// Focus trap for accessibility
useEffect(() => {
if (!isOpen) return;
let cleanup: (() => void) | undefined;
// Use requestAnimationFrame to ensure modal is in DOM
const rafId = requestAnimationFrame(() => {
const modal = document.querySelector('[role="dialog"]');
Iif (!modal) return;
const focusableElements = modal.querySelectorAll<HTMLElement>(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
);
const firstElement = focusableElements[0];
const lastElement = focusableElements[focusableElements.length - 1];
const handleTab = (e: KeyboardEvent): void => {
if (e.key !== 'Tab') return;
if (e.shiftKey && document.activeElement === firstElement) {
e.preventDefault();
lastElement?.focus();
} else if (!e.shiftKey && document.activeElement === lastElement) {
e.preventDefault();
firstElement?.focus();
}
};
modal.addEventListener('keydown', handleTab as EventListener);
firstElement?.focus();
cleanup = () => modal.removeEventListener('keydown', handleTab as EventListener);
});
return () => {
cancelAnimationFrame(rafId);
cleanup?.();
};
}, [isOpen]);
if (!isOpen) return null;
return (
<div
className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black bg-opacity-50"
onClick={onClose}
data-testid={KEYBOARD_TEST_IDS.HELP_MODAL}
aria-label="Close keyboard shortcuts dialog by clicking outside"
>
<div
role="dialog"
aria-modal="true"
aria-labelledby="keyboard-shortcuts-title"
className="bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-4xl w-full max-h-[90vh] overflow-hidden flex flex-col"
onClick={(e) => e.stopPropagation()}
>
{/* Header */}
<div className="px-6 py-4 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between">
<h2
id="keyboard-shortcuts-title"
className="text-2xl font-bold text-gray-900 dark:text-white"
data-testid={KEYBOARD_TEST_IDS.HELP_MODAL_TITLE}
>
⌨️ Keyboard Shortcuts
</h2>
<button
onClick={onClose}
className="p-2 text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors"
aria-label="Close keyboard shortcuts"
data-testid={KEYBOARD_TEST_IDS.HELP_MODAL_CLOSE}
>
<svg
className="w-6 h-6"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>
{/* Content */}
<div className="overflow-y-auto p-6 flex-1">
{Object.entries(groupedShortcuts).map(([category, categoryShortcuts]) => (
<div
key={category}
className="mb-6 last:mb-0"
data-testid={KEYBOARD_TEST_IDS.HELP_MODAL_CATEGORY}
>
<h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-3">
{SHORTCUT_CATEGORY_LABELS[category as keyof typeof SHORTCUT_CATEGORY_LABELS] || category}
</h3>
<div className="space-y-2">
{categoryShortcuts.map((shortcut) => (
<div
key={shortcut.id}
className="flex items-center justify-between py-2 px-3 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-700 transition-colors"
data-testid={KEYBOARD_TEST_IDS.HELP_MODAL_SHORTCUT}
>
<span className="text-gray-700 dark:text-gray-300">
{shortcut.description}
</span>
<ShortcutBadge
shortcut={shortcut.keys}
size="md"
platformSpecific={true}
/>
</div>
))}
</div>
</div>
))}
</div>
{/* Footer */}
<div className="px-6 py-4 border-t border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900">
<p className="text-sm text-gray-600 dark:text-gray-400 text-center">
Press <ShortcutBadge shortcut="Escape" size="sm" /> to close this dialog
</p>
</div>
</div>
</div>
);
};
export default KeyboardShortcutHelp;
|