|
| 1 | +<script setup lang="ts"> |
| 2 | +import { onClickOutside } from '@vueuse/core' |
| 3 | +
|
| 4 | +const selectedPM = useSelectedPackageManager() |
| 5 | +
|
| 6 | +const listRef = useTemplateRef('listRef') |
| 7 | +const triggerRef = useTemplateRef('triggerRef') |
| 8 | +const isOpen = shallowRef(false) |
| 9 | +const highlightedIndex = shallowRef(-1) |
| 10 | +
|
| 11 | +// Generate unique ID for accessibility |
| 12 | +const inputId = useId() |
| 13 | +const listboxId = `${inputId}-listbox` |
| 14 | +
|
| 15 | +const pm = computed(() => { |
| 16 | + return packageManagers.find(p => p.id === selectedPM.value) ?? packageManagers[0] |
| 17 | +}) |
| 18 | +
|
| 19 | +function toggle() { |
| 20 | + if (isOpen.value) { |
| 21 | + close() |
| 22 | + } else { |
| 23 | + isOpen.value = true |
| 24 | + highlightedIndex.value = packageManagers.findIndex(pm => pm.id === selectedPM.value) |
| 25 | + } |
| 26 | +} |
| 27 | +
|
| 28 | +function close() { |
| 29 | + isOpen.value = false |
| 30 | + highlightedIndex.value = -1 |
| 31 | +} |
| 32 | +
|
| 33 | +function select(id: PackageManagerId) { |
| 34 | + selectedPM.value = id |
| 35 | + close() |
| 36 | + triggerRef.value?.focus() |
| 37 | +} |
| 38 | +
|
| 39 | +// Check for reduced motion preference |
| 40 | +const prefersReducedMotion = useMediaQuery('(prefers-reduced-motion: reduce)') |
| 41 | +
|
| 42 | +onClickOutside(listRef, close, { ignore: [triggerRef] }) |
| 43 | +function handleKeydown(event: KeyboardEvent) { |
| 44 | + if (!isOpen.value) return |
| 45 | +
|
| 46 | + switch (event.key) { |
| 47 | + case 'ArrowDown': |
| 48 | + event.preventDefault() |
| 49 | + highlightedIndex.value = (highlightedIndex.value + 1) % packageManagers.length |
| 50 | + break |
| 51 | + case 'ArrowUp': |
| 52 | + event.preventDefault() |
| 53 | + highlightedIndex.value = |
| 54 | + highlightedIndex.value <= 0 ? packageManagers.length - 1 : highlightedIndex.value - 1 |
| 55 | + break |
| 56 | + case 'Enter': { |
| 57 | + event.preventDefault() |
| 58 | + const pm = packageManagers[highlightedIndex.value] |
| 59 | + if (pm) { |
| 60 | + select(pm.id) |
| 61 | + } |
| 62 | + break |
| 63 | + } |
| 64 | + case 'Escape': |
| 65 | + close() |
| 66 | + triggerRef.value?.focus() |
| 67 | + break |
| 68 | + } |
| 69 | +} |
| 70 | +</script> |
| 71 | + |
| 72 | +<template> |
| 73 | + <div class="relative"> |
| 74 | + <button |
| 75 | + ref="triggerRef" |
| 76 | + type="button" |
| 77 | + class="inline-flex items-center gap-1 px-2 py-1 font-mono text-xs text-fg-muted bg-bg-subtle/80 border border-border rounded transition-colors duration-200 hover:(text-fg border-border-hover) active:scale-95 focus:border-border-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50" |
| 78 | + :aria-expanded="isOpen" |
| 79 | + aria-haspopup="listbox" |
| 80 | + :aria-label="$t('settings.package_manager')" |
| 81 | + :aria-controls="listboxId" |
| 82 | + @click="toggle" |
| 83 | + @keydown="handleKeydown" |
| 84 | + > |
| 85 | + <ClientOnly> |
| 86 | + <span class="inline-block h-3 w-3" :class="pm.icon" aria-hidden="true" /> |
| 87 | + <span>{{ pm.label }}</span> |
| 88 | + <template #fallback> |
| 89 | + <span class="inline-block h-3 w-3 i-simple-icons:npm" aria-hidden="true" /> |
| 90 | + <span>npm</span> |
| 91 | + </template> |
| 92 | + </ClientOnly> |
| 93 | + <span |
| 94 | + class="i-carbon-chevron-down w-3 h-3" |
| 95 | + :class="[ |
| 96 | + { 'rotate-180': isOpen }, |
| 97 | + prefersReducedMotion ? '' : 'transition-transform duration-200', |
| 98 | + ]" |
| 99 | + aria-hidden="true" |
| 100 | + /> |
| 101 | + </button> |
| 102 | + |
| 103 | + <!-- Dropdown menu --> |
| 104 | + <Transition |
| 105 | + :enter-active-class="prefersReducedMotion ? '' : 'transition-opacity duration-150'" |
| 106 | + :enter-from-class="prefersReducedMotion ? '' : 'opacity-0'" |
| 107 | + enter-to-class="opacity-100" |
| 108 | + :leave-active-class="prefersReducedMotion ? '' : 'transition-opacity duration-100'" |
| 109 | + leave-from-class="opacity-100" |
| 110 | + :leave-to-class="prefersReducedMotion ? '' : 'opacity-0'" |
| 111 | + > |
| 112 | + <ul |
| 113 | + v-if="isOpen" |
| 114 | + :id="listboxId" |
| 115 | + ref="listRef" |
| 116 | + role="listbox" |
| 117 | + :aria-activedescendant=" |
| 118 | + highlightedIndex >= 0 |
| 119 | + ? `${listboxId}-${packageManagers[highlightedIndex]?.id}` |
| 120 | + : undefined |
| 121 | + " |
| 122 | + :aria-label="$t('settings.package_manager')" |
| 123 | + class="absolute right-0 top-full mt-1 min-w-28 bg-bg-elevated border border-border rounded-lg shadow-lg z-50 overflow-hidden py-1" |
| 124 | + > |
| 125 | + <li |
| 126 | + v-for="(pm, index) in packageManagers" |
| 127 | + :id="`${listboxId}-${pm.id}`" |
| 128 | + :key="pm.id" |
| 129 | + role="option" |
| 130 | + :aria-selected="selectedPM === pm.id" |
| 131 | + class="flex items-center gap-2 px-3 py-1.5 font-mono text-xs cursor-pointer transition-colors duration-150" |
| 132 | + :class="[ |
| 133 | + selectedPM === pm.id ? 'text-fg' : 'text-fg-subtle', |
| 134 | + highlightedIndex === index ? 'bg-bg-subtle' : 'hover:bg-bg-subtle', |
| 135 | + ]" |
| 136 | + @click="select(pm.id)" |
| 137 | + @mouseenter="highlightedIndex = index" |
| 138 | + > |
| 139 | + <span class="inline-block h-3 w-3 shrink-0" :class="pm.icon" aria-hidden="true" /> |
| 140 | + <span>{{ pm.label }}</span> |
| 141 | + <span |
| 142 | + v-if="selectedPM === pm.id" |
| 143 | + class="i-carbon-checkmark w-3 h-3 text-accent ml-auto shrink-0" |
| 144 | + aria-hidden="true" |
| 145 | + /> |
| 146 | + </li> |
| 147 | + </ul> |
| 148 | + </Transition> |
| 149 | + </div> |
| 150 | +</template> |
0 commit comments