Communication · Material 3: Badges
Badge
A static label for status and counts. Counts roll; gaining a digit stretches the badge. A dot variant marks something new.
Examples and variants
Variants and tones
subtle (default) and solid.
function BadgeVariants() {
return (
<div className="flex flex-wrap items-center justify-center gap-2">
<Badge>Draft</Badge>
<Badge tone="info">Beta</Badge>
<Badge tone="success">Live</Badge>
<Badge tone="warning">Review</Badge>
<Badge tone="danger">Failed</Badge>
<Badge variant="solid">New</Badge>
<Badge variant="solid" tone="danger">
Urgent
</Badge>
</div>
)
}Count and dot
Press +1: the count rolls; from 9 to 10 it stretches.
function BadgeCount() {
const [count, setCount] = useState(9)
return (
<div className="flex items-center gap-4">
<span className="relative inline-flex">
<Button size="icon" variant="secondary" aria-label={`Notifications, ${count} unread`}>
<BellIcon />
</Button>
<Badge variant="solid" tone="danger" count={count} className="absolute -top-1 -right-2" />
</span>
<span className="relative inline-flex">
<Button size="icon" variant="secondary" aria-label="Messages, new">
<BellIcon />
</Button>
<Badge dot aria-label="New" className="absolute top-1 right-1" />
</span>
<Button size="sm" variant="secondary" onClick={() => setCount((value) => value + 1)}>
+1
</Button>
</div>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/badge.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install class-variance-authority cnCopy this file to the same paths in your project. Imports use the
@/alias for your project root.components/ui/badge.tsxbadge
components/ui/badge.tsx"use client" import * as React from "react" import { cva, type VariantProps } from "class-variance-authority" import { cn } from "cn" /* Badge: a static label for status, counts or categories. Not interactive (removable or pressable tokens are Chip). With `count`, the number rolls up or down when it changes, and the badge stretches when it gains a digit. */ const badgeVariants = cva( "inline-flex h-(--badge-height) shrink-0 items-center justify-center gap-1 overflow-hidden rounded-(--badge-corner) px-(--badge-padding-x) text-xs font-medium whitespace-nowrap tabular-nums [&_svg]:size-3", { variants: { variant: { subtle: "", solid: "" }, tone: { neutral: "", info: "", success: "", warning: "", danger: "" }, }, compoundVariants: [ { variant: "subtle", tone: "neutral", className: "bg-muted text-foreground" }, { variant: "subtle", tone: "info", className: "bg-(--color-info-subtle) text-(--color-info-fg)" }, { variant: "subtle", tone: "success", className: "bg-(--color-success-subtle) text-(--color-success-fg)" }, { variant: "subtle", tone: "warning", className: "bg-(--color-warning-subtle) text-(--color-warning-fg)" }, { variant: "subtle", tone: "danger", className: "bg-(--color-danger-subtle) text-(--color-danger-fg)" }, { variant: "solid", tone: "neutral", className: "bg-primary text-primary-foreground" }, { variant: "solid", tone: "danger", className: "bg-(--color-danger) text-(--color-fg-on-accent)" }, { variant: "solid", tone: "success", className: "bg-(--color-success) text-(--color-fg-on-accent)" }, { variant: "solid", tone: "info", className: "bg-(--color-info) text-(--color-fg-on-accent)" }, { variant: "solid", tone: "warning", className: "bg-(--color-warning) text-foreground" }, ], defaultVariants: { variant: "subtle", tone: "neutral" }, } ) type BadgeProps = React.ComponentProps<"span"> & VariantProps<typeof badgeVariants> & { /** A number that rolls when it changes. Shown after children. */ count?: number /** Above this, shows "max+". */ max?: number /** A small dot with no label (for navigation items and icons). Needs an aria-label. */ dot?: boolean } function Badge({ className, variant, tone, count, max = 99, dot = false, children, ...props }: BadgeProps) { const previous = React.useRef(count) const [roll, setRoll] = React.useState<{ dir: "up" | "down"; grew: boolean; id: number } | null>(null) React.useEffect(() => { const before = previous.current previous.current = count if (before === undefined || count === undefined || before === count) return setRoll({ dir: count > before ? "up" : "down", grew: String(Math.min(count, max + 1)).length > String(Math.min(before, max + 1)).length, id: Date.now(), }) }, [count, max]) const shown = count === undefined ? null : count > max ? `${max}+` : String(count) if (dot) { return ( <span data-slot="badge" data-dot="" role="status" className={cn(badgeVariants({ variant: variant ?? "solid", tone: tone ?? "danger" }), "size-(--badge-dot-size) p-0", className)} {...props} /> ) } return ( <span data-slot="badge" key={roll?.grew ? roll.id : undefined} className={cn(badgeVariants({ variant, tone }), roll?.grew && "badge-stretch", className)} {...props} > {children} {shown !== null ? ( <span key={roll?.id ?? "n"} className={roll ? `badge-roll-${roll.dir}` : undefined}> {shown} </span> ) : null} </span> ) } export { Badge, badgeVariants }
Imports
What the examples on this page import:
import { BellIcon } from "lucide-react"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"Anatomy
- 1Container · --badge-height, --badge-padding-x, --badge-corner; dot: --badge-dot-size.
- 2Label or count · --badge-label-size, tabular numbers.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "subtle" | "solid" | "subtle" | Emphasis. |
tone | "neutral" | "info" | "success" | "warning" | "danger" | "neutral" | Meaning. |
count | number | — | Rolls on change. |
max | number | 99 | Shows max+ above it. |
dot | boolean | — | A dot with no label. Needs aria-label. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--badge-height | var(--space-5) | 20px | same |
--badge-padding-x | var(--space-2) | 8px | same |
--badge-corner | var(--shape-full) | 999px | same |
--badge-label-size | var(--type-caption-size) | 11px | same |
--badge-dot-size | var(--space-2) | 8px | same |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Count change | Digits roll up or down; a new digit stretches the badge. | --spring-snappy, --spring-bouncy |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Counts on icons belong in the button's aria-label.
- Don't rely on colour alone.
Usage
Do
- Use for status and counts.
Don’t
- Don't make badges clickable (use Chip).
Code
"use client"
import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
/*
Badge: a static label for status, counts or categories. Not interactive
(removable or pressable tokens are Chip). With `count`, the number rolls up
or down when it changes, and the badge stretches when it gains a digit.
*/
const badgeVariants = cva(
"inline-flex h-(--badge-height) shrink-0 items-center justify-center gap-1 overflow-hidden rounded-(--badge-corner) px-(--badge-padding-x) text-xs font-medium whitespace-nowrap tabular-nums [&_svg]:size-3",
{
variants: {
variant: { subtle: "", solid: "" },
tone: { neutral: "", info: "", success: "", warning: "", danger: "" },
},
compoundVariants: [
{ variant: "subtle", tone: "neutral", className: "bg-muted text-foreground" },
{ variant: "subtle", tone: "info", className: "bg-(--color-info-subtle) text-(--color-info-fg)" },
{ variant: "subtle", tone: "success", className: "bg-(--color-success-subtle) text-(--color-success-fg)" },
{ variant: "subtle", tone: "warning", className: "bg-(--color-warning-subtle) text-(--color-warning-fg)" },
{ variant: "subtle", tone: "danger", className: "bg-(--color-danger-subtle) text-(--color-danger-fg)" },
{ variant: "solid", tone: "neutral", className: "bg-primary text-primary-foreground" },
{ variant: "solid", tone: "danger", className: "bg-(--color-danger) text-(--color-fg-on-accent)" },
{ variant: "solid", tone: "success", className: "bg-(--color-success) text-(--color-fg-on-accent)" },
{ variant: "solid", tone: "info", className: "bg-(--color-info) text-(--color-fg-on-accent)" },
{ variant: "solid", tone: "warning", className: "bg-(--color-warning) text-foreground" },
],
defaultVariants: { variant: "subtle", tone: "neutral" },
}
)
type BadgeProps = React.ComponentProps<"span"> &
VariantProps<typeof badgeVariants> & {
/** A number that rolls when it changes. Shown after children. */
count?: number
/** Above this, shows "max+". */
max?: number
/** A small dot with no label (for navigation items and icons). Needs an aria-label. */
dot?: boolean
}
function Badge({ className, variant, tone, count, max = 99, dot = false, children, ...props }: BadgeProps) {
const previous = React.useRef(count)
const [roll, setRoll] = React.useState<{ dir: "up" | "down"; grew: boolean; id: number } | null>(null)
React.useEffect(() => {
const before = previous.current
previous.current = count
if (before === undefined || count === undefined || before === count) return
setRoll({
dir: count > before ? "up" : "down",
grew: String(Math.min(count, max + 1)).length > String(Math.min(before, max + 1)).length,
id: Date.now(),
})
}, [count, max])
const shown = count === undefined ? null : count > max ? `${max}+` : String(count)
if (dot) {
return (
<span
data-slot="badge"
data-dot=""
role="status"
className={cn(badgeVariants({ variant: variant ?? "solid", tone: tone ?? "danger" }), "size-(--badge-dot-size) p-0", className)}
{...props}
/>
)
}
return (
<span
data-slot="badge"
key={roll?.grew ? roll.id : undefined}
className={cn(badgeVariants({ variant, tone }), roll?.grew && "badge-stretch", className)}
{...props}
>
{children}
{shown !== null ? (
<span key={roll?.id ?? "n"} className={roll ? `badge-roll-${roll.dir}` : undefined}>
{shown}
</span>
) : null}
</span>
)
}
export { Badge, badgeVariants }