A keyboard component from Aceternity UI.
From Aceternity UI
1"use client";
2import React, {
3 createContext,
4 useContext,
5 useEffect,
6 useRef,
7 useState,
8 useCallback,
9} from "react";
10import { motion, AnimatePresence } from "motion/react";
11import { cn } from "@/lib/utils";
12import {
13 IconBrightnessDown,
14 IconBrightnessUp,
15 IconCaretRightFilled,
16 IconCaretUpFilled,
17 IconChevronUp,
18 IconMicrophone,
19 IconMoon,
20 IconPlayerSkipForward,
21 IconPlayerTrackNext,
22 IconPlayerTrackPrev,
23 IconTable,
24 IconVolume,
25 IconVolume2,
26 IconVolume3,
27 IconSearch,
28 IconWorld,
29 IconCommand,
30 IconCaretLeftFilled,
31 IconCaretDownFilled,
32} from "@tabler/icons-react";
33
34// Sound sprite definitions from config.json [startMs, durationMs]
35// Key down sounds - half duration for a snappy press sound
36const SOUND_DEFINES_DOWN: Record<string, [number, number]> = {
37 Escape: [2894, 113],
38 F1: [3610, 98],
39 F2: [4210, 90],
40 F3: [4758, 90],
41 F4: [5250, 100],
42 F5: [5831, 105],
43 F6: [6396, 105],
44 F7: [6900, 105],
45 F8: [7443, 111],
46 F9: [7955, 91],
47 F10: [8504, 105],
48 F11: [9046, 94],
49 F12: [9582, 96],
50 Backquote: [12476, 100],
51 Digit1: [12946, 96],
52 Digit2: [13470, 95],
53 Digit3: [13963, 100],
54 Digit4: [14481, 102],
55 Digit5: [14994, 94],
56 Digit6: [15505, 109],
57 Digit7: [15990, 97],
58 Digit8: [16529, 92],
59 Digit9: [17012, 103],
60 Digit0: [17550, 87],
61 Minus: [18052, 93],
62 Equal: [18553, 89],
63 Backspace: [19065, 110],
64 Tab: [21734, 119],
65 KeyQ: [22245, 95],
66 KeyW: [22790, 89],
67 KeyE: [23317, 83],
68 KeyR: [23817, 92],
69 KeyT: [24297, 92],
70 KeyY: [24811, 93],
71 KeyU: [25313, 95],
72 KeyI: [25795, 91],
73 KeyO: [26309, 84],
74 KeyP: [26804, 83],
75 BracketLeft: [27330, 85],
76 BracketRight: [27883, 99],
77 Backslash: [28393, 100],
78 CapsLock: [31011, 126],
79 KeyA: [31542, 85],
80 KeyS: [32031, 88],
81 KeyD: [32492, 85],
82 KeyF: [32973, 87],
83 KeyG: [33453, 94],
84 KeyH: [33986, 93],
85 KeyJ: [34425, 88],
86 KeyK: [34932, 90],
87 KeyL: [35410, 95],
88 Semicolon: [35914, 95],
89 Quote: [36428, 87],
90 Enter: [36902, 117],
91 ShiftLeft: [38136, 133],
92 KeyZ: [38694, 80],
93 KeyX: [39148, 76],
94 KeyC: [39632, 95],
95 KeyV: [40136, 94],
96 KeyB: [40621, 107],
97 KeyN: [41103, 90],
98 KeyM: [41610, 93],
99 Comma: [42110, 92],
100 Period: [42594, 90],
101 Slash: [43105, 95],
102 ShiftRight: [43565, 137],
103 Fn: [44251, 110],
104 ControlLeft: [45327, 83],
105 AltLeft: [45750, 82],
106 MetaLeft: [46199, 100],
107 Space: [51541, 144],
108 MetaRight: [47929, 75],
109 AltRight: [49329, 82],
110 ArrowUp: [44251, 110],
111 ArrowLeft: [49837, 88],
112 ArrowDown: [50333, 90],
113 ArrowRight: [50783, 111],
114};
115
116// Key up sounds - shorter duration, offset for the release "thock"
117// Uses the tail end of each sound sample for a lighter release effect
118const SOUND_DEFINES_UP: Record<string, [number, number]> = {
119 Escape: [2894 + 120, 100],
120 F1: [3610 + 100, 90],
121 F2: [4210 + 95, 80],
122 F3: [4758 + 95, 80],
123 F4: [5250 + 105, 90],
124 F5: [5831 + 110, 95],
125 F6: [6396 + 110, 95],
126 F7: [6900 + 110, 95],
127 F8: [7443 + 115, 100],
128 F9: [7955 + 95, 80],
129 F10: [8504 + 110, 95],
130 F11: [9046 + 100, 85],
131 F12: [9582 + 100, 85],
132 Backquote: [12476 + 105, 90],
133 Digit1: [12946 + 100, 85],
134 Digit2: [13470 + 100, 85],
135 Digit3: [13963 + 105, 90],
136 Digit4: [14481 + 110, 90],
137 Digit5: [14994 + 100, 85],
138 Digit6: [15505 + 115, 100],
139 Digit7: [15990 + 100, 90],
140 Digit8: [16529 + 95, 85],
141 Digit9: [17012 + 110, 90],
142 Digit0: [17550 + 90, 80],
143 Minus: [18052 + 100, 85],
144 Equal: [18553 + 90, 85],
145 Backspace: [19065 + 115, 100],
146 Tab: [21734 + 125, 110],
147 KeyQ: [22245 + 100, 85],
148 KeyW: [22790 + 90, 85],
149 KeyE: [23317 + 85, 80],
150 KeyR: [23817 + 95, 85],
151 KeyT: [24297 + 95, 85],
152 KeyY: [24811 + 100, 85],
153 KeyU: [25313 + 100, 85],
154 KeyI: [25795 + 95, 85],
155 KeyO: [26309 + 85, 80],
156 KeyP: [26804 + 85, 80],
157 BracketLeft: [27330 + 85, 80],
158 BracketRight: [27883 + 105, 90],
159 Backslash: [28393 + 105, 90],
160 CapsLock: [31011 + 135, 110],
161 KeyA: [31542 + 90, 80],
162 KeyS: [32031 + 90, 80],
163 KeyD: [32492 + 85, 80],
164 KeyF: [32973 + 90, 80],
165 KeyG: [33453 + 100, 85],
166 KeyH: [33986 + 95, 85],
167 KeyJ: [34425 + 90, 85],
168 KeyK: [34932 + 95, 85],
169 KeyL: [35410 + 100, 85],
170 Semicolon: [35914 + 100, 85],
171 Quote: [36428 + 90, 80],
172 Enter: [36902 + 125, 105],
173 ShiftLeft: [38136 + 140, 120],
174 KeyZ: [38694 + 85, 75],
175 KeyX: [39148 + 80, 70],
176 KeyC: [39632 + 100, 85],
177 KeyV: [40136 + 100, 85],
178 KeyB: [40621 + 115, 95],
179 KeyN: [41103 + 95, 85],
180 KeyM: [41610 + 100, 85],
181 Comma: [42110 + 95, 85],
182 Period: [42594 + 95, 85],
183 Slash: [43105 + 100, 85],
184 ShiftRight: [43565 + 145, 125],
185 Fn: [44251 + 115, 100],
186 ControlLeft: [45327 + 85, 80],
187 AltLeft: [45750 + 85, 80],
188 MetaLeft: [46199 + 105, 90],
189 Space: [51541 + 150, 130],
190 MetaRight: [47929 + 75, 70],
191 AltRight: [49329 + 85, 80],
192 ArrowUp: [44251 + 115, 100],
193 ArrowLeft: [49837 + 90, 85],
194 ArrowDown: [50333 + 95, 80],
195 ArrowRight: [50783 + 115, 100],
196};
197
198// Map key codes to display labels
199const KEY_DISPLAY_LABELS: Record<string, string> = {
200 Escape: "esc",
201 Backspace: "delete",
202 Tab: "tab",
203 Enter: "return",
204 ShiftLeft: "shift",
205 ShiftRight: "shift",
206 ControlLeft: "control",
207 ControlRight: "control",
208 AltLeft: "option",
209 AltRight: "option",
210 MetaLeft: "command",
211 MetaRight: "command",
212 Space: "space",
213 CapsLock: "caps",
214 ArrowUp: "↑",
215 ArrowDown: "↓",
216 ArrowLeft: "←",
217 ArrowRight: "→",
218 Backquote: "`",
219 Minus: "-",
220 Equal: "=",
221 BracketLeft: "[",
222 BracketRight: "]",
223 Backslash: "\\",
224 Semicolon: ";",
225 Quote: "'",
226 Comma: ",",
227 Period: ".",
228 Slash: "/",
229};
230
231const getKeyDisplayLabel = (keyCode: string): string => {
232 if (KEY_DISPLAY_LABELS[keyCode]) return KEY_DISPLAY_LABELS[keyCode];
233 if (keyCode.startsWith("Key")) return keyCode.slice(3);
234 if (keyCode.startsWith("Digit")) return keyCode.slice(5);
235 if (keyCode.startsWith("F") && keyCode.length <= 3) return keyCode;
236 return keyCode;
237};
238
239interface KeyboardContextType {
240 playSoundDown: (keyCode: string) => void;
241 playSoundUp: (keyCode: string) => void;
242 pressedKeys: Set<string>;
243 setPressed: (keyCode: string) => void;
244 setReleased: (keyCode: string) => void;
245 lastPressedKey: string | null;
246}
247
248const KeyboardContext = createContext<KeyboardContextType | null>(null);
249
250const useKeyboardSound = () => {
251 const context = useContext(KeyboardContext);
252 if (!context) {
253 throw new Error("useKeyboardSound must be used within KeyboardProvider");
254 }
255 return context;
256};
257
258const KeyboardProvider = ({
259 children,
260 enableSound = false,
261 containerRef,
262}: {
263 children: React.ReactNode;
264 enableSound?: boolean;
265 containerRef: React.RefObject<HTMLDivElement | null>;
266}) => {
267 const audioContextRef = useRef<AudioContext | null>(null);
268 const audioBufferRef = useRef<AudioBuffer | null>(null);
269 const [pressedKeys, setPressedKeys] = useState<Set<string>>(new Set());
270 const [lastPressedKey, setLastPressedKey] = useState<string | null>(null);
271 const [soundLoaded, setSoundLoaded] = useState(false);
272 const [isVisible, setIsVisible] = useState(false);
273
274 useEffect(() => {
275 if (!enableSound) return;
276
277 // Initialize AudioContext and load sound file
278 const initAudio = async () => {
279 try {
280 audioContextRef.current = new AudioContext();
281 const response = await fetch("/sounds/sound.ogg");
282 if (!response.ok) {
283 console.warn("Sound file not available");
284 return;
285 }
286 const arrayBuffer = await response.arrayBuffer();
287 audioBufferRef.current =
288 await audioContextRef.current.decodeAudioData(arrayBuffer);
289 setSoundLoaded(true);
290 } catch (error) {
291 console.warn("Failed to load sound:", error);
292 }
293 };
294
295 initAudio();
296
297 return () => {
298 audioContextRef.current?.close();
299 };
300 }, [enableSound]);
301
302 const playSoundDown = useCallback(
303 (keyCode: string) => {
304 if (!enableSound || !soundLoaded) return;
305 if (!audioContextRef.current || !audioBufferRef.current) return;
306
307 const soundDef = SOUND_DEFINES_DOWN[keyCode];
308 if (!soundDef) return;
309
310 const [startMs, durationMs] = soundDef;
311 const startTime = startMs / 1000;
312 const duration = durationMs / 1000;
313
314 // Resume audio context if suspended (browser autoplay policy)
315 if (audioContextRef.current.state === "suspended") {
316 audioContextRef.current.resume();
317 }
318
319 const source = audioContextRef.current.createBufferSource();
320 source.buffer = audioBufferRef.current;
321 source.connect(audioContextRef.current.destination);
322 source.start(0, startTime, duration);
323 },
324 [enableSound, soundLoaded],
325 );
326
327 const playSoundUp = useCallback(
328 (keyCode: string) => {
329 if (!enableSound || !soundLoaded) return;
330 if (!audioContextRef.current || !audioBufferRef.current) return;
331
332 const soundDef = SOUND_DEFINES_UP[keyCode];
333 if (!soundDef) return;
334
335 const [startMs, durationMs] = soundDef;
336 const startTime = startMs / 1000;
337 const duration = durationMs / 1000;
338
339 // Resume audio context if suspended (browser autoplay policy)
340 if (audioContextRef.current.state === "suspended") {
341 audioContextRef.current.resume();
342 }
343
344 const source = audioContextRef.current.createBufferSource();
345 source.buffer = audioBufferRef.current;
346 source.connect(audioContextRef.current.destination);
347 source.start(0, startTime, duration);
348 },
349 [enableSound, soundLoaded],
350 );
351
352 const setPressed = useCallback((keyCode: string) => {
353 setPressedKeys((prev) => new Set(prev).add(keyCode));
354 setLastPressedKey(keyCode);
355 }, []);
356
357 const setReleased = useCallback((keyCode: string) => {
358 setPressedKeys((prev) => {
359 const next = new Set(prev);
360 next.delete(keyCode);
361 return next;
362 });
363 }, []);
364
365 // Track visibility with IntersectionObserver
366 useEffect(() => {
367 const element = containerRef.current;
368 if (!element) return;
369
370 const observer = new IntersectionObserver(
371 ([entry]) => {
372 setIsVisible(entry.isIntersecting);
373 },
374 { threshold: 0.1 },
375 );
376
377 observer.observe(element);
378
379 return () => {
380 observer.disconnect();
381 };
382 }, [containerRef]);
383
384 // Handle physical keyboard events (only when visible)
385 useEffect(() => {
386 if (!isVisible) return;
387
388 const handleKeyDown = (e: KeyboardEvent) => {
389 // Prevent repeat events
390 if (e.repeat) return;
391
392 const keyCode = e.code;
393 playSoundDown(keyCode);
394 setPressed(keyCode);
395 };
396
397 const handleKeyUp = (e: KeyboardEvent) => {
398 const keyCode = e.code;
399 playSoundUp(keyCode);
400 setReleased(keyCode);
401 };
402
403 document.addEventListener("keydown", handleKeyDown);
404 document.addEventListener("keyup", handleKeyUp);
405
406 return () => {
407 document.removeEventListener("keydown", handleKeyDown);
408 document.removeEventListener("keyup", handleKeyUp);
409 };
410 }, [isVisible, playSoundDown, playSoundUp, setPressed, setReleased]);
411
412 return (
413 <KeyboardContext.Provider
414 value={{
415 playSoundDown,
416 playSoundUp,
417 pressedKeys,
418 setPressed,
419 setReleased,
420 lastPressedKey,
421 }}
422 >
423 {children}
424 </KeyboardContext.Provider>
425 );
426};
427
428const KeystrokePreview = () => {
429 const { lastPressedKey, pressedKeys } = useKeyboardSound();
430 const [displayKey, setDisplayKey] = useState<string | null>(null);
431 const [animationKey, setAnimationKey] = useState(0);
432
433 useEffect(() => {
434 if (lastPressedKey) {
435 // Clear display if space or shift is pressed
436 if (
437 lastPressedKey === "Space" ||
438 lastPressedKey === "ShiftLeft" ||
439 lastPressedKey === "ShiftRight"
440 ) {
441 setDisplayKey(null);
442 return;
443 }
444
445 setDisplayKey(getKeyDisplayLabel(lastPressedKey));
446 setAnimationKey((prev) => prev + 1);
447 }
448 }, [lastPressedKey]);
449
450 const isPressed = pressedKeys.size > 0;
451
452 return (
453 <div className="relative flex h-12 w-full items-center justify-center">
454 <AnimatePresence mode="popLayout">
455 {displayKey && (
456 <motion.div
457 key={animationKey}
458 layout
459 initial={{ opacity: 0, scale: 0.5, y: 5 }}
460 animate={{
461 opacity: 1,
462 scale: isPressed ? 0.95 : 1,
463 y: 0,
464 }}
465 exit={{ opacity: 0, scale: 0.8, y: -5 }}
466 transition={{
467 type: "spring",
468 stiffness: 500,
469 damping: 30,
470 mass: 0.5,
471 }}
472 className="absolute flex items-center justify-center rounded-lg px-4 py-2 font-mono text-2xl font-black text-neutral-700"
473 >
474 <motion.span
475 initial={{ opacity: 0, scale: 1.2, filter: "blur(10px)" }}
476 animate={{ opacity: 0.6, scale: 1, filter: "blur(0px)" }}
477 transition={{ duration: 0.05 }}
478 className="text-2xl"
479 >
480 {displayKey}
481 </motion.span>
482 </motion.div>
483 )}
484 </AnimatePresence>
485 </div>
486 );
487};
488
489export const Keyboard = ({
490 className,
491 enableSound = false,
492 showPreview = false,
493}: {
494 className?: string;
495 enableSound?: boolean;
496 showPreview?: boolean;
497}) => {
498 const containerRef = useRef<HTMLDivElement>(null);
499
500 return (
501 <KeyboardProvider enableSound={enableSound} containerRef={containerRef}>
502 <div
503 ref={containerRef}
504 className={cn(
505 "mx-auto w-fit [zoom:0.8] sm:[zoom:1.25] md:[zoom:1.5] lg:[zoom:1.75] xl:[zoom:2]",
506 className,
507 )}
508 >
509 {showPreview && <KeystrokePreview />}
510 <Keypad />
511 </div>
512 </KeyboardProvider>
513 );
514};
515
516export const Keypad = () => {
517 return (
518 <div className="h-full w-fit rounded-xl bg-neutral-200 p-1 shadow-sm ring-1 shadow-black/5 ring-black/5">
519 {/* Function Row */}
520 <Row>
521 <Key
522 keyCode="Escape"
523 containerClassName="rounded-tl-xl"
524 className="w-10 rounded-tl-lg"
525 childrenClassName="items-start justify-end pb-[2px] pl-[4px]"
526 >
527 <span>esc</span>
528 </Key>
529 <Key keyCode="F1">
530 <IconBrightnessDown className="h-[6px] w-[6px]" />
531 <span className="mt-1">F1</span>
532 </Key>
533 <Key keyCode="F2">
534 <IconBrightnessUp className="h-[6px] w-[6px]" />
535 <span className="mt-1">F2</span>
536 </Key>
537 <Key keyCode="F3">
538 <IconTable className="h-[6px] w-[6px]" />
539 <span className="mt-1">F3</span>
540 </Key>
541 <Key keyCode="F4">
542 <IconSearch className="h-[6px] w-[6px]" />
543 <span className="mt-1">F4</span>
544 </Key>
545 <Key keyCode="F5">
546 <IconMicrophone className="h-[6px] w-[6px]" />
547 <span className="mt-1">F5</span>
548 </Key>
549 <Key keyCode="F6">
550 <IconMoon className="h-[6px] w-[6px]" />
551 <span className="mt-1">F6</span>
552 </Key>
553 <Key keyCode="F7">
554 <IconPlayerTrackPrev className="h-[6px] w-[6px]" />
555 <span className="mt-1">F7</span>
556 </Key>
557 <Key keyCode="F8">
558 <IconPlayerSkipForward className="h-[6px] w-[6px]" />
559 <span className="mt-1">F8</span>
560 </Key>
561 <Key keyCode="F9">
562 <IconPlayerTrackNext className="h-[6px] w-[6px]" />
563 <span className="mt-1">F9</span>
564 </Key>
565 <Key keyCode="F10">
566 <IconVolume3 className="h-[6px] w-[6px]" />
567 <span className="mt-1">F10</span>
568 </Key>
569 <Key keyCode="F11">
570 <IconVolume2 className="h-[6px] w-[6px]" />
571 <span className="mt-1">F11</span>
572 </Key>
573 <Key keyCode="F12">
574 <IconVolume className="h-[6px] w-[6px]" />
575 <span className="mt-1">F12</span>
576 </Key>
577 <Key containerClassName="rounded-tr-xl" className="rounded-tr-lg">
578 <div className="h-4 w-4 rounded-full bg-gradient-to-b from-neutral-300 via-neutral-200 to-neutral-300 p-px">
579 <div className="h-full w-full rounded-full bg-neutral-100" />
580 </div>
581 </Key>
582 </Row>
583
584 {/* Number Row */}
585 <Row>
586 <Key keyCode="Backquote">
587 <span>~</span>
588 <span>`</span>
589 </Key>
590 <Key keyCode="Digit1">
591 <span>!</span>
592 <span>1</span>
593 </Key>
594 <Key keyCode="Digit2">
595 <span>@</span>
596 <span>2</span>
597 </Key>
598 <Key keyCode="Digit3">
599 <span>#</span>
600 <span>3</span>
601 </Key>
602 <Key keyCode="Digit4">
603 <span>$</span>
604 <span>4</span>
605 </Key>
606 <Key keyCode="Digit5">
607 <span>%</span>
608 <span>5</span>
609 </Key>
610 <Key keyCode="Digit6">
611 <span>^</span>
612 <span>6</span>
613 </Key>
614 <Key keyCode="Digit7">
615 <span>&</span>
616 <span>7</span>
617 </Key>
618 <Key keyCode="Digit8">
619 <span>*</span>
620 <span>8</span>
621 </Key>
622 <Key keyCode="Digit9">
623 <span>(</span>
624 <span>9</span>
625 </Key>
626 <Key keyCode="Digit0">
627 <span>)</span>
628 <span>0</span>
629 </Key>
630 <Key keyCode="Minus">
631 <span>—</span>
632 <span>_</span>
633 </Key>
634 <Key keyCode="Equal">
635 <span>+</span>
636 <span>=</span>
637 </Key>
638 <Key
639 keyCode="Backspace"
640 className="w-10"
641 childrenClassName="items-end justify-end pr-[4px] pb-[2px]"
642 >
643 <span>delete</span>
644 </Key>
645 </Row>
646
647 {/* QWERTY Row */}
648 <Row>
649 <Key
650 keyCode="Tab"
651 className="w-10"
652 childrenClassName="items-start justify-end pb-[2px] pl-[4px]"
653 >
654 <span>tab</span>
655 </Key>
656 {["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"].map((letter) => (
657 <Key key={letter} keyCode={`Key${letter}`}>
658 {letter}
659 </Key>
660 ))}
661 <Key keyCode="BracketLeft">
662 <span>{`{`}</span>
663 <span>{`[`}</span>
664 </Key>
665 <Key keyCode="BracketRight">
666 <span>{`}`}</span>
667 <span>{`]`}</span>
668 </Key>
669 <Key keyCode="Backslash">
670 <span>{`|`}</span>
671 <span>{`\\`}</span>
672 </Key>
673 </Row>
674
675 {/* Home Row */}
676 <Row>
677 <Key
678 keyCode="CapsLock"
679 className="w-[2.8rem]"
680 childrenClassName="items-start justify-end pb-[2px] pl-[4px]"
681 >
682 <span>caps lock</span>
683 </Key>
684 {["A", "S", "D", "F", "G", "H", "J", "K", "L"].map((letter) => (
685 <Key key={letter} keyCode={`Key${letter}`}>
686 {letter}
687 </Key>
688 ))}
689 <Key keyCode="Semicolon">
690 <span>:</span>
691 <span>;</span>
692 </Key>
693 <Key keyCode="Quote">
694 <span>{`"`}</span>
695 <span>{`'`}</span>
696 </Key>
697 <Key
698 keyCode="Enter"
699 className="w-[2.85rem]"
700 childrenClassName="items-end justify-end pr-[4px] pb-[2px]"
701 >
702 <span>return</span>
703 </Key>
704 </Row>
705
706 {/* Bottom Letter Row */}
707 <Row>
708 <Key
709 keyCode="ShiftLeft"
710 className="w-[3.65rem]"
711 childrenClassName="items-start justify-end pb-[2px] pl-[4px]"
712 >
713 <span>shift</span>
714 </Key>
715 {["Z", "X", "C", "V", "B", "N", "M"].map((letter) => (
716 <Key key={letter} keyCode={`Key${letter}`}>
717 {letter}
718 </Key>
719 ))}
720 <Key keyCode="Comma">
721 <span>{`<`}</span>
722 <span>,</span>
723 </Key>
724 <Key keyCode="Period">
725 <span>{`>`}</span>
726 <span>.</span>
727 </Key>
728 <Key keyCode="Slash">
729 <span>?</span>
730 <span>/</span>
731 </Key>
732 <Key
733 keyCode="ShiftRight"
734 className="w-[3.65rem]"
735 childrenClassName="items-end justify-end pr-[4px] pb-[2px]"
736 >
737 <span>shift</span>
738 </Key>
739 </Row>
740
741 {/* Modifier Row */}
742 <Row>
743 <ModifierKey
744 keyCode="Fn"
745 containerClassName="rounded-bl-xl"
746 className="rounded-bl-lg"
747 >
748 <span>fn</span>
749 <IconWorld className="h-[6px] w-[6px]" />
750 </ModifierKey>
751 <ModifierKey keyCode="ControlLeft">
752 <IconChevronUp className="h-[6px] w-[6px]" />
753 <span>control</span>
754 </ModifierKey>
755 <ModifierKey keyCode="AltLeft">
756 <OptionKey className="h-[6px] w-[6px]" />
757 <span>option</span>
758 </ModifierKey>
759 <ModifierKey keyCode="MetaLeft" className="w-8">
760 <IconCommand className="h-[6px] w-[6px]" />
761 <span>command</span>
762 </ModifierKey>
763 <Key keyCode="Space" className="w-[8.2rem]" />
764 <ModifierKey keyCode="MetaRight" className="w-8">
765 <IconCommand className="h-[6px] w-[6px]" />
766 <span>command</span>
767 </ModifierKey>
768 <ModifierKey keyCode="AltRight">
769 <OptionKey className="h-[6px] w-[6px]" />
770 <span>option</span>
771 </ModifierKey>
772 {/* Arrow Keys */}
773 <div className="flex h-6 w-[4.9rem] items-center justify-end rounded-[4px] p-[0.5px]">
774 <Key keyCode="ArrowLeft" className="h-6 w-6">
775 <IconCaretLeftFilled className="h-[6px] w-[6px]" />
776 </Key>
777 <div className="flex flex-col">
778 <Key keyCode="ArrowUp" className="h-3 w-6">
779 <IconCaretUpFilled className="h-[6px] w-[6px]" />
780 </Key>
781 <Key keyCode="ArrowDown" className="h-3 w-6">
782 <IconCaretDownFilled className="h-[6px] w-[6px]" />
783 </Key>
784 </div>
785 <Key
786 keyCode="ArrowRight"
787 containerClassName="rounded-br-xl"
788 className="h-6 w-6 rounded-br-lg"
789 >
790 <IconCaretRightFilled className="h-[6px] w-[6px]" />
791 </Key>
792 </div>
793 </Row>
794 </div>
795 );
796};
797
798const Row = ({ children }: { children: React.ReactNode }) => (
799 <div className="mb-[2px] flex w-full shrink-0 gap-[2px]">{children}</div>
800);
801
802const Key = ({
803 className,
804 childrenClassName,
805 containerClassName,
806 children,
807 keyCode,
808}: {
809 className?: string;
810 childrenClassName?: string;
811 containerClassName?: string;
812 children?: React.ReactNode;
813 keyCode?: string;
814}) => {
815 const { playSoundDown, playSoundUp, pressedKeys, setPressed, setReleased } =
816 useKeyboardSound();
817 const isPressed = keyCode ? pressedKeys.has(keyCode) : false;
818
819 const handleMouseDown = () => {
820 if (keyCode) {
821 playSoundDown(keyCode);
822 setPressed(keyCode);
823 }
824 };
825
826 const handleMouseUp = () => {
827 if (keyCode && isPressed) {
828 playSoundUp(keyCode);
829 setReleased(keyCode);
830 }
831 };
832
833 const handleMouseLeave = () => {
834 if (keyCode && isPressed) {
835 setReleased(keyCode);
836 }
837 };
838
839 return (
840 <div className={cn("rounded-[4px] p-[0.5px]", containerClassName)}>
841 <button
842 type="button"
843 onMouseDown={handleMouseDown}
844 onMouseUp={handleMouseUp}
845 onMouseLeave={handleMouseLeave}
846 className={cn(
847 "flex h-6 w-6 cursor-pointer items-center justify-center rounded-[3.5px] bg-gray-100 shadow-[0px_0px_1px_0px_rgba(0,0,0,0.5),0px_1px_1px_0px_rgba(0,0,0,0.1),0px_1px_0px_0px_rgba(255,255,255,1)_inset] transition-transform duration-75 active:scale-[0.98]",
848 isPressed &&
849 "scale-[0.98] bg-gray-100/80 shadow-[0px_0px_1px_0px_rgba(0,0,0,0.5),0px_1px_1px_0px_rgba(0,0,0,0.1),0px_1px_0px_0px_rgba(255,255,255,0.5)]",
850 className,
851 )}
852 >
853 <div
854 className={cn(
855 "flex h-full w-full flex-col items-center justify-center text-[5px] text-neutral-700",
856 childrenClassName,
857 )}
858 >
859 {children}
860 </div>
861 </button>
862 </div>
863 );
864};
865
866const ModifierKey = ({
867 className,
868 containerClassName,
869 children,
870 keyCode,
871}: {
872 className?: string;
873 containerClassName?: string;
874 children?: React.ReactNode;
875 keyCode?: string;
876}) => {
877 const { playSoundDown, playSoundUp, pressedKeys, setPressed, setReleased } =
878 useKeyboardSound();
879 const isPressed = keyCode ? pressedKeys.has(keyCode) : false;
880
881 const handleMouseDown = () => {
882 if (keyCode) {
883 playSoundDown(keyCode);
884 setPressed(keyCode);
885 }
886 };
887
888 const handleMouseUp = () => {
889 if (keyCode && isPressed) {
890 playSoundUp(keyCode);
891 setReleased(keyCode);
892 }
893 };
894
895 const handleMouseLeave = () => {
896 if (keyCode && isPressed) {
897 setReleased(keyCode);
898 }
899 };
900
901 return (
902 <div className={cn("rounded-[4px] p-[0.5px]", containerClassName)}>
903 <button
904 type="button"
905 onMouseDown={handleMouseDown}
906 onMouseUp={handleMouseUp}
907 onMouseLeave={handleMouseLeave}
908 className={cn(
909 "flex h-6 w-6 cursor-pointer items-center justify-center rounded-[3.5px] bg-gray-100 shadow-[0px_0px_1px_0px_rgba(0,0,0,0.5),0px_1px_1px_0px_rgba(0,0,0,0.1),0px_1px_0px_0px_rgba(255,255,255,1)_inset] transition-transform duration-75 active:scale-[0.98]",
910 isPressed &&
911 "scale-[0.98] bg-gray-100/80 shadow-[0px_0px_1px_0px_rgba(0,0,0,0.5),0px_1px_1px_0px_rgba(0,0,0,0.1),0px_1px_0px_0px_rgba(255,255,255,0.5)]",
912 className,
913 )}
914 >
915 <div className="flex h-full w-full flex-col items-start justify-between p-1 text-[5px] text-neutral-700">
916 {children}
917 </div>
918 </button>
919 </div>
920 );
921};
922
923const OptionKey = ({ className }: { className?: string }) => {
924 return (
925 <svg
926 fill="none"
927 version="1.1"
928 xmlns="http://www.w3.org/2000/svg"
929 viewBox="0 0 32 32"
930 className={className}
931 >
932 <rect
933 stroke="currentColor"
934 strokeWidth={2}
935 x="18"
936 y="5"
937 width="10"
938 height="2"
939 />
940 <polygon
941 stroke="currentColor"
942 strokeWidth={2}
943 points="10.6,5 4,5 4,7 9.4,7 18.4,27 28,27 28,25 19.6,25"
944 />
945 </svg>
946 );
947};
948