Badge
Small labels for status, counts and tags.
Installation
npx shadcn@latest add https://fabrickit.org/r/badge.jsonpnpm dlx shadcn@latest add https://fabrickit.org/r/badge.jsonyarn shadcn@latest add https://fabrickit.org/r/badge.jsonbunx --bun shadcn@latest add https://fabrickit.org/r/badge.jsonThis 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.
Step 1: Add the theme (once)
Every colour, radius and curve the components use, as --fabric-* variables. Save it as fabric.css next to your global CSS and import it there. If you've added another Fabric Kit component before, skip this.globals.css@import "./fabric.css";fabric.css/* * fabric.css — Fabric Kit's theme: every colour, radius, ring and curve the * components use, as --fabric-* variables (prefixed, so they never clash * with your own shadcn theme). Installed by `shadcn add @fabrickit/theme` * (it lands in your global CSS) — or import this file once, globally. * Dark by default. For light, put data-theme="light" on <html> (or on any * element: the tokens follow it down the tree; data-theme="dark" turns back). */ :root, [data-theme="dark"] { --fabric-page: #111111; --fabric-surface: #191919; /* triggers, recessed tiles */ --fabric-panel: #202020; /* open panels */ --fabric-raised: #2a2a2a; /* hover, range band */ --fabric-raised-hi: #343434; /* hover inside a band */ --fabric-preview: #252525; /* soft preview fills */ --fabric-text: #e0e0e0;Step 2: Copy the Badge files
The component itself. Create each one at the same path in your project. It's your code from here on.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])");Step 3: Check the import paths
The files import each other through the @/ alias (the shadcn default, there in a new Next.js app). If your project doesn't have it, add it to tsconfig.json, or change the imports to relative paths.tsconfig.json{ "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["./*"] } } }Step 4: Use it
Import it from @/components/fabricui/badge. The Usage example below shows it in a page.
Code
"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 *//*
* 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 *) {/*
* 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
"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
| Prop | Type | Default |
|---|---|---|
| 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. | boolean | false |
| busy Badge: the dot breathes, for something in progress. | boolean | false |
| 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 | — |