Badge
The shadcn/ui Badge with soft, success, warning and info variants, three sizes and a status dot — plus three animated React badge blocks built with Motion: a pulsing live status badge, a notification count that rolls like an odometer and a shimmering “New” badge.
- Drop-in shadcn API
- Server Component friendly
- Motion blocks
- prefers-reduced-motion
"use client";
import * as React from "react";
import { Bell, Minus, Plus } from "lucide-react";
import { Button } from "@/components/ui/button";
import { AnimatedCountBadge } from "@/components/dusk/badge/animated-count-badge";
export default function AnimatedCountBadgeDemo() {
const [count, setCount] = React.useState(8);
return (
<div className="flex flex-col items-center gap-6">
<div className="flex items-center gap-8">
<span className="relative inline-flex">
<Button variant="outline" size="icon" aria-label={`Notifications, ${count} unread`}>
<Bell />
</Button>
<AnimatedCountBadge
value={count}
className="absolute -top-2 -right-2"
aria-hidden
/>
</span>
<AnimatedCountBadge value={count * 137} max={9999} variant="soft" label={(n) => `${n} stars`} />
</div>
<div className="flex gap-2">
<Button variant="secondary" size="icon-sm" aria-label="Decrease" onClick={() => setCount((c) => Math.max(0, c - 1))}>
<Minus />
</Button>
<Button variant="secondary" size="sm" onClick={() => setCount((c) => c + 7)}>
+7
</Button>
<Button variant="secondary" size="icon-sm" aria-label="Increase" onClick={() => setCount((c) => c + 1)}>
<Plus />
</Button>
</div>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/badge.json1. Install the dependencies.
npm install @radix-ui/react-slot class-variance-authority2. Copy the source into
components/ui/badge.tsx.badge.tsx import * as React from "react" import { Slot, Slottable } from "@radix-ui/react-slot" import { cva, type VariantProps } from "class-variance-authority" import { cn } from "@/lib/utils" /** * DuskUI Badge — a drop-in upgrade of the shadcn/ui Badge. * * Same API and variants as shadcn (default, secondary, destructive, outline) * plus: * - Tinted status variants for dark UIs: `soft`, `success`, `warning`, `info`. * - `size`: sm, default, lg. * - `dot`: a leading status dot in the badge's text color (works with `asChild`). * - Smooth hover transitions when rendered as a link (`asChild`). * CSS only, so it works in Server Components. */ const badgeVariants = cva( [ "inline-flex w-fit shrink-0 items-center justify-center gap-1 overflow-hidden whitespace-nowrap rounded-md border font-medium", "transition-[color,background-color,border-color,box-shadow] duration-150 ease-out motion-reduce:transition-none", "focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 outline-none", "aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40", "[&>svg]:pointer-events-none [&>svg]:size-3", ], { variants: { variant: { default: "border-transparent bg-primary text-primary-foreground [a&]:hover:bg-primary/90", secondary: "border-transparent bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/90", destructive: "border-transparent bg-destructive text-white [a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60", outline: "text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground", soft: "border-primary/20 bg-primary/10 text-primary [a&]:hover:bg-primary/15", success: "border-emerald-400/20 bg-emerald-400/10 text-emerald-300 [a&]:hover:bg-emerald-400/15", warning: "border-amber-400/20 bg-amber-400/10 text-amber-300 [a&]:hover:bg-amber-400/15", info: "border-sky-400/20 bg-sky-400/10 text-sky-300 [a&]:hover:bg-sky-400/15", }, size: { sm: "h-[1.125rem] px-1.5 text-[0.6875rem] [&>svg]:size-2.5", default: "px-2 py-0.5 text-xs", lg: "h-7 gap-1.5 px-2.5 text-sm [&>svg]:size-3.5", }, }, defaultVariants: { variant: "default", size: "default", }, } ) export interface BadgeProps extends React.ComponentProps<"span">, VariantProps<typeof badgeVariants> { /** Render the child element instead of a `<span>` (Radix Slot), e.g. a link. */ asChild?: boolean /** Show a small leading dot in the current text color. */ dot?: boolean } function Badge({ className, variant, size, asChild = false, dot = false, children, ...props }: BadgeProps) { const Comp = asChild ? Slot : "span" return ( <Comp data-slot="badge" data-variant={variant ?? "default"} className={cn(badgeVariants({ variant, size }), className)} {...props} > {dot && ( <span aria-hidden data-slot="badge-dot" className="size-1.5 shrink-0 rounded-full bg-current" /> )} {/* Slottable lets the dot be merged into an `asChild` element. */} {asChild ? <Slottable>{children}</Slottable> : children} </Comp> ) } export { Badge, badgeVariants }
Replaces components/ui/badge.tsx; every shadcn variant and badgeVariants still work. CSS only — no "use client" needed.
Usage
import { Badge } from "@/components/ui/badge"
export function Status() {
return (
<Badge variant="success" dot>
Operational
</Badge>
)
}Examples
Variants
The four shadcn variants plus soft, success, warning and info — tinted fills with a hairline border that read well on dark backgrounds.
import { Badge } from "@/components/ui/badge";
export default function BadgeVariantsDemo() {
return (
<>
<Badge>Default</Badge>
<Badge variant="secondary">Secondary</Badge>
<Badge variant="outline">Outline</Badge>
<Badge variant="destructive">Destructive</Badge>
<Badge variant="soft">Soft</Badge>
<Badge variant="success">Success</Badge>
<Badge variant="warning">Warning</Badge>
<Badge variant="info">Info</Badge>
</>
);
}Dot, icons, sizes & links
dot adds a leading status dot in the text color, icons are sized automatically, size picks sm, default or lg, and asChild turns a badge into a link with a hover state.
import Link from "next/link";
import { ArrowUpRight, BadgeCheck, Clock } from "lucide-react";
import { Badge } from "@/components/ui/badge";
export default function BadgeDotIconsDemo() {
return (
<>
<Badge variant="success" dot>Operational</Badge>
<Badge variant="warning" dot>Degraded</Badge>
<Badge variant="outline" dot className="text-muted-foreground">Offline</Badge>
<Badge variant="soft">
<BadgeCheck />
Verified
</Badge>
<Badge variant="secondary" size="sm">
<Clock />
2m ago
</Badge>
<Badge asChild variant="outline" size="lg">
<Link href="/components">
Changelog
<ArrowUpRight />
</Link>
</Badge>
</>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Pulse Badge
A status badge whose dot emits an expanding ring — for “Live”, “Online” or “Deploying”. The standalone PulseDot is exported too, e.g. for avatar presence. The ring stops under reduced motion; the solid dot stays.
import { PulseBadge, PulseDot } from "@/components/dusk/badge/pulse-badge";
export default function PulseBadgeDemo() {
return (
<>
<PulseBadge>Online</PulseBadge>
<PulseBadge tone="danger">Live</PulseBadge>
<PulseBadge tone="warning">Deploying</PulseBadge>
<PulseBadge tone="muted" pulse={false}>Offline</PulseBadge>
<span className="relative inline-flex">
<span className="grid size-10 place-items-center rounded-full bg-secondary text-sm font-medium">AK</span>
<PulseDot className="absolute right-0 bottom-0 size-2.5 ring-2 ring-background rounded-full" />
<span className="sr-only">Ada Kim, online</span>
</span>
</>
);
}$ npx shadcn@latest add https://duskui.com/r/pulse-badge.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/badge/pulse-badge.tsx.pulse-badge.tsx "use client"; import * as React from "react"; import { motion, useReducedMotion } from "motion/react"; import { Badge, type BadgeProps } from "@/components/ui/badge"; import { cn } from "@/lib/utils"; const tones = { primary: "bg-primary", success: "bg-emerald-400", warning: "bg-amber-400", danger: "bg-red-500", info: "bg-sky-400", muted: "bg-muted-foreground", } as const; export type PulseTone = keyof typeof tones; export interface PulseDotProps extends React.ComponentProps<"span"> { /** Dot color. */ tone?: PulseTone; /** Emit the expanding ring. Turn off for idle states like "offline". */ pulse?: boolean; /** Seconds per pulse. Lower = more urgent. */ speed?: number; } /** * A status dot with a faint ring that expands and fades, like a live indicator. * Decorative (aria-hidden) — always pair it with text. The ring stops under * reduced motion and the solid dot stays. */ export function PulseDot({ tone = "success", pulse = true, speed = 2, className, ...props }: PulseDotProps) { const reduceMotion = useReducedMotion(); return ( <span aria-hidden data-slot="pulse-dot" className={cn("relative inline-flex size-2 shrink-0", className)} {...props} > {pulse && !reduceMotion && ( <motion.span className={cn("absolute inset-0 rounded-full", tones[tone])} initial={{ scale: 1, opacity: 0.35 }} animate={{ scale: 2, opacity: 0 }} transition={{ duration: speed, ease: "easeOut", repeat: Infinity }} /> )} <span className={cn("relative inline-flex size-full rounded-full", tones[tone])} /> </span> ); } export interface PulseBadgeProps extends Omit<BadgeProps, "dot" | "asChild"> { tone?: PulseTone; pulse?: boolean; speed?: number; } /** * A Badge with a pulsing status dot — "Live", "Online", "Deploying"… Uses the * outline variant by default so the colored dot carries the status. */ export function PulseBadge({ tone = "success", pulse = true, speed, variant = "outline", className, children, ...props }: PulseBadgeProps) { return ( <Badge variant={variant} className={cn("gap-2 overflow-visible", className)} {...props}> <PulseDot tone={tone} pulse={pulse} speed={speed} className="size-1.5" /> {children} </Badge> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| tone | "primary" | "success" | "warning" | "danger" | "info" | "muted" | "success" | Dot color. |
| pulse | boolean | true | Emit the ring. Turn off for idle states. |
| speed | number | 2 | Seconds per pulse; lower feels more urgent. |
| variant / size | Badge props | "outline" | Any Badge variant and size. |
Animated Count Badge
A count whose digits roll like an odometer when the value changes. Each digit is its own spring, new digits slide in as the number grows, the badge bumps on increases and max caps it at e.g. “99+”. Screen readers get plain text via label.
"use client";
import * as React from "react";
import { Bell, Minus, Plus } from "lucide-react";
import { Button } from "@/components/ui/button";
import { AnimatedCountBadge } from "@/components/dusk/badge/animated-count-badge";
export default function AnimatedCountBadgeDemo() {
const [count, setCount] = React.useState(8);
return (
<div className="flex flex-col items-center gap-6">
<div className="flex items-center gap-8">
<span className="relative inline-flex">
<Button variant="outline" size="icon" aria-label={`Notifications, ${count} unread`}>
<Bell />
</Button>
<AnimatedCountBadge
value={count}
className="absolute -top-2 -right-2"
aria-hidden
/>
</span>
<AnimatedCountBadge value={count * 137} max={9999} variant="soft" label={(n) => `${n} stars`} />
</div>
<div className="flex gap-2">
<Button variant="secondary" size="icon-sm" aria-label="Decrease" onClick={() => setCount((c) => Math.max(0, c - 1))}>
<Minus />
</Button>
<Button variant="secondary" size="sm" onClick={() => setCount((c) => c + 7)}>
+7
</Button>
<Button variant="secondary" size="icon-sm" aria-label="Increase" onClick={() => setCount((c) => c + 1)}>
<Plus />
</Button>
</div>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/animated-count-badge.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/badge/animated-count-badge.tsx.animated-count-badge.tsx "use client"; import * as React from "react"; import { AnimatePresence, motion, useAnimate, useReducedMotion, useSpring, useTransform, type MotionValue, } from "motion/react"; import { Badge, type BadgeProps } from "@/components/ui/badge"; import { cn } from "@/lib/utils"; const rollSpring = { stiffness: 280, damping: 28, mass: 0.8 }; /** One number (0–9) in a digit's reel, placed above or below the window by its distance from the current value. */ function ReelNumber({ reel, number }: { reel: MotionValue<number>; number: number }) { const y = useTransform(reel, (latest) => { const current = ((latest % 10) + 10) % 10; let offset = (10 + number - current) % 10; // Take the short way round so 9 → 0 rolls forward, not back through 8, 7… if (offset > 5) offset -= 10; return `${offset}em`; }); return ( <motion.span className="absolute inset-0 flex items-center justify-center" style={{ y }}> {number} </motion.span> ); } /** A single digit column. `place` is 1, 10, 100… */ function Digit({ value, place, instant }: { value: number; place: number; instant: boolean }) { const target = Math.floor(value / place); const reel = useSpring(target, rollSpring); React.useEffect(() => { if (instant) reel.jump(target); else reel.set(target); }, [reel, target, instant]); return ( <span className="relative inline-block h-[1em] w-[1ch] overflow-hidden"> {Array.from({ length: 10 }, (_, n) => ( <ReelNumber key={n} reel={reel} number={n} /> ))} </span> ); } export interface AnimatedCountBadgeProps extends Omit<BadgeProps, "children" | "asChild"> { /** The count. Negative numbers are shown as 0. */ value: number; /** Above this, show `max+` (e.g. 99+). */ max?: number; /** Give the badge a small bump whenever the count goes up. */ bump?: boolean; /** Screen-reader text for the count, e.g. `(n) => \`${n} unread messages\``. */ label?: (value: number) => string; } /** * A count badge whose digits roll like an odometer when the value changes. * Each digit is its own spring, new digits slide in with a width animation, * and the badge can bump on increases. The visual digits are hidden from * screen readers; a plain-text label is read instead. Instant under reduced motion. */ export function AnimatedCountBadge({ value, max = 99, bump = true, label, className, ...props }: AnimatedCountBadgeProps) { const reduceMotion = useReducedMotion() ?? false; const [scope, animate] = useAnimate<HTMLSpanElement>(); const safe = Math.max(0, Math.floor(value)); const shown = Math.min(safe, max); const overflow = safe > max; const places = Array.from({ length: String(shown).length }, (_, i) => 10 ** (String(shown).length - 1 - i)); const previous = React.useRef(safe); React.useEffect(() => { if (bump && !reduceMotion && safe > previous.current && scope.current) { animate(scope.current, { scale: [1, 1.08, 1] }, { duration: 0.25, ease: "easeOut" }); } previous.current = safe; }, [safe, bump, reduceMotion, animate, scope]); return ( <Badge ref={scope} className={cn("min-w-5 rounded-full px-1.5 leading-none tabular-nums", className)} {...props} > <span aria-hidden className="inline-flex items-center"> <AnimatePresence initial={false}> {places.map((place) => ( <motion.span key={place} className="inline-flex justify-end overflow-hidden" initial={reduceMotion ? false : { width: "0ch", opacity: 0 }} animate={{ width: "1ch", opacity: 1 }} exit={{ width: "0ch", opacity: 0 }} transition={reduceMotion ? { duration: 0 } : { type: "spring", duration: 0.3, bounce: 0 }} > <Digit value={shown} place={place} instant={reduceMotion} /> </motion.span> ))} </AnimatePresence> {overflow && <span>+</span>} </span> <span className="sr-only">{label ? label(safe) : safe}</span> </Badge> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| value | number | — | The count. Negative numbers show as 0. Required. |
| max | number | 99 | Above this, show max+. |
| bump | boolean | true | Bump the badge slightly when the count goes up. |
| label | (value: number) => string | — | Screen-reader text, e.g. “3 unread messages”. |
Shimmer Badge
A band of light sweeps across the badge every few seconds — eye-catching for New, Beta or Pro labels without blinking. Static under reduced motion.
import { ShimmerBadge } from "@/components/dusk/badge/shimmer-badge";
export default function ShimmerBadgeDemo() {
return (
<>
<ShimmerBadge>New</ShimmerBadge>
<ShimmerBadge variant="default" size="lg">Pro</ShimmerBadge>
<ShimmerBadge variant="outline">Beta</ShimmerBadge>
</>
);
}$ npx shadcn@latest add https://duskui.com/r/shimmer-badge.json1. Install the dependencies.
npm install motion2. Copy the source into
components/dusk/badge/shimmer-badge.tsx.shimmer-badge.tsx "use client"; import * as React from "react"; import { motion, useReducedMotion } from "motion/react"; import { Badge, type BadgeProps } from "@/components/ui/badge"; import { cn } from "@/lib/utils"; export interface ShimmerBadgeProps extends Omit<BadgeProps, "asChild"> { /** Seconds for one sweep. */ duration?: number; /** Pause between sweeps, in seconds. */ delay?: number; } /** * A badge with a faint band of light sweeping across it every few seconds — for * "New", "Beta" or "Pro" labels that should catch the eye without blinking. * Static under reduced motion. */ export function ShimmerBadge({ variant = "soft", duration = 1.4, delay = 4, className, children, ...props }: ShimmerBadgeProps) { const reduceMotion = useReducedMotion(); return ( <Badge variant={variant} className={cn("relative isolate", className)} {...props}> {children} {!reduceMotion && ( <motion.span aria-hidden className="pointer-events-none absolute inset-y-0 left-0 -z-10 w-full bg-[linear-gradient(100deg,transparent_35%,rgb(255_255_255/0.1)_50%,transparent_65%)]" initial={{ x: "-100%" }} animate={{ x: "100%" }} transition={{ duration, ease: "easeInOut", repeat: Infinity, repeatDelay: delay }} /> )} </Badge> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| duration | number | 1.4 | Seconds for one sweep. |
| delay | number | 4 | Pause between sweeps in seconds. |
| variant / size | Badge props | "soft" | Any Badge variant and size. |
API Reference
Accepts every <span> prop. Also exports badgeVariants to style other elements as badges.
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | "default" | "secondary" | "destructive" | "outline" | "soft" | "success" | "warning" | "info" | "default" | Visual style. |
| size | "sm" | "default" | "lg" | "default" | Height, padding and text size. |
| dot | boolean | false | Leading dot in the current text color. |
| asChild | boolean | false | Render the child element instead (Radix Slot), e.g. a Link. |
Accessibility
- Color is never the only signal: dots and pulses are aria-hidden decoration next to a text label.
- Animated Count Badge hides the rolling digits from assistive tech and exposes the value through
label. Put the full context (“3 unread”) on the control it decorates. - Badges rendered as links via
asChildget a visible focus-visible ring. - Pulses, shimmers and the count bump stop under prefers-reduced-motion; the count updates instantly.