Skip to main content
Decoderen
Browse documentation

Badge

A compact label for categories, statuses or supporting metadata, on Material 3's pill shape.

Default

Design
Show code
Example.astroAstro
---
import Badge from '@dhekode/ui/primitives/Badge.tsx';
---

<Badge>Design</Badge>

Variants

DesignDesign
Show code
Example.astroAstro
---
import Badge from '@dhekode/ui/primitives/Badge.tsx';
---

<Badge variant="neutral">Design</Badge>
<Badge variant="brand">Design</Badge>

Status tones

ApprovedDueOverdue
Show code
Example.astroAstro
---
import Badge from '@dhekode/ui/primitives/Badge.tsx';
---

<Badge variant="success">Approved</Badge>
<Badge variant="warning">Due</Badge>
<Badge variant="error">Overdue</Badge>

Sizes

EngineeringEngineering
Show code
Example.astroAstro
---
import Badge from '@dhekode/ui/primitives/Badge.tsx';
---

<Badge size="sm">Engineering</Badge>
<Badge size="md">Engineering</Badge>
API reference
variant

Type: 'neutral' | 'brand' | 'success' | 'warning' | 'error'

Default: 'neutral'

Outlined; tonal on the surface tint; or a tonal status (success, warning, error) whose text names the state.

size

Type: 'sm' | 'md'

Default: 'md'

Compact or standard label.

className

Type: string | undefined

Default: unset

Adds classes to the React component root, after its own utilities. React components take className, not class.

Slots: Default: concise visible text.

Static metadata, not a button or a live status announcer. For a choice or an action, use Chip.

Original component source
packages/ui/src/primitives/Badge.tsxAstro
import type { ReactNode } from 'react';
import { classNames } from '@dhekode/utils/class-names';

type Variant = 'neutral' | 'brand' | 'success' | 'warning' | 'error';
type Size = 'sm' | 'md';

export interface BadgeProps {
  variant?: Variant | undefined;
  size?: Size | undefined;
  className?: string | undefined;
  children: ReactNode;
}

/**
 * Component tokens: a static label on Material 3's pill shape. Neutral is outlined; brand and the
 * status tones are tonal. Neutral, brand, warning and error read the surface roles, so they stay
 * legible on light and dark sections; success has no surface role yet and keeps its light-surface
 * colour. The text always names the state, so colour only reinforces it.
 */
const variants: Record<Variant, string> = {
  neutral: 'border-surface-border bg-transparent text-surface-text',
  brand: 'border-transparent bg-surface-tint text-surface-accent',
  success: 'border-text-success/30 bg-text-success/10 text-text-success',
  warning: 'border-surface-warning/30 bg-surface-warning/10 text-surface-warning',
  error: 'border-surface-error/30 bg-surface-error/10 text-surface-error',
};

const sizes: Record<Size, string> = {
  sm: 'px-xs text-label',
  md: 'px-sm text-body-sm',
};

/** A short, non-interactive category or status label. For a choice or an action, use Chip. */
export default function Badge({
  variant = 'neutral',
  size = 'md',
  className,
  children,
}: BadgeProps) {
  return (
    <span
      className={classNames(
        'dk-badge inline-flex w-fit max-w-full items-center rounded-full border-(length:--dk-border-width-hairline) border-solid py-2xs font-sans leading-snug font-regular wrap-anywhere',
        variants[variant],
        sizes[size],
        className,
      )}
      data-variant={variant}
      data-size={size}
    >
      {children}
    </span>
  );
}

Best practices

  • Use text to name the category or state; color should support its meaning.
  • Status tones reinforce a state written in the label (Overdue, Approved); never rely on the color alone.