"use client"; import * as React from "react"; import { Slot } from "@radix-ui/react-slot"; import { cva, type VariantProps } from "class-variance-authority"; import { CircleAlertIcon, CircleCheckIcon, CircleMinusIcon, InfoIcon, LoaderIcon, LockIcon, TriangleAlertIcon, type LucideIcon, } from "lucide-react"; import { cn } from "../lib/cn"; // The design system's Badge: a short status on a 10% tint, navy text and an // icon in the tone's colour, so the colour never says it alone. The seven // tones are its own names. The four the dashboard had stay, on these looks: // // default → the brand tag (Electric Blue, white text) // secondary → `neutral` without its icon // destructive → `error` // outline → white with a navy hairline const badgeVariants = cva( "text-primary-blue-100 focus-visible:outline-ring inline-flex w-fit max-w-full shrink-0 items-center gap-1.5 rounded-full border border-transparent px-2.5 py-0.5 text-[0.8125rem] leading-5 font-medium outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-solid has-[>svg:first-child]:pl-2 [&>svg]:pointer-events-none [&>svg]:shrink-0 [&>svg:not([class*='size-'])]:size-3.5", { variants: { variant: { default: "bg-primary text-primary-foreground", secondary: "bg-secondary", destructive: "bg-semantic-error-10 [&>svg:not([class*='text-'])]:text-semantic-error-100", outline: "border-primary-blue-20 bg-card", success: "bg-semantic-success-10 [&>svg:not([class*='text-'])]:text-semantic-success-120", warning: "bg-semantic-warning-10 [&>svg:not([class*='text-'])]:text-semantic-warning-120", error: "bg-semantic-error-10 [&>svg:not([class*='text-'])]:text-semantic-error-100", info: "bg-accent [&>svg:not([class*='text-'])]:text-primary", progress: "bg-accent [&>svg:not([class*='text-'])]:text-primary", neutral: "bg-secondary [&>svg:not([class*='text-'])]:text-muted-foreground", locked: "bg-secondary [&>svg:not([class*='text-'])]:text-muted-foreground", }, }, defaultVariants: { variant: "default", }, }, ); type BadgeVariant = NonNullable["variant"]>; /** The icon a tone carries unless the caller gives another one, or `null`. */ const TONE_ICONS: Partial> = { destructive: CircleAlertIcon, success: CircleCheckIcon, warning: TriangleAlertIcon, error: CircleAlertIcon, info: InfoIcon, progress: LoaderIcon, neutral: CircleMinusIcon, locked: LockIcon, }; type SpanProps = React.ComponentPropsWithoutRef<"span">; export interface BadgeProps extends SpanProps, VariantProps { asChild?: boolean; /** * The icon before the text. A tone has its own; pass another to replace it * or `null` for none. A badge whose content already starts with an element * (its own icon) gets none added, and neither does one drawn on its child * with `asChild`, which keeps the child's content as it is. */ icon?: LucideIcon | null; } export const Badge = React.forwardRef( ({ className, variant, asChild = false, icon, children, ...props }, ref) => { const Comp = asChild ? Slot : "span"; const bringsItsOwn = React.isValidElement( React.Children.toArray(children)[0], ); const Icon = icon !== undefined ? icon : asChild || bringsItsOwn ? null : (TONE_ICONS[variant ?? "default"] ?? null); // The `progress` tone's own loader turns, as the other loaders in the app // do; an icon the caller chose (a clock on a pending payment) stays still. const spins = icon === undefined && Icon === LoaderIcon; return ( {asChild ? ( children ) : ( <> {Icon && ( ); }, ); Badge.displayName = "Badge";