-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathManagerSelect.vue
More file actions
192 lines (175 loc) · 5.69 KB
/
ManagerSelect.vue
File metadata and controls
192 lines (175 loc) · 5.69 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
<script setup lang="ts">
import { onClickOutside, useEventListener } from '@vueuse/core'
const selectedPM = useSelectedPackageManager()
const listRef = useTemplateRef('listRef')
const triggerRef = useTemplateRef('triggerRef')
const isOpen = shallowRef(false)
const highlightedIndex = shallowRef(-1)
const dropdownPosition = shallowRef<{ top: number; left: number } | null>(null)
function getDropdownStyle(): Record<string, string> {
if (!dropdownPosition.value) return {}
return {
top: `${dropdownPosition.value.top}px`,
left: `${dropdownPosition.value.left}px`,
}
}
useEventListener('scroll', close, true)
// Generate unique ID for accessibility
const inputId = useId()
const listboxId = `${inputId}-listbox`
function toggle() {
if (isOpen.value) {
close()
} else {
if (triggerRef.value) {
const rect = triggerRef.value.getBoundingClientRect()
dropdownPosition.value = {
top: rect.bottom + 4,
left: rect.left,
}
}
isOpen.value = true
highlightedIndex.value = packageManagers.findIndex(pm => pm.id === selectedPM.value)
}
}
function close() {
isOpen.value = false
highlightedIndex.value = -1
}
function select(id: PackageManagerId) {
selectedPM.value = id
close()
triggerRef.value?.focus()
}
// Check for reduced motion preference
const prefersReducedMotion = useMediaQuery('(prefers-reduced-motion: reduce)')
onClickOutside(listRef, close, { ignore: [triggerRef] })
function handleKeydown(event: KeyboardEvent) {
if (!isOpen.value) return
switch (event.key) {
case 'ArrowDown':
event.preventDefault()
highlightedIndex.value = (highlightedIndex.value + 1) % packageManagers.length
break
case 'ArrowUp':
event.preventDefault()
highlightedIndex.value =
highlightedIndex.value <= 0 ? packageManagers.length - 1 : highlightedIndex.value - 1
break
case 'Enter': {
event.preventDefault()
const pm = packageManagers[highlightedIndex.value]
if (pm) {
select(pm.id)
}
break
}
case 'Escape':
close()
triggerRef.value?.focus()
break
}
}
</script>
<template>
<button
ref="triggerRef"
type="button"
class="cursor-pointer flex items-center gap-1.5 px-2 py-2 font-mono text-xs text-fg-muted bg-bg-subtle border border-border-subtle border-solid rounded-md transition-colors duration-150 hover:(text-fg border-border-hover) active:scale-95 focus:border-border-hover focus-visible:outline-accent/70"
:aria-expanded="isOpen"
aria-haspopup="listbox"
:aria-label="$t('package.get_started.pm_label')"
:aria-controls="isOpen ? listboxId : undefined"
@click="toggle"
@keydown="handleKeydown"
>
<template v-for="pmOption in packageManagers" :key="pmOption.id">
<span
class="inline-block h-3 w-3 pm-select-content"
:class="pmOption.icon"
:data-pm-select="pmOption.id"
aria-hidden="true"
/>
<span
class="pm-select-content"
:data-pm-select="pmOption.id"
:aria-hidden="pmOption.id !== selectedPM"
>{{ pmOption.label }}</span
>
</template>
<span
class="i-lucide:chevron-down w-3 h-3"
:class="[
{ 'rotate-180': isOpen },
prefersReducedMotion ? '' : 'transition-transform duration-200',
]"
aria-hidden="true"
/>
</button>
<!-- Dropdown menu (teleported to body to avoid clipping) -->
<Teleport to="body">
<Transition
:enter-active-class="prefersReducedMotion ? '' : 'transition-opacity duration-150'"
:enter-from-class="prefersReducedMotion ? '' : 'opacity-0'"
enter-to-class="opacity-100"
:leave-active-class="prefersReducedMotion ? '' : 'transition-opacity duration-100'"
leave-from-class="opacity-100"
:leave-to-class="prefersReducedMotion ? '' : 'opacity-0'"
>
<ul
v-if="isOpen"
:id="listboxId"
ref="listRef"
role="listbox"
:aria-activedescendant="
highlightedIndex >= 0
? `${listboxId}-${packageManagers[highlightedIndex]?.id}`
: undefined
"
:aria-label="$t('package.get_started.pm_label')"
:style="getDropdownStyle()"
class="fixed bg-bg-subtle border border-border rounded-md shadow-lg z-50"
>
<li
v-for="(pm, index) in packageManagers"
:id="`${listboxId}-${pm.id}`"
:key="pm.id"
role="option"
:aria-selected="selectedPM === pm.id"
class="cursor-pointer flex items-center gap-2 px-3 py-1.5 font-mono text-xs transition-colors duration-150"
:class="[
selectedPM === pm.id ? 'text-fg' : 'text-fg-subtle',
highlightedIndex === index ? 'bg-bg-elevated' : 'hover:bg-bg-elevated',
]"
@click="select(pm.id)"
@mouseenter="highlightedIndex = index"
>
<span class="inline-block h-3 w-3" :class="pm.icon" aria-hidden="true" />
<span>{{ pm.label }}</span>
<span
v-if="selectedPM === pm.id"
class="i-lucide:check w-3 h-3 text-accent ms-auto"
aria-hidden="true"
/>
</li>
</ul>
</Transition>
</Teleport>
</template>
<style>
:root[data-pm] .pm-select-content {
display: none;
}
:root[data-pm='npm'] [data-pm-select='npm'],
:root[data-pm='pnpm'] [data-pm-select='pnpm'],
:root[data-pm='yarn'] [data-pm-select='yarn'],
:root[data-pm='bun'] [data-pm-select='bun'],
:root[data-pm='deno'] [data-pm-select='deno'],
:root[data-pm='vlt'] [data-pm-select='vlt'] {
display: inline-block;
}
/* Fallback: when no data-pm is set, npm is selected by default */
:root:not([data-pm]) .pm-select-content:not([data-pm-select='npm']) {
display: none;
}
</style>