Skip to content

Commit 124dada

Browse files
committed
feat: support any value
1 parent 18b79c2 commit 124dada

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

docs/app/components/BadgeGeneratorParameters.vue

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { copy, copied } = useClipboard({ copiedDuring: 2000 })
99
1010
const labelColor = useState('badge-label-color', () => '')
1111
const labelText = useState('badge-label-text', () => '')
12+
const badgeValue = useState('badge-value', () => '')
1213
const badgeColor = useState('badge-color', () => '')
1314
const usePkgName = useState('badge-use-name', () => false)
1415
const badgeStyle = useState('badge-style', () => 'default')
@@ -60,6 +61,7 @@ const queryParams = computed(() => {
6061
if (labelColor.value) params.append('labelColor', cleanHex(labelColor.value))
6162
if (badgeColor.value) params.append('color', cleanHex(badgeColor.value))
6263
if (badgeStyle.value !== 'default') params.append('style', badgeStyle.value)
64+
if (badgeValue.value) params.append('value', badgeValue.value)
6365
6466
if (usePkgName.value) {
6567
params.append('name', 'true')
@@ -169,21 +171,46 @@ const copyToClipboard = async () => {
169171
170172
<div class="h-px bg-gray-200 dark:bg-white/5 w-full" />
171173
172-
<div class="grid grid-cols-1 sm:grid-cols-3 gap-6">
173-
<div class="flex flex-col gap-1.5">
174-
<div class="flex items-center justify-between min-h-3.75">
175-
<label class="text-[10px] font-bold uppercase text-gray-400 ml-1">Label Text</label>
176-
<span v-if="usePkgName" class="text-[9px] text-emerald-500 font-medium transition-opacity"
177-
>Auto-Name Enabled</span
178-
>
174+
<div class="grid grid-cols-1 sm:grid-cols-4 gap-6">
175+
<div class="flex flex-col gap-1.5">
176+
<label class="text-[10px] font-bold uppercase text-gray-400 ml-1">Label Text</label>
177+
<div class="relative group">
178+
<input
179+
v-model="labelText"
180+
:disabled="usePkgName"
181+
type="text"
182+
placeholder="Custom Label"
183+
class="w-full px-3 py-2 h-9 rounded-lg border border-gray-200 dark:border-white/10 bg-white dark:bg-black/20 text-xs outline-none focus:border-emerald-500 disabled:cursor-not-allowed transition-all"
184+
:class="{ 'opacity-50 grayscale pl-3': usePkgName }"
185+
/>
186+
187+
<transition
188+
enter-active-class="transition duration-200 ease-out"
189+
enter-from-class="opacity-0 scale-95"
190+
enter-to-class="opacity-100 scale-100"
191+
leave-active-class="transition duration-150 ease-in"
192+
leave-from-class="opacity-100 scale-100"
193+
leave-to-class="opacity-0 scale-95"
194+
>
195+
<div
196+
v-if="usePkgName"
197+
class="absolute right-1.5 top-1/2 -translate-y-1/2 pointer-events-none"
198+
>
199+
<span class="flex items-center gap-1 px-1.5 py-0.5 rounded-md bg-emerald-500/10 dark:bg-emerald-500/20 text-emerald-500 text-[9px] font-bold uppercase tracking-tighter border border-emerald-500/20">
200+
Auto
201+
</span>
202+
</div>
203+
</transition>
204+
</div>
179205
</div>
206+
207+
<div class="flex flex-col gap-1.5">
208+
<label class="text-[10px] font-bold uppercase text-gray-400 ml-1">Badge Value</label>
180209
<input
181-
v-model="labelText"
182-
:disabled="usePkgName"
183-
type="text"
184-
placeholder="Custom Label"
185-
class="px-3 py-2 h-9 rounded-lg border border-gray-200 dark:border-white/10 bg-white dark:bg-black/20 text-xs outline-none focus:border-emerald-500 disabled:cursor-not-allowed transition-all"
186-
:class="{ 'opacity-30 grayscale': usePkgName }"
210+
v-model="badgeValue"
211+
type="text"
212+
placeholder="Override Value"
213+
class="px-3 py-2 h-9 rounded-lg border border-gray-200 dark:border-white/10 bg-white dark:bg-black/20 text-xs outline-none focus:border-emerald-500 transition-all"
187214
/>
188215
</div>
189216

docs/content/2.guide/6.badges.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,27 @@ Use this generator to get the Markdown code you desire:
6969

7070
:badge-generator-parameters
7171

72-
### `labelColor`
72+
### `label`
7373

74-
Overrides the default label color. You can pass a standard hex code (with or without the `#` prefix). The label text color is automatically chosen (black or white) based on WCAG contrast ratio, so the badge remains readable.
74+
Overrides the default label text. You can pass any string to customize the label displayed on the left half of the badge.
7575

76-
- **Default**: `#0a0a0a`
77-
- **Usage**: `?labelColor=HEX_CODE`
76+
- **Default**: Depends on the badge type (e.g., "version", "downloads/mo").
77+
- **Usage**: `?label=YOUR_LABEL`
7878

79-
### `label`
79+
### `value`
8080

81-
Overrides the default label text. You can pass any string to customize the label displayed on the badge.
81+
Overrides the default value text of the badge. You can pass any string to customize the value displayed on the right half of the badge.
8282

83-
- **Default**: Depends on the badge type (e.g., "version", "downloads/mo").
83+
- **Default**: Calculated values depending on the badge type (e.g., "v4.2.0", "5.4M").
8484
- **Usage**: `?label=YOUR_LABEL`
8585

86+
### `labelColor`
87+
88+
Overrides the default label color. You can pass a standard hex code (with or without the `#` prefix). The label text color is automatically chosen (black or white) based on WCAG contrast ratio, so the badge remains readable.
89+
90+
- **Default**: `#0a0a0a`
91+
- **Usage**: `?labelColor=HEX_CODE`
92+
8693
### `color`
8794

8895
Overrides the default strategy color. You can pass a standard hex code (with or without the `#` prefix). The text color is automatically chosen (black or white) based on WCAG contrast ratio, so the badge remains readable.

server/api/registry/badge/[type]/[...pkg].get.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const SafeColorSchema = v.pipe(
2323
const QUERY_SCHEMA = v.object({
2424
name: v.optional(v.string()),
2525
label: v.optional(SafeStringSchema),
26+
value: v.optional(SafeStringSchema),
2627
color: v.optional(SafeColorSchema),
2728
labelColor: v.optional(SafeColorSchema),
2829
})
@@ -452,6 +453,7 @@ export default defineCachedEventHandler(
452453
const labelColor = queryParams.success ? queryParams.output.labelColor : undefined
453454
const showName = queryParams.success && queryParams.output.name === 'true'
454455
const userLabel = queryParams.success ? queryParams.output.label : undefined
456+
const userValue = queryParams.success ? queryParams.output.value : undefined
455457
const badgeStyleResult = v.safeParse(BadgeStyleSchema, query.style)
456458
const badgeStyle = badgeStyleResult.success ? badgeStyleResult.output : 'default'
457459

@@ -465,7 +467,7 @@ export default defineCachedEventHandler(
465467
const strategyResult = await strategy(pkgData, requestedVersion)
466468

467469
const finalLabel = userLabel ? userLabel : showName ? packageName : strategyResult.label
468-
const finalValue = strategyResult.value
470+
const finalValue = userValue ? userValue : strategyResult.value
469471

470472
const rawColor = userColor ?? strategyResult.color
471473
const finalColor = rawColor?.startsWith('#') ? rawColor : `#${rawColor}`

0 commit comments

Comments
 (0)