forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageSelector.vue
More file actions
219 lines (198 loc) · 6.95 KB
/
PackageSelector.vue
File metadata and controls
219 lines (198 loc) · 6.95 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
<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 ?? 4)
// Input state
const inputValue = shallowRef('')
const isInputFocused = shallowRef(false)
// Use the shared search composable (supports both npm and Algolia providers)
const { data: searchData, status } = useSearch(inputValue, { 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))
})
const numberFormatter = useNumberFormatter()
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 = ''
}
function removePackage(name: string) {
packages.value = packages.value.filter(p => p !== name)
}
function handleKeydown(e: KeyboardEvent) {
const inputValueTrim = inputValue.value.trim()
const hasMatchInPackages = filteredResults.value.find(result => {
return result.name === inputValueTrim
})
if (e.key === 'Enter' && inputValueTrim) {
e.preventDefault()
if (showNoDependencyOption.value) {
addPackage(NO_DEPENDENCY_ID)
} else if (hasMatchInPackages) {
addPackage(inputValueTrim)
}
}
}
function handleBlur() {
useTimeoutFn(() => {
isInputFocused.value = false
}, 200)
}
</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-carbon:clean 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="small"
:aria-label="
$t('compare.selector.remove_package', {
package: pkg === NO_DEPENDENCY_ID ? $t('compare.no_dependency.label') : pkg,
})
"
@click="removePackage(pkg)"
classicon="i-carbon:close"
/>
</TagStatic>
</div>
<!-- Add package input -->
<div v-if="packages.length < maxPackages" 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
size="medium"
class="w-full min-w-25 ps-7"
aria-autocomplete="list"
@focus="isInputFocused = true"
@blur="handleBlur"
@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 && (filteredResults.length > 0 || isSearching || showNoDependencyOption)
"
class="absolute top-full inset-x-0 mt-1 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"
class="block w-full text-start"
:aria-label="$t('compare.no_dependency.add_column')"
@click="addPackage(NO_DEPENDENCY_ID)"
>
<span class="text-sm text-accent italic flex items-center gap-2 block">
<span class="i-carbon:clean 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" class="px-4 py-3 text-sm text-fg-muted">
{{ $t('compare.selector.searching') }}
</div>
<ButtonBase
v-for="result in filteredResults"
:key="result.name"
class="block w-full text-start"
@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">
{{ 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>