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 | /**
* @packageDocumentation
*
* Command palette UI. Triggered by keyboard shortcut (default: $mod+K).
* Displays all registered commands with titles, filterable by search.
*/
import { useState, useEffect, useRef, useMemo } from "react";
import { Box, Text, TextField } from "@radix-ui/themes";
import { CommandRegistry } from "./index";
interface CommandPaletteProps {
registry: CommandRegistry;
open: boolean;
onClose: () => void;
}
export function CommandPalette({ registry, open, onClose }: CommandPaletteProps) {
const [query, setQuery] = useState("");
const [selectedIndex, setSelectedIndex] = useState(0);
const inputRef = useRef<HTMLInputElement>(null);
const commands = useMemo(() => {
const all = registry.getAll().filter((c) => c.title);
if (!query.trim()) return all;
const q = query.toLowerCase();
return all.filter((c) => c.title.toLowerCase().includes(q));
}, [registry, query]);
useEffect(() => {
setSelectedIndex(0);
}, [query]);
useEffect(() => {
if (open) {
setQuery("");
inputRef.current?.focus();
}
}, [open]);
useEffect(() => {
if (!open) return;
function onKeyDown(e: KeyboardEvent) {
if (e.key === "Escape") {
e.preventDefault();
onClose();
return;
}
if (e.key === "ArrowDown") {
e.preventDefault();
setSelectedIndex((i) => Math.min(i + 1, commands.length - 1));
return;
}
if (e.key === "ArrowUp") {
e.preventDefault();
setSelectedIndex((i) => Math.max(i - 1, 0));
return;
}
if (e.key === "Enter") {
e.preventDefault();
const cmd = commands[selectedIndex];
if (cmd) {
cmd.execute();
onClose();
}
return;
}
if (e.key === "Tab") {
e.preventDefault();
inputRef.current?.focus();
return;
}
}
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [open, commands, selectedIndex, onClose]);
if (!open) return null;
return (
<Box
style={{
position: "fixed",
inset: 0,
zIndex: 100,
display: "flex",
flexDirection: "column",
alignItems: "center",
paddingTop: "15vh",
}}
onClick={onClose}
>
<Box
style={{
width: 560,
maxWidth: "90vw",
background: "var(--color-panel)",
borderRadius: 8,
boxShadow: "0 16px 48px rgba(0,0,0,0.4)",
overflow: "hidden",
border: "1px solid var(--gray-5)",
}}
onClick={(e) => e.stopPropagation()}
>
<TextField.Root
ref={inputRef}
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder="Type a command..."
size="3"
style={{
borderRadius: 0,
border: "none",
borderBottom: "1px solid var(--gray-5)",
background: "transparent",
}}
/>
<Box style={{ maxHeight: 320, overflow: "auto" }}>
{commands.map((cmd, i) => (
<Box
key={cmd.id}
data-testid={`palette-item-${cmd.id}`}
onClick={() => {
cmd.execute();
onClose();
}}
style={{
padding: "8px 12px",
cursor: "pointer",
background: i === selectedIndex ? "var(--accent-5)" : undefined,
color: i === selectedIndex ? "var(--accent-11)" : undefined,
display: "flex",
alignItems: "center",
justifyContent: "space-between",
}}
onMouseEnter={() => setSelectedIndex(i)}
>
<Text size="2">{cmd.title}</Text>
{(() => {
const isMac = navigator.platform.includes("Mac");
const shortcut = isMac && cmd.shortcutMac ? cmd.shortcutMac : cmd.shortcut;
if (!shortcut) return null;
return (
<Text size="1" color="gray">
{shortcut}
</Text>
);
})()}
</Box>
))}
{commands.length === 0 && (
<Box style={{ padding: 16, textAlign: "center" }}>
<Text size="2" color="gray">
No commands found
</Text>
</Box>
)}
</Box>
</Box>
</Box>
);
}
|