Skip to content

Commit a66454e

Browse files
fix(badge): add breathing room for fallback value segment
1 parent 9c66f01 commit a66454e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const COLORS = {
4343

4444
const BADGE_PADDING_X = 8
4545
const MIN_BADGE_TEXT_WIDTH = 40
46+
const FALLBACK_VALUE_EXTRA_PADDING_X = 4
4647
const SHIELDS_LABEL_PADDING_X = 5
4748

4849
const BADGE_FONT_SHORTHAND = 'normal normal 400 11px Geist, system-ui, -apple-system, sans-serif'
@@ -132,14 +133,17 @@ function measureTextWidth(text: string, font: string): number | null {
132133
return null
133134
}
134135

135-
function measureDefaultTextWidth(text: string): number {
136+
function measureDefaultTextWidth(text: string, fallbackExtraPadding = 0): number {
136137
const measuredWidth = measureTextWidth(text, BADGE_FONT_SHORTHAND)
137138

138139
if (measuredWidth !== null) {
139140
return Math.max(MIN_BADGE_TEXT_WIDTH, measuredWidth + BADGE_PADDING_X * 2)
140141
}
141142

142-
return Math.max(MIN_BADGE_TEXT_WIDTH, estimateTextWidth(text, 'default') + BADGE_PADDING_X * 2)
143+
return Math.max(
144+
MIN_BADGE_TEXT_WIDTH,
145+
estimateTextWidth(text, 'default') + BADGE_PADDING_X * 2 + fallbackExtraPadding,
146+
)
143147
}
144148

145149
function escapeXML(str: string): string {
@@ -188,7 +192,7 @@ function renderDefaultBadgeSvg(params: {
188192
const { finalColor, finalLabel, finalLabelColor, finalValue, labelTextColor, valueTextColor } =
189193
params
190194
const leftWidth = finalLabel.trim().length === 0 ? 0 : measureDefaultTextWidth(finalLabel)
191-
const rightWidth = measureDefaultTextWidth(finalValue)
195+
const rightWidth = measureDefaultTextWidth(finalValue, FALLBACK_VALUE_EXTRA_PADDING_X)
192196
const totalWidth = leftWidth + rightWidth
193197
const height = 20
194198
const escapedLabel = escapeXML(finalLabel)

0 commit comments

Comments
 (0)