Badge for Astro
Small Astro labels for status, category, and counts.
Installation
bunx --bun shadcn@latest add @gnostic/badgenpx shadcn@latest add @gnostic/badgepnpm dlx shadcn@latest add @gnostic/badgeCopy the files
src/components/ui/badge/badge-variants.tssrc/components/ui/badge/badge.astrosrc/components/ui/badge/badge-group.astrosrc/components/ui/badge/index.ts
Usage
import { Badge } from "@/components/ui/badge"
<Badge variant="default">Badge</Badge>
Link
Use the polymorphic as prop to render the badge as a link element.
---
import { Badge } from "@/components/ui/badge"
---
<Badge as="a" href="/">Badge</Badge>
Polymorphic
Badge is strict polymorphic. Pass as to change the rendered element and
use attributes for that element.
<Badge as="button" type="button">Action</Badge>
<Badge as="a" href="/docs">Docs</Badge>
Notes
- Use variants first before adding custom classes.
Badgeworks well for status labels, counts, and small inline metadata.
Shape
shape accepts default and rounded. default keeps the badge radius. rounded makes a pill.
With an icon
Put a check icon in the badge. The badge sets the icon size.
With a count
Set shape to rounded when the badge shows a notification count.
Badge group
Put a badge at the start or end of a short label.
API Reference
Read the installed source for the complete prop types.
When to use Badge
Use Badge for a short fact attached to another object. Keep the text brief and choose a semantic variant only when the meaning is clear. The size prop changes density. The shape prop changes the radius.
Keyboard and behavior
A badge is a label, not an action by default. Do not depend on its color alone to communicate status. If you make it a link through its supported element props, use a clear destination label and keep the focus style.
Theme and layout
The variants share semantic color tokens with alerts and buttons. Test the small size with the actual label text. Long sentences belong in a description or alert instead of a badge.