Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Badge for Astro

Small Astro labels for status, category, and counts.

default
default
default
secondary
secondary
secondary
success
success
success
warning
warning
warning
info
info
info
destructive
destructive
destructive
error
error
error
outline
outline
outline
ghost
ghost
ghost

Installation

bunx --bun shadcn@latest add @gnostic/badge
npx shadcn@latest add @gnostic/badge
pnpm dlx shadcn@latest add @gnostic/badge

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/badge/badge-variants.ts
import { cva, type VariantProps } from 'class-variance-authority'
import { cn } from 'cn'

const badgeStyles = cva("font-sans focus-visible:ring-ring focus-visible:ring-offset-background relative inline-flex shrink-0 items-center justify-center gap-1 rounded-sm border border-transparent font-medium whitespace-nowrap transition-shadow outline-none focus-visible:ring-2 focus-visible:ring-offset-1 disabled:pointer-events-none disabled:opacity-64 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='opacity-'])]:opacity-80 [&_svg:not([class*='size-'])]:size-3.5 sm:[&_svg:not([class*='size-'])]:size-3 [button&,a&]:cursor-pointer [button&,a&]:pointer-coarse:after:absolute [button&,a&]:pointer-coarse:after:size-full [button&,a&]:pointer-coarse:after:min-h-11 [button&,a&]:pointer-coarse:after:min-w-11", {
    defaultVariants: {
        size: 'default',
        variant: 'default',
    },
    variants: {
        size: {
            default: "h-5.5 min-w-5.5 px-[calc(--spacing(1)-1px)] text-sm sm:h-4.5 sm:min-w-4.5 sm:text-xs",
            lg: "h-6.5 min-w-6.5 px-[calc(--spacing(1.5)-1px)] text-base sm:h-5.5 sm:min-w-5.5 sm:text-sm",
            sm: "h-5 min-w-5 rounded-[.25rem] px-[calc(--spacing(1)-1px)] text-xs sm:h-4 sm:min-w-4 sm:text-[.625rem]",
        },
        variant: {
            error: 'bg-destructive/8 text-destructive-foreground dark:bg-destructive/16',
            info: 'bg-info/8 text-info-foreground dark:bg-info/16',
            success: 'bg-success/8 text-success-foreground dark:bg-success/16',
            warning: 'bg-warning/8 text-warning-foreground dark:bg-warning/16',
            ghost: "text-foreground hover:bg-accent border-transparent",
            link: "text-primary border-transparent underline-offset-4 hover:underline",
            default: "bg-primary text-primary-foreground [button&,a&]:hover:bg-primary/90",
            destructive: "bg-destructive [button&,a&]:hover:bg-destructive/90 text-white",
            outline: "border-input bg-background text-foreground dark:bg-input/32 [button&,a&]:hover:bg-accent/50 dark:[button&,a&]:hover:bg-input/48",
            secondary: "bg-secondary text-secondary-foreground [button&,a&]:hover:bg-secondary/90",
        },
    },
})

export const badgeVariants = (...args: Parameters<typeof badgeStyles>) =>
    cn(badgeStyles(...args))

export type BadgeVariantProps = VariantProps<typeof badgeVariants>
src/components/ui/badge/badge.astro
---
import type { HTMLTag, Polymorphic } from "astro/types"

import { cn } from "cn"

import { badgeVariants, type BadgeVariantProps } from "./badge-variants"

type Props<Tag extends HTMLTag> = Polymorphic<{ as: Tag }> & BadgeVariantProps

const {
  as: Tag = "span",
  class: className,
  variant = "default",
  size = "default",
  ...props
} = Astro.props
---

<Tag
  data-slot="badge"
  data-variant={variant}
  data-size={size}
  class={cn(badgeVariants({ variant, size }), className)}
  {...props}
>
  <slot />
</Tag>
src/components/ui/badge/index.ts
export { badgeVariants, type BadgeVariantProps } from "./badge-variants"

export { default as Badge } from "./badge.astro"

Usage

import { Badge } from "@/components/ui/badge"
<Badge variant="default">Badge</Badge>

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.
  • Badge works well for status labels, counts, and small inline metadata.

With an icon

Put a check icon in the badge. The badge sets the icon size.

Verified Verified Verified

With a count

Add rounded-full when the badge shows a notification count.

8 3 12

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 without requiring a different component.

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.