|
| 1 | +import { For, Show, createEffect, createMemo, createSignal, onCleanup } from "solid-js"; |
| 2 | +import { Search, X } from "lucide-solid"; |
| 3 | + |
| 4 | +import Button from "./button"; |
| 5 | +import { formatKeybindLabel } from "../utils/keybinds"; |
| 6 | + |
| 7 | +export type PaletteItem = { |
| 8 | + id: string; |
| 9 | + title: string; |
| 10 | + description?: string; |
| 11 | + category?: string; |
| 12 | + keybind?: string; |
| 13 | + onSelect: () => void; |
| 14 | + onHighlight?: () => void; |
| 15 | +}; |
| 16 | + |
| 17 | +export type PaletteGroup = { |
| 18 | + id: string; |
| 19 | + title: string; |
| 20 | + items: PaletteItem[]; |
| 21 | +}; |
| 22 | + |
| 23 | +export type CommandPaletteModalProps = { |
| 24 | + open: boolean; |
| 25 | + mode: "command" | "file"; |
| 26 | + query: string; |
| 27 | + setQuery: (value: string) => void; |
| 28 | + groups: PaletteGroup[]; |
| 29 | + onClose: () => void; |
| 30 | +}; |
| 31 | + |
| 32 | +export default function CommandPaletteModal(props: CommandPaletteModalProps) { |
| 33 | + let searchInputRef: HTMLInputElement | undefined; |
| 34 | + const [activeIndex, setActiveIndex] = createSignal(0); |
| 35 | + |
| 36 | + const filteredGroups = createMemo(() => { |
| 37 | + const query = props.query.trim().toLowerCase(); |
| 38 | + if (!query) return props.groups; |
| 39 | + return props.groups |
| 40 | + .map((group: PaletteGroup) => { |
| 41 | + const items = group.items.filter((item) => { |
| 42 | + const haystack = `${item.title} ${item.description ?? ""} ${item.category ?? ""}`.toLowerCase(); |
| 43 | + return haystack.includes(query); |
| 44 | + }); |
| 45 | + return { ...group, items }; |
| 46 | + }) |
| 47 | + .filter((group) => group.items.length > 0); |
| 48 | + }); |
| 49 | + |
| 50 | + const flattenedItems = createMemo(() => filteredGroups().flatMap((group) => group.items)); |
| 51 | + |
| 52 | + createEffect(() => { |
| 53 | + if (props.open) { |
| 54 | + setActiveIndex(0); |
| 55 | + requestAnimationFrame(() => searchInputRef?.focus()); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + createEffect(() => { |
| 60 | + const item = flattenedItems()[activeIndex()]; |
| 61 | + item?.onHighlight?.(); |
| 62 | + }); |
| 63 | + |
| 64 | + createEffect(() => { |
| 65 | + if (!props.open) return; |
| 66 | + const onKeyDown = (event: KeyboardEvent) => { |
| 67 | + if (!props.open) return; |
| 68 | + if (event.key === "Escape") { |
| 69 | + event.preventDefault(); |
| 70 | + event.stopPropagation(); |
| 71 | + props.onClose(); |
| 72 | + return; |
| 73 | + } |
| 74 | + if (event.key === "ArrowDown") { |
| 75 | + event.preventDefault(); |
| 76 | + event.stopPropagation(); |
| 77 | + setActiveIndex((current: number) => Math.min(current + 1, flattenedItems().length - 1)); |
| 78 | + return; |
| 79 | + } |
| 80 | + if (event.key === "ArrowUp") { |
| 81 | + event.preventDefault(); |
| 82 | + event.stopPropagation(); |
| 83 | + setActiveIndex((current: number) => Math.max(current - 1, 0)); |
| 84 | + return; |
| 85 | + } |
| 86 | + if (event.key === "Enter") { |
| 87 | + const item = flattenedItems()[activeIndex()]; |
| 88 | + if (!item) return; |
| 89 | + event.preventDefault(); |
| 90 | + event.stopPropagation(); |
| 91 | + item.onSelect(); |
| 92 | + props.onClose(); |
| 93 | + } |
| 94 | + }; |
| 95 | + window.addEventListener("keydown", onKeyDown, true); |
| 96 | + onCleanup(() => window.removeEventListener("keydown", onKeyDown, true)); |
| 97 | + }); |
| 98 | + |
| 99 | + const placeholder = () => |
| 100 | + props.mode === "file" ? "Search files" : "Search commands or files"; |
| 101 | + |
| 102 | + return ( |
| 103 | + <Show when={props.open}> |
| 104 | + <div class="fixed inset-0 z-50 bg-gray-1/60 backdrop-blur-sm flex items-start justify-center p-4 overflow-y-auto"> |
| 105 | + <div class="bg-gray-2 border border-gray-6/70 w-full max-w-xl rounded-2xl shadow-2xl overflow-hidden max-h-[calc(100vh-2rem)] flex flex-col"> |
| 106 | + <div class="p-5 border-b border-gray-6/50 flex items-center gap-3"> |
| 107 | + <Search size={16} class="text-gray-10" /> |
| 108 | + <input |
| 109 | + ref={(el) => (searchInputRef = el)} |
| 110 | + type="text" |
| 111 | + value={props.query} |
| 112 | + onInput={(event: InputEvent) => props.setQuery((event.currentTarget as HTMLInputElement).value)} |
| 113 | + placeholder={placeholder()} |
| 114 | + class="flex-1 bg-transparent text-sm text-gray-12 placeholder-gray-7 focus:outline-none" |
| 115 | + /> |
| 116 | + <Button variant="ghost" class="!p-2 rounded-full" onClick={props.onClose}> |
| 117 | + <X size={16} /> |
| 118 | + </Button> |
| 119 | + </div> |
| 120 | + |
| 121 | + <div class="p-4 space-y-5 overflow-y-auto"> |
| 122 | + <Show |
| 123 | + when={filteredGroups().length} |
| 124 | + fallback={<div class="text-sm text-gray-9">No results found.</div>} |
| 125 | + > |
| 126 | + <For each={filteredGroups()}> |
| 127 | + {(group: PaletteGroup) => ( |
| 128 | + <div class="space-y-2"> |
| 129 | + <div class="text-[11px] font-semibold uppercase tracking-wider text-gray-9"> |
| 130 | + {group.title} |
| 131 | + </div> |
| 132 | + <div class="space-y-1"> |
| 133 | + <For each={group.items}> |
| 134 | + {(item: PaletteItem) => { |
| 135 | + const isActive = createMemo( |
| 136 | + () => flattenedItems()[activeIndex()]?.id === item.id, |
| 137 | + ); |
| 138 | + return ( |
| 139 | + <button |
| 140 | + type="button" |
| 141 | + class={`w-full flex items-start justify-between gap-3 rounded-xl px-3 py-2 text-left transition-colors ${ |
| 142 | + isActive() |
| 143 | + ? "bg-gray-12/10 text-gray-12" |
| 144 | + : "text-gray-11 hover:bg-gray-12/5" |
| 145 | + }`} |
| 146 | + onMouseEnter={() => { |
| 147 | + const index = flattenedItems().findIndex((entry) => entry.id === item.id); |
| 148 | + if (index >= 0) setActiveIndex(index); |
| 149 | + item.onHighlight?.(); |
| 150 | + }} |
| 151 | + onMouseDown={(event: MouseEvent) => { |
| 152 | + event.preventDefault(); |
| 153 | + item.onSelect(); |
| 154 | + props.onClose(); |
| 155 | + }} |
| 156 | + > |
| 157 | + <div class="min-w-0"> |
| 158 | + <div class="text-sm font-medium text-gray-12 truncate">{item.title}</div> |
| 159 | + <Show when={item.description}> |
| 160 | + <div class="text-[11px] text-gray-9 truncate">{item.description}</div> |
| 161 | + </Show> |
| 162 | + </div> |
| 163 | + <Show when={item.keybind}> |
| 164 | + <span class="text-[10px] text-gray-9 uppercase tracking-wide border border-gray-6/60 rounded-lg px-2 py-1"> |
| 165 | + {formatKeybindLabel(item.keybind!)} |
| 166 | + </span> |
| 167 | + </Show> |
| 168 | + </button> |
| 169 | + ); |
| 170 | + }} |
| 171 | + </For> |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + )} |
| 175 | + </For> |
| 176 | + </Show> |
| 177 | + </div> |
| 178 | + </div> |
| 179 | + </div> |
| 180 | + </Show> |
| 181 | + ); |
| 182 | +} |
0 commit comments