|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { SlimPackumentVersion, InstallSizeResult } from '#shared/types' |
| 3 | +import { onClickOutside, useEventListener } from '@vueuse/core' |
| 4 | +
|
| 5 | +const props = withDefaults( |
| 6 | + defineProps<{ |
| 7 | + packageName: string |
| 8 | + version: SlimPackumentVersion |
| 9 | + installSize: InstallSizeResult | null |
| 10 | + size?: 'small' | 'medium' |
| 11 | + }>(), |
| 12 | + { |
| 13 | + size: 'medium', |
| 14 | + }, |
| 15 | +) |
| 16 | +
|
| 17 | +const triggerRef = useTemplateRef('triggerRef') |
| 18 | +const listRef = useTemplateRef('listRef') |
| 19 | +const isOpen = shallowRef(false) |
| 20 | +const highlightedIndex = shallowRef(-1) |
| 21 | +const dropdownPosition = shallowRef<{ top: number; right: number } | null>(null) |
| 22 | +
|
| 23 | +const menuId = useId() |
| 24 | +const menuItems = computed(() => [ |
| 25 | + { id: 'package', label: $t('package.download.package'), icon: 'i-lucide:package' }, |
| 26 | + { id: 'dependencies', label: $t('package.download.dependencies'), icon: 'i-lucide:list-tree' }, |
| 27 | +]) |
| 28 | +
|
| 29 | +function getDropdownStyle(): Record<string, string> { |
| 30 | + if (!dropdownPosition.value) return {} |
| 31 | + return { |
| 32 | + top: `${dropdownPosition.value.top}px`, |
| 33 | + right: `${document.documentElement.clientWidth - dropdownPosition.value.right}px`, |
| 34 | + } |
| 35 | +} |
| 36 | +
|
| 37 | +function toggle() { |
| 38 | + if (isOpen.value) { |
| 39 | + close() |
| 40 | + } else { |
| 41 | + const rect = triggerRef.value?.$el?.getBoundingClientRect() |
| 42 | + if (rect) { |
| 43 | + dropdownPosition.value = { |
| 44 | + top: rect.bottom + 4, |
| 45 | + right: rect.right, |
| 46 | + } |
| 47 | + } |
| 48 | + isOpen.value = true |
| 49 | + highlightedIndex.value = 0 |
| 50 | + } |
| 51 | +} |
| 52 | +
|
| 53 | +function close() { |
| 54 | + isOpen.value = false |
| 55 | + highlightedIndex.value = -1 |
| 56 | +} |
| 57 | +
|
| 58 | +onClickOutside(listRef, close, { ignore: [triggerRef] }) |
| 59 | +
|
| 60 | +function handleKeydown(event: KeyboardEvent) { |
| 61 | + if (!isOpen.value) { |
| 62 | + if (event.key === 'ArrowDown' || event.key === 'Enter' || event.key === ' ') { |
| 63 | + event.preventDefault() |
| 64 | + toggle() |
| 65 | + } |
| 66 | + return |
| 67 | + } |
| 68 | +
|
| 69 | + switch (event.key) { |
| 70 | + case 'ArrowDown': |
| 71 | + event.preventDefault() |
| 72 | + highlightedIndex.value = (highlightedIndex.value + 1) % menuItems.value.length |
| 73 | + break |
| 74 | + case 'ArrowUp': |
| 75 | + event.preventDefault() |
| 76 | + highlightedIndex.value = |
| 77 | + highlightedIndex.value <= 0 ? menuItems.value.length - 1 : highlightedIndex.value - 1 |
| 78 | + break |
| 79 | + case 'Enter': |
| 80 | + case ' ': |
| 81 | + event.preventDefault() |
| 82 | + handleAction(menuItems.value[highlightedIndex.value]?.id) |
| 83 | + break |
| 84 | + case 'Escape': |
| 85 | + event.preventDefault() |
| 86 | + close() |
| 87 | + triggerRef.value?.$el?.focus() |
| 88 | + break |
| 89 | + case 'Tab': |
| 90 | + close() |
| 91 | + break |
| 92 | + } |
| 93 | +} |
| 94 | +
|
| 95 | +function handleAction(id: string | undefined) { |
| 96 | + if (id === 'package') { |
| 97 | + downloadPackage() |
| 98 | + } else if (id === 'dependencies') { |
| 99 | + downloadDependenciesScript() |
| 100 | + } |
| 101 | + close() |
| 102 | +} |
| 103 | +
|
| 104 | +function downloadPackage() { |
| 105 | + const tarballUrl = props.version.dist.tarball |
| 106 | + if (!tarballUrl) return |
| 107 | +
|
| 108 | + const link = document.createElement('a') |
| 109 | + link.href = tarballUrl |
| 110 | + link.download = `${props.packageName}-${props.version.version}.tgz` |
| 111 | + document.body.appendChild(link) |
| 112 | + link.click() |
| 113 | + document.body.removeChild(link) |
| 114 | +} |
| 115 | +
|
| 116 | +function downloadDependenciesScript() { |
| 117 | + if (!props.installSize) return |
| 118 | +
|
| 119 | + const lines = [ |
| 120 | + '#!/bin/bash', |
| 121 | + `# Download dependencies for ${props.packageName}@${props.version.version}`, |
| 122 | + 'mkdir -p node_modules', |
| 123 | + '', |
| 124 | + ] |
| 125 | +
|
| 126 | + // Add root package |
| 127 | + const rootTarball = (props.version.dist as any).tarball |
| 128 | + if (rootTarball) { |
| 129 | + lines.push(`# ${props.packageName}@${props.version.version}`) |
| 130 | + lines.push( |
| 131 | + `curl -L ${rootTarball} -o ${props.packageName.replace(/\//g, '__')}-${props.version.version}.tgz`, |
| 132 | + ) |
| 133 | + } |
| 134 | +
|
| 135 | + // Add dependencies |
| 136 | + props.installSize.dependencies.forEach((dep: any) => { |
| 137 | + lines.push(`# ${dep.name}@${dep.version}`) |
| 138 | + lines.push(`curl -L ${dep.tarballUrl} -o ${dep.name.replace(/\//g, '__')}-${dep.version}.tgz`) |
| 139 | + }) |
| 140 | +
|
| 141 | + const blob = new Blob([lines.join('\n')], { type: 'text/x-shellscript' }) |
| 142 | + const url = URL.createObjectURL(blob) |
| 143 | + const link = document.createElement('a') |
| 144 | + link.href = url |
| 145 | + link.download = `download-${props.packageName.replace(/\//g, '__')}-deps.sh` |
| 146 | + document.body.appendChild(link) |
| 147 | + link.click() |
| 148 | + document.body.removeChild(link) |
| 149 | + URL.revokeObjectURL(url) |
| 150 | +} |
| 151 | +
|
| 152 | +const prefersReducedMotion = useMediaQuery('(prefers-reduced-motion: reduce)') |
| 153 | +
|
| 154 | +useEventListener('scroll', () => isOpen.value && close(), { passive: true }) |
| 155 | +
|
| 156 | +defineOptions({ |
| 157 | + inheritAttrs: false, |
| 158 | +}) |
| 159 | +</script> |
| 160 | + |
| 161 | +<template> |
| 162 | + <ButtonBase |
| 163 | + ref="triggerRef" |
| 164 | + v-bind="$attrs" |
| 165 | + type="button" |
| 166 | + :variant="size === 'small' ? 'subtle' : 'secondary'" |
| 167 | + :size="size" |
| 168 | + classicon="i-lucide:download" |
| 169 | + :aria-expanded="isOpen" |
| 170 | + aria-haspopup="menu" |
| 171 | + :aria-controls="menuId" |
| 172 | + @click="toggle" |
| 173 | + @keydown="handleKeydown" |
| 174 | + > |
| 175 | + {{ $t('package.download.button') }} |
| 176 | + <span |
| 177 | + class="i-lucide:chevron-down ms-1" |
| 178 | + :class="[ |
| 179 | + size === 'small' ? 'w-3 h-3' : 'w-3.5 h-3.5', |
| 180 | + { 'rotate-180': isOpen }, |
| 181 | + prefersReducedMotion ? '' : 'transition-transform duration-200', |
| 182 | + ]" |
| 183 | + aria-hidden="true" |
| 184 | + /> |
| 185 | + </ButtonBase> |
| 186 | + |
| 187 | + <Teleport to="body"> |
| 188 | + <Transition |
| 189 | + :enter-active-class="prefersReducedMotion ? '' : 'transition-opacity duration-150'" |
| 190 | + :enter-from-class="prefersReducedMotion ? '' : 'opacity-0'" |
| 191 | + enter-to-class="opacity-100" |
| 192 | + :leave-active-class="prefersReducedMotion ? '' : 'transition-opacity duration-100'" |
| 193 | + leave-from-class="opacity-100" |
| 194 | + :leave-to-class="prefersReducedMotion ? '' : 'opacity-0'" |
| 195 | + > |
| 196 | + <div |
| 197 | + v-if="isOpen" |
| 198 | + :id="menuId" |
| 199 | + ref="listRef" |
| 200 | + role="menu" |
| 201 | + :style="getDropdownStyle()" |
| 202 | + class="fixed bg-bg-subtle border border-border rounded-md shadow-lg z-50 py-1 w-64 overscroll-contain" |
| 203 | + @keydown="handleKeydown" |
| 204 | + > |
| 205 | + <button |
| 206 | + v-for="(item, index) in menuItems" |
| 207 | + :key="item.id" |
| 208 | + role="menuitem" |
| 209 | + type="button" |
| 210 | + class="w-full flex items-center gap-2 px-3 py-2 text-sm text-fg-muted transition-colors duration-150" |
| 211 | + :class="[ |
| 212 | + highlightedIndex === index |
| 213 | + ? 'bg-bg-elevated text-fg' |
| 214 | + : 'hover:bg-bg-elevated hover:text-fg', |
| 215 | + ]" |
| 216 | + @click="handleAction(item.id)" |
| 217 | + @mouseenter="highlightedIndex = index" |
| 218 | + > |
| 219 | + <span :class="item.icon" class="w-4 h-4" aria-hidden="true" /> |
| 220 | + {{ item.label }} |
| 221 | + </button> |
| 222 | + </div> |
| 223 | + </Transition> |
| 224 | + </Teleport> |
| 225 | +</template> |
0 commit comments