-
-
Notifications
You must be signed in to change notification settings - Fork 274
feat: add package download button #1586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Adebesin-Cell
wants to merge
5
commits into
npmx-dev:main
Choose a base branch
from
Adebesin-Cell:feat/download-button
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+373
−40
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f5c594b
feat: Add download button component and install size types
Adebesin-Cell b21c8ac
fix: Update function to use correct translation function
Adebesin-Cell b3419bd
feat: Add tarball URLs for dependencies
Adebesin-Cell 07261cb
style: Add tarball URLs to test dependencies
Adebesin-Cell dafc475
feat(Button, PackageDownloadButton): Add async download functionality
Adebesin-Cell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,244 @@ | ||
| <script setup lang="ts"> | ||
| import type { SlimPackumentVersion, InstallSizeResult } from '#shared/types' | ||
| import { onClickOutside, useEventListener } from '@vueuse/core' | ||
|
|
||
| const props = withDefaults( | ||
| defineProps<{ | ||
| packageName: string | ||
| version: SlimPackumentVersion | ||
| installSize: InstallSizeResult | null | ||
| size?: 'small' | 'medium' | ||
| }>(), | ||
| { | ||
| size: 'medium', | ||
| }, | ||
| ) | ||
|
|
||
| const triggerRef = useTemplateRef('triggerRef') | ||
| const listRef = useTemplateRef('listRef') | ||
| const isOpen = shallowRef(false) | ||
| const highlightedIndex = shallowRef(-1) | ||
| const dropdownPosition = shallowRef<{ top: number; right: number } | null>(null) | ||
|
|
||
| const { t } = useI18n() | ||
| const menuId = useId() | ||
| const menuItems = computed(() => [ | ||
| { id: 'package', label: t('package.download.package'), icon: 'i-lucide:package' }, | ||
| { id: 'dependencies', label: t('package.download.dependencies'), icon: 'i-lucide:list-tree' }, | ||
| ]) | ||
|
|
||
| function getDropdownStyle(): Record<string, string> { | ||
| if (!dropdownPosition.value) return {} | ||
| return { | ||
| top: `${dropdownPosition.value.top}px`, | ||
| right: `${document.documentElement.clientWidth - dropdownPosition.value.right}px`, | ||
| } | ||
| } | ||
|
|
||
| function toggle() { | ||
| if (isOpen.value) { | ||
| close() | ||
| } else { | ||
| const rect = triggerRef.value?.$el?.getBoundingClientRect() | ||
| if (rect) { | ||
| dropdownPosition.value = { | ||
| top: rect.bottom + 4, | ||
| right: rect.right, | ||
| } | ||
| } | ||
| isOpen.value = true | ||
| highlightedIndex.value = 0 | ||
| } | ||
| } | ||
|
|
||
| function close() { | ||
| isOpen.value = false | ||
| highlightedIndex.value = -1 | ||
| } | ||
|
|
||
| onClickOutside(listRef, close, { ignore: [triggerRef] }) | ||
|
|
||
| function handleKeydown(event: KeyboardEvent) { | ||
| if (!isOpen.value) { | ||
| if (event.key === 'ArrowDown' || event.key === 'Enter' || event.key === ' ') { | ||
| event.preventDefault() | ||
| toggle() | ||
| } | ||
| return | ||
| } | ||
|
|
||
| switch (event.key) { | ||
| case 'ArrowDown': | ||
| event.preventDefault() | ||
| highlightedIndex.value = (highlightedIndex.value + 1) % menuItems.value.length | ||
| break | ||
| case 'ArrowUp': | ||
| event.preventDefault() | ||
| highlightedIndex.value = | ||
| highlightedIndex.value <= 0 ? menuItems.value.length - 1 : highlightedIndex.value - 1 | ||
| break | ||
| case 'Enter': | ||
| case ' ': | ||
| event.preventDefault() | ||
| handleAction(menuItems.value[highlightedIndex.value]?.id) | ||
| break | ||
| case 'Escape': | ||
| event.preventDefault() | ||
| close() | ||
| triggerRef.value?.$el?.focus() | ||
| break | ||
| case 'Tab': | ||
| close() | ||
| break | ||
| } | ||
| } | ||
|
|
||
| function handleAction(id: string | undefined) { | ||
| if (id === 'package') { | ||
| downloadPackage() | ||
| } else if (id === 'dependencies') { | ||
| downloadDependenciesScript() | ||
| } | ||
| close() | ||
| } | ||
|
|
||
| async function downloadPackage() { | ||
| const tarballUrl = props.version.dist.tarball | ||
| if (!tarballUrl) return | ||
|
|
||
| try { | ||
| const response = await fetch(tarballUrl) | ||
| const blob = await response.blob() | ||
| const url = URL.createObjectURL(blob) | ||
| const link = document.createElement('a') | ||
| link.href = url | ||
| link.download = `${props.packageName.replace(/\//g, '__')}-${props.version.version}.tgz` | ||
| document.body.appendChild(link) | ||
| link.click() | ||
| document.body.removeChild(link) | ||
| URL.revokeObjectURL(url) | ||
| } catch (error) { | ||
| console.error('Failed to download package:', error) | ||
| // Fallback to direct link for non-CORS or other issues, though download attribute may be ignored | ||
| const link = document.createElement('a') | ||
| link.href = tarballUrl | ||
| link.download = `${props.packageName.replace(/\//g, '__')}-${props.version.version}.tgz` | ||
| document.body.appendChild(link) | ||
| link.click() | ||
| document.body.removeChild(link) | ||
| } | ||
| } | ||
|
|
||
| function downloadDependenciesScript() { | ||
| if (!props.installSize) return | ||
|
|
||
| const lines = [ | ||
| '#!/bin/bash', | ||
| `# Download dependencies for ${props.packageName}@${props.version.version}`, | ||
| 'mkdir -p node_modules', | ||
| '', | ||
| ] | ||
|
|
||
| // Add root package | ||
| const rootTarball = props.version.dist.tarball | ||
| if (rootTarball) { | ||
| lines.push(`# ${props.packageName}@${props.version.version}`) | ||
| lines.push( | ||
| `curl -L "${rootTarball}" -o "${props.packageName.replace(/\//g, '__')}-${props.version.version}.tgz"`, | ||
| ) | ||
| } | ||
|
|
||
| // Add dependencies | ||
| props.installSize.dependencies.forEach(dep => { | ||
| if (!dep.tarballUrl) return | ||
| lines.push(`# ${dep.name}@${dep.version}`) | ||
| lines.push( | ||
| `curl -L "${dep.tarballUrl}" -o "${dep.name.replace(/\//g, '__')}-${dep.version}.tgz"`, | ||
| ) | ||
| }) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const blob = new Blob([lines.join('\n')], { type: 'text/x-shellscript' }) | ||
| const url = URL.createObjectURL(blob) | ||
| const link = document.createElement('a') | ||
| link.href = url | ||
| link.download = `download-${props.packageName.replace(/\//g, '__')}-deps.sh` | ||
| document.body.appendChild(link) | ||
| link.click() | ||
| document.body.removeChild(link) | ||
| URL.revokeObjectURL(url) | ||
| } | ||
|
|
||
| const prefersReducedMotion = useMediaQuery('(prefers-reduced-motion: reduce)') | ||
|
|
||
| useEventListener('scroll', () => isOpen.value && close(), { passive: true }) | ||
|
|
||
| defineOptions({ | ||
| inheritAttrs: false, | ||
| }) | ||
| </script> | ||
|
|
||
| <template> | ||
| <ButtonBase | ||
| ref="triggerRef" | ||
| v-bind="$attrs" | ||
| type="button" | ||
| :variant="size === 'small' ? 'subtle' : 'secondary'" | ||
| :size="size" | ||
| classicon="i-lucide:download" | ||
| :aria-expanded="isOpen" | ||
| aria-haspopup="menu" | ||
| :aria-controls="menuId" | ||
| @click="toggle" | ||
| @keydown="handleKeydown" | ||
| > | ||
| {{ $t('package.download.button') }} | ||
| <span | ||
| class="i-lucide:chevron-down ms-1" | ||
| :class="[ | ||
| size === 'small' ? 'w-3 h-3' : 'w-3.5 h-3.5', | ||
| { 'rotate-180': isOpen }, | ||
| prefersReducedMotion ? '' : 'transition-transform duration-200', | ||
| ]" | ||
| aria-hidden="true" | ||
| /> | ||
| </ButtonBase> | ||
|
|
||
| <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'" | ||
| > | ||
| <div | ||
| v-if="isOpen" | ||
| :id="menuId" | ||
| ref="listRef" | ||
| role="menu" | ||
| :style="getDropdownStyle()" | ||
| class="fixed bg-bg-subtle border border-border rounded-md shadow-lg z-50 py-1 w-64 overscroll-contain" | ||
| @keydown="handleKeydown" | ||
| > | ||
| <button | ||
| v-for="(item, index) in menuItems" | ||
| :key="item.id" | ||
| role="menuitem" | ||
| type="button" | ||
| class="w-full flex items-center gap-2 px-3 py-2 text-sm text-fg-muted transition-colors duration-150" | ||
| :class="[ | ||
| highlightedIndex === index | ||
| ? 'bg-bg-elevated text-fg' | ||
| : 'hover:bg-bg-elevated hover:text-fg', | ||
| ]" | ||
| @click="handleAction(item.id)" | ||
| @mouseenter="highlightedIndex = index" | ||
| > | ||
| <span :class="item.icon" class="w-4 h-4" aria-hidden="true" /> | ||
| {{ item.label }} | ||
| </button> | ||
| </div> | ||
| </Transition> | ||
| </Teleport> | ||
| </template> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.