Skip to content
Ndop UI

Badge

Displays a badge or a component that looks like a badge.

  • v1.0.0
  • SSR
  • Zoneless
  • 0.8 kB gzip
BadgeSecondaryDestructiveOutline
89920+
import { Component } from '@angular/core';
import { Badge } from '@/ui/badge/badge';

/** The four main Badge variants, then badges used as counts. */
@Component({
  selector: 'example-badge-demo',
  imports: [Badge],
  template: `
    <div class="flex flex-col items-center gap-2">
      <div class="flex w-full flex-wrap gap-2">
        <span ndBadge>Badge</span>
        <span ndBadge variant="secondary">Secondary</span>
        <span ndBadge variant="destructive">Destructive</span>
        <span ndBadge variant="outline">Outline</span>
      </div>
      <div class="flex w-full flex-wrap gap-2">
        <span
          ndBadge
          class="h-5 min-w-5 rounded-full px-1 font-mono tabular-nums"
          >8</span
        >
        <span
          ndBadge
          variant="destructive"
          class="h-5 min-w-5 rounded-full px-1 font-mono tabular-nums"
          >99</span
        >
        <span
          ndBadge
          variant="outline"
          class="h-5 min-w-5 rounded-full px-1 font-mono tabular-nums"
          >20+</span
        >
      </div>
    </div>
  `,
})
export class BadgeDemoExample {}

A badge marks something with a short status, count or label, such as "Beta", "3 new" or a version. ndBadge only styles its host, so a badge is as accessible as the element it's on: plain text in a span, a link in an a, an item in a list. Six variants set the emphasis, from default to link.

Installation

npx @ndop/cli add badge

Usage

import { Badge } from '@/ui/badge/badge';
<div class="flex flex-col items-center gap-2">
  <div class="flex w-full flex-wrap gap-2">
    <span ndBadge>Badge</span>
    <span ndBadge variant="secondary">Secondary</span>
    <span ndBadge variant="destructive">Destructive</span>
    <span ndBadge variant="outline">Outline</span>
  </div>
  <div class="flex w-full flex-wrap gap-2">
    <span
      ndBadge
      class="h-5 min-w-5 rounded-full px-1 font-mono tabular-nums"
      >8</span
    >
    <span
      ndBadge
      variant="destructive"
      class="h-5 min-w-5 rounded-full px-1 font-mono tabular-nums"
      >99</span
    >
    <span
      ndBadge
      variant="outline"
      class="h-5 min-w-5 rounded-full px-1 font-mono tabular-nums"
      >20+</span
    >
  </div>
</div>

Examples

A badge that links somewhere, with a hover style.

import { Component } from '@angular/core';
import { Badge } from '@/ui/badge/badge';

/** A badge that links somewhere, with a hover style. */
@Component({
  selector: 'example-badge-link',
  imports: [Badge],
  template: `<a ndBadge variant="outline" href="#changelog">Changelog</a>`,
})
export class BadgeLinkExample {}

Badge variants

Every Badge variant side by side.

DefaultSecondaryDestructiveOutlineGhostLink
import { Component } from '@angular/core';
import { Badge } from '@/ui/badge/badge';

/** Every Badge variant side by side. */
@Component({
  selector: 'example-badge-variants',
  imports: [Badge],
  template: `
    <div class="flex flex-wrap gap-2">
      <span ndBadge>Default</span>
      <span ndBadge variant="secondary">Secondary</span>
      <span ndBadge variant="destructive">Destructive</span>
      <span ndBadge variant="outline">Outline</span>
      <span ndBadge variant="ghost">Ghost</span>
      <a ndBadge variant="link" href="#changelog">Link</a>
    </div>
  `,
})
export class BadgeVariantsExample {}

Accessibility

Badge has no keyboard interaction or focus handling of its own; it keeps the semantics of the element it's on.

API reference

Badge

[ndBadge]

Styled badge: a short status, count or label. It only styles its host, so the host keeps its own semantics: a span for text, an a when it links somewhere, an li in a list of badges.

Badge binding Type Default
[variant]
Visual style.
'default' | 'destructive' | 'outline' | 'secondary' | 'ghost' | 'link''default'
[class]
Extra classes, merged last so they win.
string''
Data attribute Values
data-slotbadge
data-variant —

Agent notes

Do

  • Keep the text to a word or two, or a number.
  • Put ndBadge on an a with an href when the badge leads somewhere; it then gets a hover style.
  • When a badge's meaning depends on its color, also say it in the text, such as "Failed" rather than a red dot.
  • Give a group of badges a list (ul with li[ndBadge]) and a label when they belong together.

Don't

  • Don't put a click handler on a span[ndBadge]; use a button for actions so keyboard and screen reader users can operate it.
  • Don't use a badge for long text or sentences; use an alert or plain text.