Skip to content

Commit 4796b78

Browse files
authored
refactor: prefer VueUse, reuse formatBytes (#612)
1 parent ec831b8 commit 4796b78

9 files changed

Lines changed: 90 additions & 101 deletions

File tree

app/components/CodeDirectoryListing.vue

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts">
22
import type { PackageFileTree } from '#shared/types'
33
import { getFileIcon } from '~/utils/file-icons'
4+
import { formatBytes } from '~/utils/formatters'
45
56
const props = defineProps<{
67
tree: PackageFileTree[]
@@ -35,13 +36,6 @@ const parentPath = computed(() => {
3536
if (parts.length <= 1) return ''
3637
return parts.slice(0, -1).join('/')
3738
})
38-
39-
// Format file size
40-
function formatBytes(bytes: number): string {
41-
if (bytes < 1024) return `${bytes} B`
42-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} kB`
43-
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
44-
}
4539
</script>
4640

4741
<template>

app/components/ColumnPicker.vue

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,23 @@ const menuRef = useTemplateRef('menuRef')
1616
const menuId = useId()
1717
1818
// Close on click outside (check both button and menu)
19-
function handleClickOutside(event: MouseEvent) {
20-
const target = event.target as Node
21-
const isOutsideButton = buttonRef.value && !buttonRef.value.contains(target)
22-
const isOutsideMenu = !menuRef.value || !menuRef.value.contains(target)
23-
if (isOutsideButton && isOutsideMenu) {
19+
onClickOutside(
20+
menuRef,
21+
() => {
2422
isOpen.value = false
25-
}
26-
}
23+
},
24+
{
25+
ignore: [buttonRef],
26+
},
27+
)
2728
2829
// Close on Escape key
29-
function handleKeydown(event: KeyboardEvent) {
30+
useEventListener('keydown', event => {
3031
if (event.key === 'Escape' && isOpen.value) {
3132
isOpen.value = false
3233
buttonRef.value?.focus()
3334
}
34-
}
35-
36-
onMounted(() => {
37-
document.addEventListener('click', handleClickOutside)
38-
document.addEventListener('keydown', handleKeydown)
3935
})
40-
41-
onUnmounted(() => {
42-
document.removeEventListener('click', handleClickOutside)
43-
document.removeEventListener('keydown', handleKeydown)
44-
})
45-
4636
// Columns that can be toggled (name is always visible)
4737
const toggleableColumns = computed(() => props.columns.filter(col => col.id !== 'name'))
4838

app/components/HeaderAccountMenu.client.vue

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,16 @@ const hasBothConnections = computed(() => isNpmConnected.value && !!atprotoUser.
2323
/** Only show count of active (pending/approved/running) operations */
2424
const operationCount = computed(() => activeOperations.value.length)
2525
26-
function handleClickOutside(event: MouseEvent) {
27-
const target = event.target as HTMLElement
28-
if (!target.closest('.account-menu')) {
29-
isOpen.value = false
30-
}
31-
}
26+
const accountMenuRef = useTemplateRef('accountMenuRef')
27+
28+
onClickOutside(accountMenuRef, () => {
29+
isOpen.value = false
30+
})
3231
33-
function handleKeydown(event: KeyboardEvent) {
32+
useEventListener('keydown', event => {
3433
if (event.key === 'Escape' && isOpen.value) {
3534
isOpen.value = false
3635
}
37-
}
38-
39-
onMounted(() => {
40-
document.addEventListener('click', handleClickOutside)
41-
})
42-
43-
onUnmounted(() => {
44-
document.removeEventListener('click', handleClickOutside)
4536
})
4637
4738
function openConnectorModal() {
@@ -56,7 +47,7 @@ function openAuthModal() {
5647
</script>
5748

5849
<template>
59-
<div class="account-menu relative" @keydown="handleKeydown">
50+
<div ref="accountMenuRef" class="relative">
6051
<button
6152
type="button"
6253
class="relative flex items-center justify-end gap-2 px-2 py-1.5 min-w-24 rounded-md transition-colors duration-200 hover:bg-bg-subtle focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50"

app/components/ScrollToTop.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ const isActive = computed(() => !excludedRoutes.has(route.name as string))
99
const isMounted = useMounted()
1010
const isVisible = shallowRef(false)
1111
const scrollThreshold = 300
12-
const supportsScrollStateQueries = useSupported(() => {
13-
return isMounted.value && CSS.supports('container-type', 'scroll-state')
14-
})
12+
const { isSupported: supportsScrollStateQueries } = useCssSupports(
13+
'container-type',
14+
'scroll-state',
15+
{ ssrValue: false },
16+
)
1517
1618
function onScroll() {
1719
if (!supportsScrollStateQueries.value) {

app/pages/[...package].vue

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { assertValidPackageName } from '#shared/utils/npm'
55
import { joinURL } from 'ufo'
66
import { areUrlsEquivalent } from '#shared/utils/url'
77
import { isEditableElement } from '~/utils/input'
8+
import { formatBytes } from '~/utils/formatters'
89
910
definePageMeta({
1011
name: 'package',
@@ -243,12 +244,6 @@ function normalizeGitUrl(url: string): string {
243244
.replace(/^git@github\.com:/, 'https://github.com/')
244245
}
245246
246-
function formatBytes(bytes: number): string {
247-
if (bytes < 1024) return `${bytes} B`
248-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} kB`
249-
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
250-
}
251-
252247
function getDependencyCount(version: PackumentVersion | null): number {
253248
if (!version?.dependencies) return 0
254249
return Object.keys(version.dependencies).length

app/pages/code/[...path].vue

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
PackageFileTreeResponse,
55
PackageFileContentResponse,
66
} from '#shared/types'
7+
import { formatBytes } from '~/utils/formatters'
78
89
definePageMeta({
910
name: 'code',
@@ -194,13 +195,6 @@ function packageRoute(ver?: string | null) {
194195
return { name: 'package' as const, params: { package: segments } }
195196
}
196197
197-
// Format file size
198-
function formatBytes(bytes: number): string {
199-
if (bytes < 1024) return `${bytes} B`
200-
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} kB`
201-
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
202-
}
203-
204198
// Line number click handler - update URL hash without scrolling
205199
function handleLineClick(lineNum: number, event: MouseEvent) {
206200
let newHash: string

app/utils/formatters.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,10 @@ export function formatCompactNumber(
4646

4747
return `${sign}${Math.round(abs)}`
4848
}
49+
50+
// Format file size
51+
export function formatBytes(bytes: number): string {
52+
if (bytes < 1024) return `${bytes} B`
53+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} kB`
54+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`
55+
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
6565
"@vite-pwa/assets-generator": "1.0.2",
6666
"@vite-pwa/nuxt": "1.1.0",
6767
"@voidzero-dev/vite-plus-core": "0.0.0-833c515fa25cef20905a7f9affb156dfa6f151ab",
68-
"@vueuse/core": "14.1.0",
69-
"@vueuse/nuxt": "14.1.0",
70-
"@vueuse/router": "^14.1.0",
68+
"@vueuse/core": "14.2.0",
69+
"@vueuse/nuxt": "14.2.0",
70+
"@vueuse/router": "^14.2.0",
7171
"marked": "17.0.1",
7272
"module-replacements": "2.11.0",
7373
"nuxt": "4.3.0",

0 commit comments

Comments
 (0)