Badge

Small labels for status, counts and tags.

Looks
DraftNewPaidOverdueBeta
With a mark
LiveDraftShippedFailedInvited
Small
DraftNewPaidOverdueBeta
acme-webmain · 2m ago
Live
docsfeat/search · just now
Building
apifix/auth · 14m ago
Failed
blogmain · 3d ago
Paused
Filters
DesignMotionTypographyDark modeComponents

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/badge.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/badge.tsx
"use client";

import * as React from "react";
import { flushSync } from "react-dom";
import { setCount, popTag, removeTag } from "@/lib/fabricui/badge-engine";
import "./badge.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
const cx = (...c: (string | false | undefined)[]) => c.filter(Boolean).join(" ");

export type BadgeProps = React.HTMLAttributes<HTMLSpanElement> & {
  /** neutral grey (default), white, green, red, or just a ring */
  variant?: "default" | "solid" | "success" | "danger" | "outline";
  /** 22px (default) or 18px */
  size?: "md" | "sm";
  /** a status dot, in the badge's own colour */
components/fabricui/badge.css
/*
 * Badge — a small label. Shared by every page that tags, counts or marks
 * status. No effects: badges are small and everywhere, so they only use
 * plain, clean motion (springs, fades).
 *
 *   <span class="badge">Draft</span>                          neutral
 *   <span class="badge badge--solid">New</span>               white
 *   <span class="badge badge--success badge--dot">Live</span> with a status dot
 *   <span class="badge badge--danger"><svg class="icon">…</svg>Failed</span>
 *   <span class="badge badge--outline">Beta</span>
 *
 * Sizes: (default) 22px, --sm 18px. Corners follow the roundness rule.
 * Count: .badge-count on an icon button (it springs in and out, its number
 * rolls). Tag: .badge with a remove button inside (.badge__remove).
 */
.badge:where(.fui-badge, .fui-badge *) {
lib/fabricui/badge-engine.js
/*
 * Badge: the count's roll and spring, and a tag's pop in and close up.
 * From the original Badge page's script (badge.html); numbers unchanged.
 */

const reduce = () => matchMedia("(prefers-reduced-motion: reduce)").matches;

/**
 * Count: set(el, n) rolls the number (old up and out, new up and in) and
 * springs the badge out at zero and back in from zero. 99+ past 99.
 * @param {HTMLElement} el the .badge-count
 * @param {number} n
 */
export function setCount(el, n) {
  const shown = n > 99 ? "99+" : String(n);
  const cur = el.querySelector("span:not([data-leave])");

Usage

Usage
"use client";

import * as React from "react";
import { Badge, BadgeCount, Tag } from "@/components/fabricui/badge";
import { Button } from "@/components/fabricui/button";

export function Deploy() {
  return <Badge variant="success" dot>Live</Badge>;
}

export function Inbox({ unread }: { unread: number }) {
  return (
    <BadgeCount count={unread}>
      <Button variant="ghost" aria-label={`Inbox, ${unread} unread`}>…</Button>
    </BadgeCount>
  );
}

export function Filters() {
  const [tags, setTags] = React.useState(["Design", "Motion"]);
  return tags.map(t => (
    <Tag key={t} onRemove={() => setTags(l => l.filter(x => x !== t))}>{t}</Tag>
  ));
}

Props

PropTypeDefault
variant

Badge: grey, white, green, red, or just a ring.

"default" | "solid" | "success" | "danger" | "outline""default"
size

Badge: 22px or 18px tall.

"md" | "sm""md"
dot

Badge: a status dot in the badge's own colour.

booleanfalse
busy

Badge: the dot breathes, for something in progress.

booleanfalse
BadgeCount

A count on what it wraps (an icon button, say). count rolls to its new number, springs away at 0 and shows 99+ past 99. It's hidden from screen readers: put the count in the button's label.

component—
Tag

A removable badge. onRemove is called once it has shrunk away and its neighbours have closed the gap: take it out of your list then. popDelay pops it in on mount; removeLabel names its close button.

component—