Skip to main content

Badge

Label status or metadata beside related content.

New feature
Props
Slots

Anatomy#

Badge [component; slot=root]
├── leading [slot]
├── label [slot]
└── trailing [slot]

Usage#

Use Badge for compact status or metadata beside content. It renders a presentational span by default. For an action, render a semantic interactive element with an accessible name.

import { Badge } from 'moraine'

export function Example() {
  return <Badge>Active</Badge>
}

Presentational status#

Place Badge beside a separately labeled control or heading. It does not replace an accessible status announcement where one is required. variant (solid, soft by default, subtle, outline), tone, and size (sm, md by default, lg) change appearance only.

<Badge variant="soft">In progress</Badge>
<Badge variant="outline" tone="success" size="sm">
  v2.4.0
</Badge>

Label and icons#

Badge text always comes from children. leading and trailing take icon names and render before and after the label. A badge with no children and exactly one icon renders as a square icon badge.

<Badge variant="soft" leading="i-lucide:eye">Needs review</Badge>
<Badge leading="i-lucide:check" />

Interactive root#

For a clickable badge, set as to a semantic element and pass the attributes that element requires.

<Badge as="button" type="button" onClick={onFilter}>
  Needs review
</Badge>

Examples#

Status#

Pair badges with surrounding text to indicate status or category.

import { Badge } from 'moraine'
import { For } from 'solid-js'

const TASKS = [
  { title: 'Review access request', status: 'Needs review', variant: 'outline' },
  { title: 'Publish release notes', status: 'In progress', variant: 'soft' },
  { title: 'Archive old project', status: 'Complete', variant: 'solid' },
] as const

export function BadgeUsage() {
  return (
    <ul class="max-w-md w-full divide-border divide-y">
      <For each={TASKS}>
        {(task) => (
          <li class="py-2 flex gap-3 items-center justify-between">
            <span class="text-sm">{task.title}</span>
            <Badge variant={task.variant}>{task.status}</Badge>
          </li>
        )}
      </For>
    </ul>
  )
}

With icons#

Add leading or trailing icons to reinforce the badge label.

import { Badge } from 'moraine'

export function StatusAndMetadata() {
  return (
    <div class="p-4 b-1 b-border rounded-xl max-w-xl space-y-4">
      <div class="space-y-1.5">
        <p class="text-xs text-muted-foreground tracking-wider font-semibold uppercase">
          Deployment Pipeline
        </p>
        <div class="flex flex-wrap gap-3 items-center">
          <Badge variant="solid" leading="i-lucide:check-circle">
            Production Deployed
          </Badge>
          <Badge variant="soft" leading="i-lucide:loader-circle">
            Building Preview...
          </Badge>
          <Badge variant="outline" leading="i-lucide:shield-check">
            Security Audit Passed
          </Badge>
        </div>
      </div>

      <div class="pt-2 border-t border-border space-y-1.5">
        <p class="text-xs text-muted-foreground tracking-wider font-semibold uppercase">
          Git Metadata
        </p>
        <div class="flex flex-wrap gap-3 items-center">
          <Badge variant="outline" trailing="i-lucide:git-commit">
            commit: #9f82a1
          </Badge>
          <Badge variant="outline" leading="i-lucide:users">
            34 contributors
          </Badge>
          <Badge variant="outline" leading="i-lucide:star">
            1.2k stars
          </Badge>
        </div>
      </div>
    </div>
  )
}

Interactive#

Render Badge with as="button" for clickable filter chips. Keep type="button", and set aria-pressed so the active filter is announced as well as styled.

import { Badge } from 'moraine'
import { createMemo, createSignal, For } from 'solid-js'

const FILTERS = [
  { value: 'all', label: 'All' },
  { value: 'open', label: 'Open' },
  { value: 'review', label: 'Needs review' },
] as const

const ISSUES = [
  { title: 'Hydration mismatch on Select', state: 'open' },
  { title: 'Document Field hidden labels', state: 'review' },
  { title: 'Fix Slider marker alignment', state: 'open' },
]

export function FilterIssues() {
  const [filter, setFilter] = createSignal<(typeof FILTERS)[number]['value']>('all')
  const visible = createMemo(() =>
    ISSUES.filter((issue) => filter() === 'all' || issue.state === filter()),
  )

  return (
    <div class="max-w-md w-full space-y-3">
      <div class="flex flex-wrap gap-2">
        <For each={FILTERS}>
          {(item) => (
            <Badge
              as="button"
              type="button"
              aria-pressed={filter() === item.value}
              variant={filter() === item.value ? 'solid' : 'outline'}
              onClick={() => setFilter(item.value)}
            >
              {item.label}
            </Badge>
          )}
        </For>
      </div>
      <ul class="divide-border divide-y">
        <For each={visible()}>{(issue) => <li class="text-sm py-2">{issue.title}</li>}</For>
      </ul>
    </div>
  )
}

API Reference#

Props#

Props for the Badge component. Renders a <span> element by default.

Prop