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#

Pass label instead of children when the text comes from data. label takes precedence over children when both are set. leading and trailing take icon names and render before and after the label. A badge with no label and exactly one icon renders as a square icon badge.

<Badge label="Needs review" variant="soft" leading="i-lucide:eye" />
<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#

Label task status in a queue#

Keep the task title as the accessible name. Badge only restates workflow state.

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>
  )
}

Annotate a deployment and commit metadata#

Use leading or trailing icons when the label names a pipeline stage or a git fact.

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>
  )
}

Filter an issue list#

Render Badge as a button when the chip is a command. Keep type="button" and an onClick handler; the visible label is the accessible name.

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"
              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