Skip to content

Badge Properties

Compact UI elements that highlight status, labels, or counts with various styles and colors. Customizable in size and appearance with support for disabled states.

Preview

Default

Installation

Add Badge to your project with the CK-UI CLI. It copies the source into your project, so the code is yours to change.

Terminal
npx ck-ui-cli@latest add badge
Adds
Badge.tsxto your CK-UI folder (src/components/ck-ui by default)
Installs
@radix-ui/react-slotif your project does not have them yet

First time with CK-UI in this project? Run npx ck-ui-cli@latest init once before adding components. The installation guide explains what it sets up.

Manual installationInstall the packages and copy the files yourself.

Install the following dependencies

Run these commands in your terminal to install the required packages

Terminal
npm install @radix-ui/react-slot

Create or update your Badge component

Copy this code to create your Badge component

/components/ui/Badge.tsx
import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cn } from "@/lib/utils";

// Server-compatible: no hooks or context, so Badge can be rendered from server components.

export type BadgeVariant = "filled" | "light" | "lighter" | "stroke";
/** Decorative palette (Badge is an explicit palette API, see CONVENTIONS.md). */
export type BadgeColor = "neutral" | "gray" | "blue" | "orange" | "red" | "green" | "yellow" | "purple" | "sky" | "pink" | "teal";
export type BadgeSize = "sm" | "md" | "lg";

/**
 * @deprecated These names never had styles of their own. They map to:
 * `default` → `filled`, `secondary` → `light`, `outline` → `stroke`, `destructive` → `filled` + `color="red"`.
 */
type LegacyBadgeVariant = "default" | "secondary" | "outline" | "destructive";
/** @deprecated Use `sm` / `md`. */
type LegacyBadgeSize = "small" | "medium";
/**
 * Sizes accepted by `size`. Any other string is applied as class names.
 * @deprecated Passing class names through `size` (e.g. `size="size-7"`) — use `className` instead.
 */
type BadgeSizeInput = BadgeSize | LegacyBadgeSize | (string & {});

const LEGACY_VARIANTS: Record<LegacyBadgeVariant, { variant: BadgeVariant; color?: BadgeColor }> = {
    default: { variant: "filled" },
    secondary: { variant: "light" },
    outline: { variant: "stroke" },
    destructive: { variant: "filled", color: "red" },
};
const LEGACY_SIZES: Record<LegacyBadgeSize, BadgeSize> = { small: "sm", medium: "md" };

const SIZE_STYLES: Record<BadgeSize, { root: string; icon: string }> = {
    sm: { root: "h-4 min-w-4 gap-1.5 px-1 uppercase size-subheading-2xs", icon: "-mx-1 size-3" },
    md: { root: "h-5 min-w-5 gap-1.5 px-2 size-label-xs capitalize", icon: "-mx-2 size-4" },
    lg: { root: "h-6 min-w-6 gap-1.5 px-2 size-label-xs capitalize", icon: "-mx-2 size-5" },
};

// Text is at least 4.5:1 on its background in both themes (WCAG AA, see color-contrast.test.ts): filled badges use the
// lightest shade white text passes on, lighter and stroke badges use -700 text in light mode.
const COLOR_STYLES: Record<BadgeVariant, Record<BadgeColor, string>> = {
    filled: {
        neutral: "bg-neutral-500 dark:bg-neutral-600 text-white",
        gray: "bg-gray-500 dark:bg-gray-600 text-white",
        blue: "bg-blue-600 text-white",
        orange: "bg-orange-700 text-white",
        red: "bg-red-600 text-white",
        green: "bg-green-700 text-white",
        yellow: "bg-yellow-700 text-white",
        purple: "bg-purple-600 text-white",
        sky: "bg-sky-700 text-white",
        pink: "bg-pink-600 text-white",
        teal: "bg-teal-700 text-white",
    },
    light: {
        neutral: "bg-neutral-100 dark:bg-neutral-800 text-neutral-800 dark:text-neutral-200",
        gray: "bg-gray-100 dark:bg-gray-800 text-gray-800 dark:text-gray-200",
        blue: "bg-blue-100 dark:bg-blue-800 text-blue-800 dark:text-blue-200",
        orange: "bg-orange-100 dark:bg-orange-800 text-orange-800 dark:text-orange-200",
        red: "bg-red-100 dark:bg-red-800 text-red-800 dark:text-red-200",
        green: "bg-green-100 dark:bg-green-800 text-green-800 dark:text-green-200",
        yellow: "bg-yellow-100 dark:bg-yellow-800 text-yellow-800 dark:text-yellow-200",
        purple: "bg-purple-100 dark:bg-purple-800 text-purple-800 dark:text-purple-200",
        sky: "bg-sky-100 dark:bg-sky-800 text-sky-800 dark:text-sky-200",
        pink: "bg-pink-100 dark:bg-pink-800 text-pink-800 dark:text-pink-200",
        teal: "bg-teal-100 dark:bg-teal-800 text-teal-800 dark:text-teal-200",
    },
    lighter: {
        neutral: "bg-neutral-100 dark:bg-neutral-900 text-neutral-700 dark:text-neutral-300",
        gray: "bg-gray-50 dark:bg-gray-900 text-gray-700 dark:text-gray-300",
        blue: "bg-blue-50 dark:bg-blue-900 text-blue-700 dark:text-blue-300",
        orange: "bg-orange-50 dark:bg-orange-900 text-orange-700 dark:text-orange-300",
        red: "bg-red-50 dark:bg-red-900 text-red-700 dark:text-red-300",
        green: "bg-green-50 dark:bg-green-900 text-green-700 dark:text-green-300",
        yellow: "bg-yellow-50 dark:bg-yellow-900 text-yellow-700 dark:text-yellow-300",
        purple: "bg-purple-50 dark:bg-purple-900 text-purple-700 dark:text-purple-300",
        sky: "bg-sky-50 dark:bg-sky-900 text-sky-700 dark:text-sky-300",
        pink: "bg-pink-50 dark:bg-pink-900 text-pink-700 dark:text-pink-300",
        teal: "bg-teal-50 dark:bg-teal-900 text-teal-700 dark:text-teal-300",
    },
    stroke: {
        neutral: "ring-1 ring-inset ring-neutral-200 dark:ring-neutral-700 text-neutral-700 dark:text-neutral-300",
        gray: "ring-1 ring-inset ring-gray-200 dark:ring-gray-700 text-gray-700 dark:text-gray-300",
        blue: "ring-1 ring-inset ring-blue-200 dark:ring-blue-700 text-blue-700 dark:text-blue-300",
        orange: "ring-1 ring-inset ring-orange-200 dark:ring-orange-700 text-orange-700 dark:text-orange-300",
        red: "ring-1 ring-inset ring-red-200 dark:ring-red-700 text-red-700 dark:text-red-300",
        green: "ring-1 ring-inset ring-green-200 dark:ring-green-700 text-green-700 dark:text-green-300",
        yellow: "ring-1 ring-inset ring-yellow-200 dark:ring-yellow-700 text-yellow-700 dark:text-yellow-300",
        purple: "ring-1 ring-inset ring-purple-200 dark:ring-purple-700 text-purple-700 dark:text-purple-300",
        sky: "ring-1 ring-inset ring-sky-200 dark:ring-sky-700 text-sky-700 dark:text-sky-300",
        pink: "ring-1 ring-inset ring-pink-200 dark:ring-pink-700 text-pink-700 dark:text-pink-300",
        teal: "ring-1 ring-inset ring-teal-200 dark:ring-teal-700 text-teal-700 dark:text-teal-300",
    },
};

const DISABLED_STYLES = "ring-1 ring-inset ring-border bg-transparent text-neutral-300 dark:text-neutral-600";

const hasKey = <T extends object>(record: T, key: string): key is Extract<keyof T, string> => Object.prototype.hasOwnProperty.call(record, key);

/** Resolves a size to a canonical step, or to legacy class names when it is not a known size. */
export function resolveBadgeSize(size?: BadgeSizeInput | null): { size?: BadgeSize; className?: string } {
    if (!size) return { size: "sm" };
    if (hasKey(SIZE_STYLES, size)) return { size };
    if (hasKey(LEGACY_SIZES, size)) return { size: LEGACY_SIZES[size] };
    return { className: size };
}

export function resolveBadgeVariant(variant?: BadgeVariant | LegacyBadgeVariant | null, color?: BadgeColor | null): { variant: BadgeVariant; color: BadgeColor } {
    if (variant && hasKey(LEGACY_VARIANTS, variant)) {
        const legacy = LEGACY_VARIANTS[variant];
        return { variant: legacy.variant, color: legacy.color ?? color ?? "neutral" };
    }
    const resolvedVariant = variant && hasKey(COLOR_STYLES, variant) ? variant : "filled";
    const resolvedColor = color && hasKey(COLOR_STYLES.filled, color) ? color : "neutral";
    return { variant: resolvedVariant, color: resolvedColor };
}

function sizeClasses(size: BadgeSizeInput | undefined, part: "root" | "icon") {
    const resolved = resolveBadgeSize(size);
    return resolved.size ? SIZE_STYLES[resolved.size][part] : resolved.className;
}

export interface BadgeRootProps extends Omit<React.HTMLAttributes<HTMLSpanElement>, "color"> {
    /** Render the single child element instead of a <span>, merging Badge's props onto it. */
    asChild?: boolean;
    variant?: BadgeVariant | LegacyBadgeVariant;
    color?: BadgeColor;
    size?: BadgeSizeInput;
    /** Muted, outlined appearance. Sets `aria-disabled` and `data-disabled`; it does not block events on an `asChild` element. */
    disabled?: boolean;
    /** Minimal horizontal padding, for counters and single characters. */
    square?: boolean;
    /** Pill shape (`rounded-full`) instead of `rounded-md`. */
    rounded?: boolean;
}

const BadgeRoot = React.forwardRef<HTMLSpanElement, BadgeRootProps>(
    ({ asChild = false, className, variant, color, size, disabled = false, square = false, rounded = false, children, ...props }, ref) => {
        const resolvedSize = resolveBadgeSize(size);
        const resolved = resolveBadgeVariant(variant, color);
        const Component = asChild ? Slot : "span";

        // Rounding comes before className so consumer classes (e.g. `rounded-full`) win.
        const classes = cn(
            "inline-flex items-center justify-center leading-none transition-colors duration-base ease-standard",
            resolvedSize.size ? SIZE_STYLES[resolvedSize.size].root : resolvedSize.className,
            square && "px-1",
            rounded ? "rounded-full" : "rounded-md",
            disabled ? DISABLED_STYLES : COLOR_STYLES[resolved.variant][resolved.color],
            className
        );

        return (
            <Component ref={ref} className={classes} data-size={resolvedSize.size} data-disabled={disabled ? "" : undefined} aria-disabled={disabled || undefined} {...props}>
                {children}
            </Component>
        );
    }
);
BadgeRoot.displayName = "Badge.Root";

type BadgeIconElement = React.ReactElement<React.HTMLAttributes<HTMLElement> & React.RefAttributes<HTMLElement>>;

export interface BadgeIconProps extends React.HTMLAttributes<HTMLElement> {
    /**
     * Icon component to render, e.g. `as={RiInformationFill}`.
     * A rendered element (`as={<RiInformationFill />}`) is still accepted; its own classes are merged first.
     */
    as?: React.ElementType | BadgeIconElement;
    /** Icon size. Defaults to `sm`; it does not follow Badge.Root's size. */
    size?: BadgeSizeInput;
}

const BadgeIcon = React.forwardRef<HTMLElement, BadgeIconProps>(({ as, className, size, ...props }, ref) => {
    const classes = cn("shrink-0", sizeClasses(size, "icon"), className);

    if (React.isValidElement<React.HTMLAttributes<HTMLElement> & React.RefAttributes<HTMLElement>>(as)) {
        return React.cloneElement(as, { "aria-hidden": true, ...props, ref, className: cn(as.props.className, classes) });
    }

    const Component = as ?? "span";
    return <Component ref={ref} aria-hidden="true" className={classes} {...props} />;
});
BadgeIcon.displayName = "Badge.Icon";

export interface BadgeDotProps extends React.HTMLAttributes<HTMLSpanElement> {
    /** Dot box size. Defaults to `sm`. */
    size?: BadgeSizeInput;
}

const BadgeDot = React.forwardRef<HTMLSpanElement, BadgeDotProps>(({ className, size, ...props }, ref) => {
    return (
        <span
            ref={ref}
            aria-hidden="true"
            className={cn("flex shrink-0 items-center justify-center before:h-1 before:w-1 before:rounded-full before:bg-current", sizeClasses(size, "icon"), className)}
            {...props}
        />
    );
});
BadgeDot.displayName = "Badge.Dot";

export { BadgeRoot as Root, BadgeIcon as Icon, BadgeDot as Dot };

Test Your Component

  • 1. Add the component to a test page
  • 2. Check all props and variants work as expected
  • 3. Verify responsive behavior
  • 4. Test with different content/data
  • 5. Ensure accessibility features work correctly

Colors and Styles

The Badge component supports various colors and styles to suit different UI contexts. Each style is available in multiple colors.

filled Style

The primary style for badges, offering high contrast and visibility.

grayblueorangeredgreenyellowpurpleskypinkteal

light Style

A softer variant that maintains color association while reducing visual weight.

grayblueorangeredgreenyellowpurpleskypinkteal

lighter Style

A softer variant that maintains color association while reducing visual weight.

grayblueorangeredgreenyellowpurpleskypinkteal

stroke Style

An outline style that provides a subtle yet distinct appearance.

grayblueorangeredgreenyellowpurpleskypinkteal

Rounded vs Default

Badges can have either rounded or square corners. Use the 'rounded' prop to switch between these styles.

DefaultRounded222222257257

Badge Sizes

Badges come in three sizes: sm (h-4, the default), md (h-5) and lg (h-6). Use the 'size' prop to switch between them.

sm Badgemd Badgelg Badge

Badges with Icons

Icons can be added to badges to provide additional visual cues. Icons can be positioned on the left or right side of the badge text.

left iconleft iconleft icon
right iconright iconright icon

Badges with Dots

Dots can be used instead of icons for a more subtle indicator. They're particularly useful for status indicators or notifications.

BadgeBadge

Status Badges

Status badges are used to indicate different states or levels of importance. They come with predefined colors and can be displayed with icons or dots in both rounded and default styles.

BadgeBadgeBadgeBadge
BadgeBadgeBadgeBadge
BadgeBadgeBadgeBadge
BadgeBadgeBadgeBadge
BadgeBadgeBadgeBadge

Custom Badge

You can create a custom badge with full class override capabilities.

Custom

Rounded Badges

Rounded badges provide a softer look with fully rounded corners, ideal for modern UI designs.

grayblueorangeredgreenyellowpurpleskypinkteal

API Reference

The Badge component provides a versatile way to display short status descriptors in your interface. It uses Radix UI's Slot primitive for composition when the asChild prop is used.

Badge.Root

Main badge component with support for various styles, colors, and sizes.

PropTypeDefaultDescription
size
"sm""md""lg"
"sm"

Size of the badge. `small` and `medium` are deprecated aliases of `sm` and `md`.

variant
"filled""light""lighter""stroke"
"filled"

Visual style. Deprecated aliases: `default` → filled, `secondary` → light, `outline` → stroke, `destructive` → filled + red.

color
"neutral""gray""blue""orange""red""green""yellow""purple""sky""pink""teal"
"neutral"

Colour of the badge

disabled"boolean""false"

Muted, outlined appearance. Sets aria-disabled and data-disabled.

square"boolean""false"

Minimal horizontal padding, for counters and single characters

rounded"boolean""false"

Pill shape (rounded-full) instead of rounded-md

asChild"boolean""false"

Render the single child element instead of a span, merging the badge props onto it

className"string""undefined"

Merged last, so it overrides the badge classes (including rounding)

Badge.Icon

Adds an icon to the badge with proper sizing and alignment. The icon is decorative (aria-hidden).

PropTypeDefaultDescription
as
"ElementType""ReactElement"
"span"

Icon component, e.g. as={RiInformationFill}. A rendered element (as={<RiInformationFill />}) is also accepted.

size
"sm""md""lg"
"sm"

Size of the icon. It does not follow Badge.Root's size.

className"string""undefined"

Additional CSS classes, merged last

Badge.Dot

Displays a small dot (in the current text colour) within the badge. The dot is decorative (aria-hidden), so keep a text label next to it.

PropTypeDefaultDescription
size
"sm""md""lg"
"sm"

Size of the box around the dot

className"string""undefined"

Additional CSS classes, merged last

Used in these blocks

Block families with at least one block built on this component. Each page previews its blocks and lists the components and packages they use.