Components

Badge

A short label for a state or a kind, in DM Sans Medium capitals with 6% tracking: White, #262626, a hairline, or a red wash. A small spectrum dot can lead it.

Install

React apps get the React files; every other stack gets the web component.

npx spyglass-ds add badge
<script type="module" src="/spyglass-ds/elements/badge.js"></script>
Variants and dots
NewVideoCarouselPaused
RunningTestingEnded

Usage

A creative's format or status, a plan's "New", a count beside a tab.

Never

  • A spectrum color as the badge's fill: a dot is as far as color goes
  • Color alone for the meaning: the word says it
  • More than one White badge in a group
  • Sentences in a badge

Props

The web component takes the same as attributes or properties (<spyglass-badge>); title is heading there.

<Badge>

PropTypeDefault
variantBadgeVariant

default (White), secondary, outline or destructive.

'default'
dotBadgeDot

A small spectrum color before the word.

Examples

The same example in React and as web components; both render identically.

react/examples/badge/Variants.tsx
import { Badge } from '@/react/components/ui/Badge'
// The four variants, and the dots a creative's status takes.

export default function Variants() {
  return (
    <div className="flex flex-col items-center gap-4">
      <div className="flex flex-wrap items-center justify-center gap-2">
        <Badge>New</Badge>
        <Badge variant="secondary">Video</Badge>
        <Badge variant="outline">Carousel</Badge>
        <Badge variant="destructive">Paused</Badge>
      </div>
      <div className="flex flex-wrap items-center justify-center gap-2">
        <Badge variant="outline" dot="green">Running</Badge>
        <Badge variant="outline" dot="yellow">Testing</Badge>
        <Badge variant="outline" dot="red">Ended</Badge>
      </div>
    </div>
  )
}
elements/examples/badge/Variants.html
<!-- The four variants, and the dots a creative's status takes. -->
<div class="flex flex-col items-center gap-4">
  <div class="flex flex-wrap items-center justify-center gap-2">
    <spyglass-badge>New</spyglass-badge>
    <spyglass-badge variant="secondary">Video</spyglass-badge>
    <spyglass-badge variant="outline">Carousel</spyglass-badge>
    <spyglass-badge variant="destructive">Paused</spyglass-badge>
  </div>
  <div class="flex flex-wrap items-center justify-center gap-2">
    <spyglass-badge variant="outline" dot="green">Running</spyglass-badge>
    <spyglass-badge variant="outline" dot="yellow">Testing</spyglass-badge>
    <spyglass-badge variant="outline" dot="red">Ended</spyglass-badge>
  </div>
</div>

Source

react/components/ui/Badge.tsx
import type { HTMLAttributes } from 'react'
import { cn } from '@/react/lib/cn'

// shadcn's badge in the site's look (globals.css .t-badge, the plan badge): a short label for a state or a kind, DM
// Sans Medium 12/16 in sentence case, 24px tall with a 4px corner. default is White with a Black label (one per group,
// for what matters most: the plan badge); secondary the site's #363636 inset fill with a #D4D4D4 label ("Coming soon");
// outline a #666666 hairline on Black; destructive a red wash with a red label. `dot` leads it with a
// small spectrum color, the only place a badge takes one; the word still carries the meaning, never the color alone.
export type BadgeVariant = 'default' | 'secondary' | 'outline' | 'destructive'
export type BadgeDot = 'magenta' | 'red' | 'orange' | 'yellow' | 'green' | 'cyan' | 'blue'

const VARIANTS: Record<BadgeVariant, string> = {
  default: 'border-transparent bg-action-primary text-action-primary-fg',
  secondary: 'border-transparent bg-surface-inset text-fg-soft',
  outline: 'border-edge-strong bg-transparent text-fg',
  destructive: 'border-transparent bg-surface-danger text-fg-danger',
}
const DOTS: Record<BadgeDot, string> = {
  magenta: 'bg-magenta',
  red: 'bg-red',
  orange: 'bg-orange',
  yellow: 'bg-yellow',
  green: 'bg-green',
  cyan: 'bg-cyan',
  blue: 'bg-blue',
}

export interface BadgeProps extends HTMLAttributes<HTMLSpanElement> {
  /** default (White), secondary, outline or destructive. */
  variant?: BadgeVariant
  /** A small spectrum color before the word. */
  dot?: BadgeDot
}

export function Badge({ variant = 'default', dot, className, children, ...rest }: BadgeProps) {
  return (
    <span data-ds="badge" className={cn('inline-flex h-6 shrink-0 items-center gap-1.5 rounded-ds-sm border px-2 type-caption font-medium whitespace-nowrap', VARIANTS[variant] ?? VARIANTS.default, className)} {...rest}>
      {dot && <span className={cn('size-1.5 shrink-0 rounded-full', DOTS[dot])} aria-hidden="true" />}
      {children}
    </span>
  )
}

export default Badge
react/lib/cn.ts
// Joins class names, skipping falsy ones: cn('a', on && 'b', ['c', undefined]). It does not merge conflicting
// utilities (no tailwind-merge), as Vue's class binding does not: the library's classes are written not to compete,
// and a className from the caller is appended after them, as Vue appends a fallthrough class.
export type ClassValue = string | false | null | undefined | 0 | ClassValue[]

export function cn(...values: ClassValue[]): string {
	const out: string[] = []
	for (const v of values) {
		if (!v) continue
		if (Array.isArray(v)) {
			const s = cn(...v)
			if (s) out.push(s)
		}
		else out.push(v)
	}
	return out.join(' ')
}
elements/components/ui/badge.ts
import { html, nothing } from 'lit'
import { DsElement, define } from '@/elements/lib/element'
import styles from 'virtual:tw'

// Badge.tsx as <spyglass-badge>: a short label for a state or a kind, DM Sans Medium 12/16 in sentence case, 24px tall
// with a 4px corner. default is White with a Black label; secondary the #363636 inset fill; outline a #666666 hairline on
// Black; destructive a red wash with a red label. `dot` leads it with a small spectrum color; the word still carries
// the meaning.
export type BadgeVariant = 'default' | 'secondary' | 'outline' | 'destructive'
export type BadgeDot = 'magenta' | 'red' | 'orange' | 'yellow' | 'green' | 'cyan' | 'blue'
const VARIANTS: Record<BadgeVariant, string> = {
	default: 'border-transparent bg-action-primary text-action-primary-fg',
	secondary: 'border-transparent bg-surface-inset text-fg-soft',
	outline: 'border-edge-strong bg-transparent text-fg',
	destructive: 'border-transparent bg-surface-danger text-fg-danger',
}
const DOTS: Record<BadgeDot, string> = {
	magenta: 'bg-magenta',
	red: 'bg-red',
	orange: 'bg-orange',
	yellow: 'bg-yellow',
	green: 'bg-green',
	cyan: 'bg-cyan',
	blue: 'bg-blue',
}

/**
 * @summary A short label for a state or a kind, in capitals.
 * @slot - The word.
 */
export class Badge extends DsElement {
	static styles = [DsElement.styles, styles]
	static properties = { variant: { reflect: true }, dot: {} }

	/** default (White), secondary, outline or destructive. */
	variant: BadgeVariant = 'default'
	/** A small spectrum color before the word. */
	dot?: BadgeDot

	render() {
		const dot = this.dot && DOTS[this.dot]
		return html`<span data-ds="badge" class=${['inline-flex h-6 shrink-0 items-center gap-1.5 rounded-ds-sm border px-2 type-caption font-medium whitespace-nowrap', VARIANTS[this.variant] ?? VARIANTS.default].join(' ')}>${dot ? html`<span class=${['size-1.5 shrink-0 rounded-full', dot].join(' ')} aria-hidden="true"></span>` : nothing}<slot></slot></span>`
	}
}

define('badge', Badge)

Reference: Derived from the guidelines: labels in DM Sans Medium capitals at 6%, Black and White first, spectrum colors as small accents only. shadcn's badge for variants..