Skip to content

feat: improve badge customization with dynamic text based on contrast#1979

Open
sandros94 wants to merge 4 commits intonpmx-dev:mainfrom
sandros94:feat/improve-badge-customization
Open

feat: improve badge customization with dynamic text based on contrast#1979
sandros94 wants to merge 4 commits intonpmx-dev:mainfrom
sandros94:feat/improve-badge-customization

Conversation

@sandros94
Copy link
Contributor

🔗 Linked issue

Followup from working on the unjs/automd#137 integration

Reproduction:

https://npmx.dev/api/registry/badge/downloads/unjwt?color=FFDC3B&labelColor=CCC

Broken rendering

📚 Description

Currently the badges have the text hardcoded to white, we can simply add a WCAG luminance evaluation to properly decide if the text should be white or black depending on the color input

Also added proper hex color validation for badges.

@vercel
Copy link

vercel bot commented Mar 7, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
npmx.dev Ready Ready Preview, Comment Mar 7, 2026 11:25pm
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs.npmx.dev Ignored Ignored Preview Mar 7, 2026 11:25pm
npmx-lunaria Ignored Ignored Mar 7, 2026 11:25pm

Request Review

@sandros94 sandros94 changed the title feat(api): improve badge customization with dynamic text based on contrast feat: improve badge customization with dynamic text based on contrast Mar 7, 2026
@codecov
Copy link

codecov bot commented Mar 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 7, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e3e6da96-9be8-4c39-b553-3951c826eee7

📥 Commits

Reviewing files that changed from the base of the PR and between 5b2011b and 40f2aa8.

📒 Files selected for processing (1)
  • test/e2e/badge.spec.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/e2e/badge.spec.ts

📝 Walkthrough

Walkthrough

The PR adds automatic text-colour selection for badge label and value using WCAG contrast calculations. It validates and normalises colour inputs with SafeColorSchema, introduces toLinear and getContrastTextColor utilities, computes labelTextColor and valueTextColor from finalLabelColor and finalColor, and threads those into badge renderers. renderDefaultBadgeSvg and renderShieldsBadgeSvg signatures were extended to accept the computed text colours and their SVG output was adjusted. Documentation examples and an examples table were updated, and end-to-end tests were added to verify contrast behaviour and 3-character hex expansion.

Possibly related PRs

Suggested labels

a11y

Suggested reviewers

  • danielroe
  • ghostdevv
  • wojtekmaj
🚥 Pre-merge checks | ✅ 1
✅ Passed checks (1 passed)
Check name Status Explanation
Description check ✅ Passed The pull request description clearly relates to the changeset, detailing the contrast issue with badge text colors and the solution implemented.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
docs/content/2.guide/1.features.md (1)

162-162: Minor grammar improvement: add comma before 'so' in compound sentences.

Static analysis flags that a comma should precede 'so' when it connects two independent clauses.

📝 Suggested fix
-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.
+Overrides the default label color. You can pass a standard hex code (with or without the `#` prefix). The label text colour is automatically chosen (black or white) based on WCAG contrast ratio, so the badge remains readable.
-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.
+Overrides the default strategy color. You can pass a standard hex code (with or without the `#` prefix). The text colour is automatically chosen (black or white) based on WCAG contrast ratio, so the badge remains readable.

Also applies to: 176-176


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ac835d6d-5831-4cab-b294-b020da103551

📥 Commits

Reviewing files that changed from the base of the PR and between 0d952fa and 5b2011b.

📒 Files selected for processing (3)
  • docs/content/2.guide/1.features.md
  • server/api/registry/badge/[type]/[...pkg].get.ts
  • test/e2e/badge.spec.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants