-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathPackageSelector.vue
More file actions
327 lines (290 loc) · 10.1 KB
/
PackageSelector.vue
File metadata and controls
327 lines (290 loc) · 10.1 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<script setup lang="ts">
import { NO_DEPENDENCY_ID } from '~/composables/usePackageComparison'
const packages = defineModel<string[]>({ required: true })
const props = defineProps<{
/** Maximum number of packages allowed */
max?: number
}>()
const maxPackages = computed(() => props.max ?? MAX_PACKAGE_SELECTION)
// Input state
const inputValue = shallowRef('')
const isInputFocused = shallowRef(false)
// Keyboard navigation state
const highlightedIndex = shallowRef(-1)
const listRef = useTemplateRef('listRef')
const PAGE_JUMP = 5
// Use the shared search composable (supports both npm and Algolia providers)
const { searchProvider } = useSearchProvider()
const { data: searchData, status } = useSearch(inputValue, searchProvider, { size: 15 })
const isSearching = computed(() => status.value === 'pending')
// Trigger strings for "What Would James Do?" typeahead Easter egg
// Intentionally not localized
const EASTER_EGG_TRIGGERS = new Set([
'no dep',
'none',
'vanilla',
'diy',
'zero',
'nothing',
'0',
"don't",
'native',
'use the platform',
])
// Check if "no dependency" option should show in typeahead
const showNoDependencyOption = computed(() => {
if (packages.value.includes(NO_DEPENDENCY_ID)) return false
const input = inputValue.value.toLowerCase().trim()
if (!input) return false
return EASTER_EGG_TRIGGERS.has(input)
})
// Filter out already selected packages
const filteredResults = computed(() => {
if (!searchData.value?.objects) return []
return searchData.value.objects
.map(o => ({
name: o.package.name,
description: o.package.description,
}))
.filter(r => !packages.value.includes(r.name))
})
// Unified list of navigable items for keyboard navigation
const navigableItems = computed(() => {
const items: { type: 'no-dependency' | 'package'; name: string }[] = []
if (showNoDependencyOption.value) {
items.push({ type: 'no-dependency', name: NO_DEPENDENCY_ID })
}
for (const r of filteredResults.value) {
items.push({ type: 'package', name: r.name })
}
return items
})
const resultIndexOffset = computed(() => (showNoDependencyOption.value ? 1 : 0))
const numberFormatter = useNumberFormatter()
const keyboardShortcuts = useKeyboardShortcutsPreference()
function addPackage(name: string) {
if (packages.value.length >= maxPackages.value) return
if (packages.value.includes(name)) return
// Keep NO_DEPENDENCY_ID always last
if (name === NO_DEPENDENCY_ID) {
packages.value = [...packages.value, name]
} else if (packages.value.includes(NO_DEPENDENCY_ID)) {
// Insert before the no-dep entry
const withoutNoDep = packages.value.filter(p => p !== NO_DEPENDENCY_ID)
packages.value = [...withoutNoDep, name, NO_DEPENDENCY_ID]
} else {
packages.value = [...packages.value, name]
}
inputValue.value = ''
highlightedIndex.value = -1
}
function removePackage(name: string) {
packages.value = packages.value.filter(p => p !== name)
}
function handleKeydown(e: KeyboardEvent) {
if (!keyboardShortcuts.value) {
return
}
const items = navigableItems.value
const count = items.length
switch (e.key) {
case 'ArrowDown':
e.preventDefault()
if (count === 0) return
if (highlightedIndex.value < count - 1) {
highlightedIndex.value++
} else {
highlightedIndex.value = 0
}
break
case 'ArrowUp':
e.preventDefault()
if (count === 0) return
if (highlightedIndex.value > 0) {
highlightedIndex.value--
} else {
highlightedIndex.value = count - 1
}
break
case 'PageDown':
e.preventDefault()
if (count === 0) return
if (highlightedIndex.value === -1) {
highlightedIndex.value = Math.min(PAGE_JUMP - 1, count - 1)
} else {
highlightedIndex.value = Math.min(highlightedIndex.value + PAGE_JUMP, count - 1)
}
break
case 'PageUp':
e.preventDefault()
if (count === 0) return
highlightedIndex.value = Math.max(highlightedIndex.value - PAGE_JUMP, 0)
break
case 'Enter': {
const inputValueTrim = inputValue.value.trim()
if (!inputValueTrim) return
e.preventDefault()
// If an item is highlighted, select it
if (highlightedIndex.value >= 0 && highlightedIndex.value < count) {
addPackage(items[highlightedIndex.value]!.name)
return
}
// Fallback: exact match or easter egg (preserves existing behavior)
if (showNoDependencyOption.value) {
addPackage(NO_DEPENDENCY_ID)
} else {
const hasMatch = filteredResults.value.find(r => r.name === inputValueTrim)
if (hasMatch) {
addPackage(inputValueTrim)
}
}
break
}
case 'Escape':
inputValue.value = ''
highlightedIndex.value = -1
break
}
}
// Reset highlight when user types
watch(inputValue, () => {
highlightedIndex.value = -1
})
// Scroll highlighted item into view
watch(highlightedIndex, index => {
if (index >= 0 && listRef.value) {
const items = listRef.value.querySelectorAll('[data-navigable]')
const item = items[index] as HTMLElement | undefined
item?.scrollIntoView({ block: 'nearest' })
}
})
const containerRef = useTemplateRef('containerRef')
onClickOutside(containerRef, () => {
isInputFocused.value = false
highlightedIndex.value = -1
})
</script>
<template>
<div class="space-y-3">
<!-- Selected packages -->
<div v-if="packages.length > 0" class="flex flex-wrap gap-2">
<TagStatic v-for="pkg in packages" :key="pkg">
<!-- No dependency display -->
<template v-if="pkg === NO_DEPENDENCY_ID">
<span class="text-sm text-accent italic flex items-center gap-1.5">
<span class="i-lucide:leaf w-3.5 h-3.5" aria-hidden="true" />
{{ $t('compare.no_dependency.label') }}
</span>
</template>
<LinkBase v-else :to="packageRoute(pkg)" class="text-sm">
{{ pkg }}
</LinkBase>
<ButtonBase
size="sm"
:aria-label="
$t('compare.selector.remove_package', {
package: pkg === NO_DEPENDENCY_ID ? $t('compare.no_dependency.label') : pkg,
})
"
@click="removePackage(pkg)"
classicon="i-lucide:x"
/>
</TagStatic>
</div>
<!-- Add package input -->
<div v-if="packages.length < maxPackages" ref="containerRef" class="relative">
<div class="relative group flex items-center">
<label for="package-search" class="sr-only">
{{ $t('compare.selector.search_label') }}
</label>
<span
class="absolute inset-is-3 text-fg-subtle font-mono text-md pointer-events-none transition-colors duration-200 motion-reduce:transition-none [.group:hover:not(:focus-within)_&]:text-fg/80 group-focus-within:text-accent z-1"
>
/
</span>
<InputBase
id="package-search"
v-model="inputValue"
type="text"
:placeholder="
packages.length === 0
? $t('compare.selector.search_first')
: $t('compare.selector.search_add')
"
no-correct
class="w-full min-w-25 ps-7"
aria-autocomplete="list"
ref="inputRef"
@focus="isInputFocused = true"
@keydown="handleKeydown"
/>
</div>
<!-- Search results dropdown -->
<Transition
enter-active-class="transition-opacity duration-150"
enter-from-class="opacity-0"
leave-active-class="transition-opacity duration-100"
leave-from-class="opacity-100"
leave-to-class="opacity-0"
>
<div
v-if="isInputFocused && (navigableItems.length > 0 || isSearching)"
ref="listRef"
class="absolute top-full inset-x-0 mt-1 px-0.5 bg-bg-elevated border border-border rounded-lg shadow-lg z-50 max-h-64 overflow-y-auto"
>
<!-- No dependency option (easter egg with James) -->
<ButtonBase
v-if="showNoDependencyOption"
data-navigable
class="block w-full text-start !border-transparent"
:class="highlightedIndex === 0 ? '!bg-accent/15' : ''"
:aria-label="$t('compare.no_dependency.add_column')"
@mouseenter="highlightedIndex = 0"
@click="addPackage(NO_DEPENDENCY_ID)"
>
<span class="text-sm text-accent italic flex items-center gap-2">
<span class="i-lucide:leaf w-4 h-4" aria-hidden="true" />
{{ $t('compare.no_dependency.typeahead_title') }}
</span>
<span class="text-xs text-fg-muted truncate mt-0.5">
{{ $t('compare.no_dependency.typeahead_description') }}
</span>
</ButtonBase>
<div
v-if="isSearching && navigableItems.length === 0"
class="px-4 py-3 text-sm text-fg-muted"
>
{{ $t('compare.selector.searching') }}
</div>
<ButtonBase
v-for="(result, index) in filteredResults"
:key="result.name"
data-navigable
class="block w-full text-start my-0.5 !border-transparent"
:class="highlightedIndex === index + resultIndexOffset ? '!bg-accent/15' : ''"
@mouseenter="highlightedIndex = index + resultIndexOffset"
@click="addPackage(result.name)"
>
<span class="font-mono text-sm text-fg block">{{ result.name }}</span>
<span
v-if="result.description"
class="text-xs text-fg-muted truncate mt-0.5 w-full block"
>
{{ stripHtmlTags(decodeHtmlEntities(result.description)) }}
</span>
</ButtonBase>
</div>
</Transition>
</div>
<!-- Hint -->
<p class="text-xs text-fg-subtle">
{{
$t('compare.selector.packages_selected', {
count: numberFormatter.format(packages.length),
max: numberFormatter.format(maxPackages),
})
}}
<span v-if="packages.length < 2">{{ $t('compare.selector.add_hint') }}</span>
</p>
</div>
</template>