Badge

The red count capsule, and tinted or filled tags.

controls

Apple: Badges

· Base UI: element

Count and tags

3128NewBeta
import { Badge } from "@applecn/ui/components/badge"

export default function BadgeBasic() {
  return (
    <div className="flex items-center gap-4">
      <Badge>3</Badge>
      <Badge>128</Badge>
      <Badge variant="tag">New</Badge>
      <Badge variant="filled">Beta</Badge>
    </div>
  )
}

Install

From the registry this site publishes, with dependencies resolved by the CLI.

npx shadcn@latest add https://applecn.vercel.app/r/badge.json

Source

packages/ui/src/components/badge.tsx

import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import type { ComponentProps } from "react"

/**
 * Badges. `count` is the red capsule a tab bar or app icon wears for unread counts; `tag` and
 * `filled` are the tinted and solid capsules used for labels and status.
 */
const badgeVariants = cva(
  "inline-flex shrink-0 items-center justify-center rounded-full whitespace-nowrap tabular-nums",
  {
    variants: {
      variant: {
        count:
          "h-(--badge-height) min-w-(--badge-min-width) bg-system-red px-1.5 type-footnote font-medium text-white",
        tag: "h-(--badge-height) min-w-(--badge-min-width) bg-primary/15 px-2 type-caption-1 font-semibold text-primary",
        filled:
          "h-(--badge-height) min-w-(--badge-min-width) bg-primary px-2 type-caption-1 font-semibold text-primary-foreground",
      },
    },
    defaultVariants: {
      variant: "count",
    },
  }
)

type BadgeProps = ComponentProps<"span"> &
  VariantProps<typeof badgeVariants> & {
    /** Announce value changes to assistive technology. */
    live?: boolean
  }

function Badge({
  className,
  variant = "count",
  live = false,
  ...props
}: BadgeProps) {
  return (
    <span
      data-slot="badge"
      data-variant={variant}
      {...(live ? { role: "status", "aria-live": "polite" } : {})}
      className={cn(badgeVariants({ variant }), className)}
      {...props}
    />
  )
}

export { Badge, badgeVariants }
export type { BadgeProps }