Skip to content

Commit a1821ff

Browse files
committed
feat: add label formatter and make clipboard robust
1 parent 381114c commit a1821ff

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

docs/app/components/BadgeGenerator.vue

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,36 @@ const isValid = ref(true)
55
const copied = ref(false)
66
77
const types = [
8-
'version',
9-
'license',
10-
'size',
11-
'downloads',
12-
'downloads-day',
13-
'downloads-week',
14-
'downloads-month',
15-
'downloads-year',
16-
'vulnerabilities',
17-
'dependencies',
18-
'created',
19-
'updated',
20-
'engines',
21-
'types',
22-
'maintainers',
23-
'deprecated',
24-
'quality',
25-
'popularity',
26-
'maintenance',
27-
'score',
28-
'name',
8+
'version', 'license', 'size', 'downloads', 'downloads-day',
9+
'downloads-week', 'downloads-month', 'downloads-year',
10+
'vulnerabilities', 'dependencies', 'created', 'updated',
11+
'engines', 'types', 'maintainers', 'deprecated',
12+
'quality', 'popularity', 'maintenance', 'score', 'name',
2913
]
3014
3115
watch(pkg, () => {
3216
isValid.value = true
3317
})
3418
19+
const formatLabel = (str) => {
20+
if (!str || typeof str !== 'string') return ''
21+
return str
22+
.split('-')
23+
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
24+
.join(' per ')
25+
}
26+
3527
const copyToClipboard = async () => {
3628
const markdown = `[![Open on npmx.dev](https://npmx.dev/api/registry/badge/${type.value}/${pkg.value})](https://npmx.dev/package/${pkg.value})`
37-
await navigator.clipboard.writeText(markdown)
38-
39-
copied.value = true
40-
setTimeout(() => {
41-
copied.value = false
42-
}, 2000)
29+
try {
30+
await navigator.clipboard.writeText(markdown)
31+
copied.value = true
32+
setTimeout(() => {
33+
copied.value = false
34+
}, 2000)
35+
} catch {
36+
console.error('Failed to copy to clipboard')
37+
}
4338
}
4439
</script>
4540

@@ -70,7 +65,7 @@ const copyToClipboard = async () => {
7065
v-model="type"
7166
class="w-full h-10.5 px-4 py-2 rounded-lg border border-gray-200 dark:border-white/10 bg-white dark:bg-black/20 focus:ring-2 focus:ring-emerald-500/20 focus:border-emerald-500 outline-none text-sm transition-all appearance-none cursor-pointer"
7267
>
73-
<option v-for="t in types" :key="t" :value="t" class="dark:bg-gray-900">{{ t }}</option>
68+
<option v-for="t in types" :key="t" :value="t" class="dark:bg-gray-900">{{ formatLabel(t) }}</option>
7469
</select>
7570
<span
7671
class="absolute right-3 top-1/2 -translate-y-1/2 i-lucide-chevron-down w-4 h-4 text-gray-400 pointer-events-none"

docs/app/components/BadgeGeneratorParameters.vue

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,25 @@ watch([pkg, type, queryParams], () => {
7777
isValid.value = true
7878
})
7979
80+
const formatLabel = (str) => {
81+
if (!str || typeof str !== 'string') return ''
82+
return str
83+
.split('-')
84+
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
85+
.join(' per ')
86+
}
87+
8088
const copyToClipboard = async () => {
8189
const markdown = `[![Open on npmx.dev](${badgeUrl.value})](https://npmx.dev/package/${pkg.value})`
82-
await navigator.clipboard.writeText(markdown)
83-
84-
copied.value = true
85-
setTimeout(() => {
86-
copied.value = false
87-
}, 2000)
90+
try {
91+
await navigator.clipboard.writeText(markdown)
92+
copied.value = true
93+
setTimeout(() => {
94+
copied.value = false
95+
}, 2000)
96+
} catch {
97+
console.error('Failed to copy to clipboard')
98+
}
8899
}
89100
</script>
90101
@@ -110,11 +121,8 @@ const copyToClipboard = async () => {
110121
>Badge Type</label
111122
>
112123
<div class="relative">
113-
<select
114-
v-model="type"
115-
class="w-full h-10.5 px-4 py-2 rounded-lg border border-gray-200 dark:border-white/10 bg-white dark:bg-black/20 focus:ring-2 focus:ring-emerald-500/20 focus:border-emerald-500 outline-none text-sm transition-all appearance-none cursor-pointer"
116-
>
117-
<option v-for="t in types" :key="t" :value="t" class="dark:bg-gray-900">{{ t }}</option>
124+
<select v-model="type" class="w-full h-10.5 px-4 py-2 rounded-lg border border-gray-200 dark:border-white/10 bg-white dark:bg-black/20 focus:ring-2 focus:ring-emerald-500/20 focus:border-emerald-500 outline-none text-sm transition-all appearance-none cursor-pointer">
125+
<option v-for="t in types" :key="t" :value="t" class="dark:bg-gray-900">{{ formatLabel(t) }}</option>
118126
</select>
119127
<span
120128
class="absolute right-3 top-1/2 -translate-y-1/2 i-lucide-chevron-down w-4 h-4 text-gray-400 pointer-events-none"

0 commit comments

Comments
 (0)