|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { Card } from "./card"; |
| 3 | +import { Command, X } from "lucide-react"; |
| 4 | + |
| 5 | +const KeyboardShortcuts = () => { |
| 6 | + const [isOpen, setIsOpen] = useState(false); |
| 7 | + |
| 8 | + useEffect(() => { |
| 9 | + const handleKeyPress = (e: KeyboardEvent) => { |
| 10 | + // Toggle shortcuts panel with Ctrl+K or Cmd+K |
| 11 | + if ((e.ctrlKey || e.metaKey) && e.key === 'k') { |
| 12 | + e.preventDefault(); |
| 13 | + setIsOpen(prev => !prev); |
| 14 | + } |
| 15 | + |
| 16 | + // Close with Escape |
| 17 | + if (e.key === 'Escape' && isOpen) { |
| 18 | + setIsOpen(false); |
| 19 | + } |
| 20 | + |
| 21 | + // Navigation shortcuts (only when shortcuts panel is closed) |
| 22 | + if (!isOpen) { |
| 23 | + if ((e.ctrlKey || e.metaKey) && e.key === 'h') { |
| 24 | + e.preventDefault(); |
| 25 | + window.location.hash = '#/'; |
| 26 | + } |
| 27 | + if ((e.ctrlKey || e.metaKey) && e.key === 's') { |
| 28 | + e.preventDefault(); |
| 29 | + window.location.hash = '#/simulation'; |
| 30 | + } |
| 31 | + if ((e.ctrlKey || e.metaKey) && e.key === 't') { |
| 32 | + e.preventDefault(); |
| 33 | + window.location.hash = '#/theory'; |
| 34 | + } |
| 35 | + if ((e.ctrlKey || e.metaKey) && e.key === 'q') { |
| 36 | + e.preventDefault(); |
| 37 | + window.location.hash = '#/quiz'; |
| 38 | + } |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + window.addEventListener('keydown', handleKeyPress); |
| 43 | + return () => window.removeEventListener('keydown', handleKeyPress); |
| 44 | + }, [isOpen]); |
| 45 | + |
| 46 | + if (!isOpen) { |
| 47 | + return ( |
| 48 | + <button |
| 49 | + onClick={() => setIsOpen(true)} |
| 50 | + className="fixed bottom-8 left-8 z-50 bg-secondary/80 hover:bg-secondary backdrop-blur-sm p-3 rounded-full shadow-lg hover:shadow-xl transition-all group" |
| 51 | + title="Keyboard Shortcuts (Ctrl+K)" |
| 52 | + > |
| 53 | + <Command className="w-5 h-5 text-white group-hover:scale-110 transition-transform" /> |
| 54 | + </button> |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <div className="fixed inset-0 z-50 bg-black/50 backdrop-blur-sm flex items-center justify-center p-4"> |
| 60 | + <Card className="bg-card/95 backdrop-blur-md border-primary/30 p-6 max-w-2xl w-full shadow-2xl"> |
| 61 | + <div className="flex items-center justify-between mb-6"> |
| 62 | + <div className="flex items-center gap-3"> |
| 63 | + <Command className="w-6 h-6 text-primary" /> |
| 64 | + <h2 className="text-2xl font-bold gradient-text">Keyboard Shortcuts</h2> |
| 65 | + </div> |
| 66 | + <button |
| 67 | + onClick={() => setIsOpen(false)} |
| 68 | + className="text-muted-foreground hover:text-white transition-colors" |
| 69 | + > |
| 70 | + <X className="w-5 h-5" /> |
| 71 | + </button> |
| 72 | + </div> |
| 73 | + |
| 74 | + <div className="space-y-4"> |
| 75 | + <div> |
| 76 | + <h3 className="text-sm font-semibold text-muted-foreground mb-3">NAVIGATION</h3> |
| 77 | + <div className="space-y-2"> |
| 78 | + {[ |
| 79 | + { keys: ['Ctrl', 'H'], desc: 'Go to Home' }, |
| 80 | + { keys: ['Ctrl', 'S'], desc: 'Go to Simulation' }, |
| 81 | + { keys: ['Ctrl', 'T'], desc: 'Go to Theory' }, |
| 82 | + { keys: ['Ctrl', 'Q'], desc: 'Go to Quiz' }, |
| 83 | + ].map((shortcut, index) => ( |
| 84 | + <div key={index} className="flex items-center justify-between p-3 bg-background/40 rounded-lg hover:bg-background/60 transition-colors"> |
| 85 | + <span className="text-white/90">{shortcut.desc}</span> |
| 86 | + <div className="flex gap-2"> |
| 87 | + {shortcut.keys.map((key, i) => ( |
| 88 | + <kbd key={i} className="px-3 py-1.5 bg-primary/20 text-primary text-sm font-semibold rounded border border-primary/30"> |
| 89 | + {key} |
| 90 | + </kbd> |
| 91 | + ))} |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + ))} |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + |
| 98 | + <div> |
| 99 | + <h3 className="text-sm font-semibold text-muted-foreground mb-3">GENERAL</h3> |
| 100 | + <div className="space-y-2"> |
| 101 | + {[ |
| 102 | + { keys: ['Ctrl', 'K'], desc: 'Toggle Shortcuts Panel' }, |
| 103 | + { keys: ['Esc'], desc: 'Close Modals' }, |
| 104 | + ].map((shortcut, index) => ( |
| 105 | + <div key={index} className="flex items-center justify-between p-3 bg-background/40 rounded-lg hover:bg-background/60 transition-colors"> |
| 106 | + <span className="text-white/90">{shortcut.desc}</span> |
| 107 | + <div className="flex gap-2"> |
| 108 | + {shortcut.keys.map((key, i) => ( |
| 109 | + <kbd key={i} className="px-3 py-1.5 bg-accent/20 text-accent text-sm font-semibold rounded border border-accent/30"> |
| 110 | + {key} |
| 111 | + </kbd> |
| 112 | + ))} |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + ))} |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + |
| 119 | + <p className="text-xs text-muted-foreground text-center pt-4"> |
| 120 | + Use <kbd className="px-2 py-1 bg-secondary/20 text-secondary text-xs rounded">Cmd</kbd> instead of <kbd className="px-2 py-1 bg-secondary/20 text-secondary text-xs rounded">Ctrl</kbd> on Mac |
| 121 | + </p> |
| 122 | + </div> |
| 123 | + </Card> |
| 124 | + </div> |
| 125 | + ); |
| 126 | +}; |
| 127 | + |
| 128 | +export default KeyboardShortcuts; |
0 commit comments