Badge
A word circled or coloured in: a pill pulled once around it, shaded, hatched or in red pen.
import { Badge } from "@/components/ui/badge";
export default function BadgeDemo() {
return (
<div className="flex flex-wrap items-center gap-4">
<Badge seed="badge-default">New</Badge>
<Badge variant="secondary" seed="badge-secondary">
Draft
</Badge>
<Badge variant="outline" seed="badge-outline">
v1.2
</Badge>
<Badge variant="destructive" seed="badge-destructive">
Overdue
</Badge>
<Badge variant="ghost" seed="badge-ghost">
Hover me
</Badge>
<Badge variant="link" render={<a href="#badge" />} seed="badge-link">
A link
</Badge>
<Badge radius={4} roughness={1.6} seed="badge-square">
Boxed
</Badge>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ badge"use client";
import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import { cva, type VariantProps } from "class-variance-authority";
import { useId, useMemo, type ComponentProps } from "react";
import { cn } from "@/lib/utils";
import { usePen, type InkFill, type Pen } from "@/hooks/use-ink-box";
import { Stroke } from "@/lib/ink";
import { InkOutline } from "@/lib/ink-outline";
import { hatchStrokes, roundedRectPath, scribbleFill, shadeFill } from "@/lib/ink-sketch";
const badgeVariants = cva(
"group/badge relative isolate inline-flex w-fit shrink-0 items-center justify-center gap-1 whitespace-nowrap [&>svg:not(.ink-sketch)]:pointer-events-none [&>svg:not(.ink-sketch)]:size-3.5",
{
variants: {
size: {
sm: "h-5 px-2 text-xs [&>svg:not(.ink-sketch)]:size-3",
default: "h-6 px-2.5 text-sm",
lg: "h-8 px-3.5 text-base [&>svg:not(.ink-sketch)]:size-4",
},
variant: {
default: "text-primary-foreground",
secondary: "text-foreground",
destructive: "text-destructive",
outline: "text-foreground",
ghost: "ink-hover text-ink-2 [a]:hover:text-ink",
link: "ink-hover text-foreground",
},
},
defaultVariants: { variant: "default", size: "default" },
},
);
type Variant = NonNullable<VariantProps<typeof badgeVariants>["variant"]>;
/**
* A word circled or coloured in: a small pill pulled once around the text,
* shaded solid for the default badge, hatched for secondary, red for
* destructive. Renders a <span>; pass `render` to make it a link.
*/
function Badge({
className,
variant = "default",
size = "default",
render,
children,
seed,
roughness,
radius,
fill,
draw,
weight,
speed,
...props
}: useRender.ComponentProps<"span"> &
VariantProps<typeof badgeVariants> &
Pick<Pen, "roughness" | "radius" | "fill" | "draw" | "weight" | "speed"> & { seed?: string | number }) {
const pen = usePen({ roughness, radius, fill, draw, weight, speed, passes: 1, corners: "joined" });
const v = variant ?? "default";
return useRender({
defaultTagName: "span",
props: mergeProps<"span">(
// Data attributes are fine on a span; the object literal type just doesn't list them.
{
"data-slot": "badge",
"data-variant": v,
className: cn(badgeVariants({ variant: v, size }), className),
children: (
<>
<BadgeInk variant={v} pen={pen} seed={seed} />
{children}
</>
),
} as ComponentProps<"span">,
props,
),
render,
state: { slot: "badge", variant: v },
});
}
function BadgeInk({ variant, pen, seed }: { variant: Variant; pen: Pen; seed?: string | number }) {
const id = useId().replace(/[^\w-]/g, "");
if (variant === "link") return null;
const solid = variant === "default";
const filled = solid || variant === "secondary";
return (
<InkOutline
pen={variant === "ghost" ? { ...pen, draw: "none" } : pen}
seed={seed}
estimate={[64, 24]}
pad={6}
className={cn("-z-10", variant === "ghost" && "opacity-0 transition-opacity group-hover/badge:opacity-100", solid && "text-primary")}
>
{({ w, h, r, s }) =>
filled && (
<>
<defs>
<clipPath id={`${id}-clip`}>
<path d={roundedRectPath(w, h, r)} />
</clipPath>
</defs>
{solid && <path d={roundedRectPath(w, h, r)} className="ink-fill" />}
<g clipPath={`url(#${id}-clip)`}>
<BadgeFill fill={pen.fill ?? (solid ? "shade" : "hatch")} solid={solid} s={s} w={w} h={h} draw={pen.draw} />
</g>
</>
)
}
</InkOutline>
);
}
function BadgeFill({ fill, solid, s, w, h, draw }: { fill: InkFill; solid: boolean; s: number; w: number; h: number; draw: Pen["draw"] }) {
const d = useMemo(() => {
if (fill === "flat") return "";
if (fill === "hatch") return hatchStrokes(s + 30, w, h, { gap: solid ? 2.4 : 3.6, angle: -50, jitter: 0.3 }).join("");
if (fill === "scribble") return scribbleFill(s + 30, w, h, { gap: solid ? 2.2 : 3.4 });
return shadeFill(s + 30, w, h, { gap: solid ? 2 : 3.4, angle: -12, overrun: 2 });
}, [fill, solid, s, w, h]);
if (!d) return solid ? null : <path d={roundedRectPath(w, h, h / 2)} className="ink-fill" style={{ opacity: 0.14 }} />;
return <Stroke d={d} draw={draw ?? "auto"} delay={150} duration={420} width={solid ? 1.1 : 0.9} opacity={solid ? 1 : 0.45} />;
}
export { Badge, badgeVariants };Usage
import { Badge } from "@/components/ui/badge"
<Badge>New</Badge>
<Badge variant="outline">v1.2</Badge>
<Badge variant="link" render={<a href="/changelog" />}>Changelog</Badge>Examples
With icons
Glyphs and counts inside the badge.
Verified Heads up FailedUnread · 12
import { Badge } from "@/components/ui/badge";
import { InkGlyph } from "@/lib/ink-glyphs";
export default function BadgeIcons() {
return (
<div className="flex flex-wrap items-center gap-4">
<Badge seed="bi-verified">
<InkGlyph name="check" /> Verified
</Badge>
<Badge variant="secondary" seed="bi-info">
<InkGlyph name="info" /> Heads up
</Badge>
<Badge variant="destructive" seed="bi-alert">
<InkGlyph name="alert" /> Failed
</Badge>
<Badge variant="outline" seed="bi-count">
Unread · 12
</Badge>
</div>
);
}Status in a list
One variant per state, aligned to the right of each row.
- Blue ballpoints, box of 50Paid
- Cream notebooks, A5Pending
- Fineliner setShipped
- Legal pads, pack of 6Refunded
import { Badge } from "@/components/ui/badge";
const orders = [
["Blue ballpoints, box of 50", "Paid", "default"],
["Cream notebooks, A5", "Pending", "secondary"],
["Fineliner set", "Shipped", "outline"],
["Legal pads, pack of 6", "Refunded", "destructive"],
] as const;
export default function BadgeStatus() {
return (
<ul className="flex w-full max-w-md flex-col gap-4">
{orders.map(([item, status, variant]) => (
<li key={item} className="flex items-center justify-between gap-4">
<span>{item}</span>
<Badge variant={variant} seed={`bs-${status}`}>
{status}
</Badge>
</li>
))}
</ul>
);
}Sizes
Small, default and large, in three variants.
smdefaultlg
smdefaultlg
smdefaultlg
import { Badge } from "@/components/ui/badge";
const sizes = ["sm", "default", "lg"] as const;
export default function BadgeSizes() {
return (
<div className="flex flex-col gap-6">
{(["default", "secondary", "outline"] as const).map((variant) => (
<div key={variant} className="flex flex-wrap items-center gap-4">
{sizes.map((size) => (
<Badge key={size} variant={variant} size={size} seed={`bs-${variant}-${size}`}>
{size}
</Badge>
))}
</div>
))}
</div>
);
}Filter chips
Badges rendered as toggle buttons: pressed ones are coloured in.
Showing letters, lists.
"use client";
import { useState } from "react";
import { Badge } from "@/components/ui/badge";
const topics = ["Letters", "Sketches", "Lists", "Recipes", "Margins"];
export default function BadgeFilters() {
const [on, setOn] = useState<string[]>(["Letters", "Lists"]);
const toggle = (topic: string) => setOn((list) => (list.includes(topic) ? list.filter((t) => t !== topic) : [...list, topic]));
return (
<div className="flex w-full max-w-md flex-col gap-4">
<div role="group" aria-label="Topics" className="flex flex-wrap gap-2">
{topics.map((topic) => (
<Badge
key={topic}
size="lg"
variant={on.includes(topic) ? "default" : "outline"}
seed={`bf-${topic}`}
render={<button type="button" aria-pressed={on.includes(topic)} onClick={() => toggle(topic)} className="cursor-pointer" />}
>
{topic}
</Badge>
))}
</div>
<p className="text-ink-2">{on.length ? `Showing ${on.join(", ").toLowerCase()}.` : "Showing everything."}</p>
</div>
);
}Removable tags
A close button inside the badge, and a way to put them back.
- ink
- paper
- margins
- coffee
"use client";
import { useState } from "react";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { InkGlyph } from "@/lib/ink-glyphs";
const start = ["ink", "paper", "margins", "coffee"];
export default function BadgeRemovable() {
const [tags, setTags] = useState(start);
return (
<div className="flex w-full max-w-md flex-col items-start gap-4">
<ul aria-label="Tags" className="flex flex-wrap gap-2">
{tags.map((tag) => (
<li key={tag}>
<Badge variant="secondary" size="lg" seed={`br-${tag}`} className="gap-1.5 pr-1.5">
{tag}
<button type="button" aria-label={`Remove ${tag}`} onClick={() => setTags((t) => t.filter((x) => x !== tag))} className="grid size-5 cursor-pointer place-items-center text-ink-2 hover:text-ink">
<InkGlyph name="close" className="size-3" />
</button>
</Badge>
</li>
))}
{tags.length === 0 && <li className="text-ink-3">No tags left.</li>}
</ul>
{tags.length < start.length && (
<Button variant="ghost" size="sm" seed="br-reset" onClick={() => setTags(start)}>
Put them back
</Button>
)}
</div>
);
}API reference
- variantdefault
"default" "default" | "secondary" | "destructive" | "outline" | "ghost" | "link"Coloured in, hatched, red pen, circled, circled on hover, or underlined.
- render
ReactElementRender as another element, e.g. a link.
- sizedefault
"default" "sm" | "default" | "lg"20, 24 or 32px high, with the type to match.
- seed
string | numberPins the drawing.
Pen settings
The drawing also takes draw, roughness, radius, fill, weight and speed, as props or from the nearest InkProvider. What each one does.