Components · Display
Badge
A short status or count next to text.
1.4% of the 2,062 sites in The UI Index have a tag (28 sites).
NewDraftv0.1Failed
- northwind.devLive
- Preview #142Building
- Preview #141Failed
NewDraftv0.1Failed
- northwind.devLive
- Preview #142Building
- Preview #141Failed
Both previews render the same markup; the HTML tab below is that markup without a framework.
Install
- React
- npx shadcn@
latest add https:// www.themes.dev/ caseroom/ r/ badge.json - Astro
- npx shadcn@
latest add https:// www.themes.dev/ caseroom/ r/ astro/ badge.json
Or copy the files below into your project. They need clsx, tailwind-merge, and the Caseroom theme. The HTML needs only the compiled CSS.
import type { HTMLAttributes } from 'react';
import { cn } from '@/lib/utils';
import { badgeVariants, type BadgeVariants } from './badge.variants';
export type BadgeProps = HTMLAttributes<HTMLSpanElement> & BadgeVariants;
/** A short status or count. Not interactive: for a removable tag or a filter, use a button. */
export function Badge({ variant, className, ...props }: BadgeProps) {
return <span className={cn(badgeVariants({ variant }), className)} {...props} />;
}
export { badgeVariants };import { cva, type VariantProps } from 'class-variance-authority';
export const badgeVariants = cva(
'inline-flex w-fit shrink-0 items-center gap-1 rounded-sm border px-1.5 py-0.5 text-xs font-medium whitespace-nowrap [&_svg]:size-3',
{
variants: {
variant: {
// Foreground, not primary: a badge is a status, so an accent preset (which marks actions) leaves it alone.
default: 'border-transparent bg-foreground text-background',
secondary: 'border-transparent bg-secondary text-secondary-foreground',
outline: 'border-border text-foreground',
destructive: 'border-transparent bg-destructive text-destructive-foreground',
},
},
defaultVariants: { variant: 'default' },
},
);
export type BadgeVariants = VariantProps<typeof badgeVariants>;---
import type { HTMLAttributes } from 'astro/types';
import { cn } from '@/lib/utils';
import { badgeVariants, type BadgeVariants } from './badge.variants';
type Props = HTMLAttributes<'span'> & BadgeVariants;
const { variant, class: className, ...rest } = Astro.props;
---
<span class={cn(badgeVariants({ variant }), className)} {...rest}><slot /></span>import { cva, type VariantProps } from 'class-variance-authority';
export const badgeVariants = cva(
'inline-flex w-fit shrink-0 items-center gap-1 rounded-sm border px-1.5 py-0.5 text-xs font-medium whitespace-nowrap [&_svg]:size-3',
{
variants: {
variant: {
// Foreground, not primary: a badge is a status, so an accent preset (which marks actions) leaves it alone.
default: 'border-transparent bg-foreground text-background',
secondary: 'border-transparent bg-secondary text-secondary-foreground',
outline: 'border-border text-foreground',
destructive: 'border-transparent bg-destructive text-destructive-foreground',
},
},
defaultVariants: { variant: 'default' },
},
);
export type BadgeVariants = VariantProps<typeof badgeVariants>;<div class="grid w-full max-w-xs gap-4">
<div class="flex flex-wrap items-center gap-2">
<span class="inline-flex w-fit shrink-0 items-center gap-1 rounded-sm border px-1.5 py-0.5 text-xs font-medium whitespace-nowrap [&_svg]:size-3 border-transparent bg-foreground text-background">
New
</span>
<span class="inline-flex w-fit shrink-0 items-center gap-1 rounded-sm border px-1.5 py-0.5 text-xs font-medium whitespace-nowrap [&_svg]:size-3 border-transparent bg-secondary text-secondary-foreground">
Draft
</span>
<span class="inline-flex w-fit shrink-0 items-center gap-1 rounded-sm border px-1.5 py-0.5 text-xs font-medium whitespace-nowrap [&_svg]:size-3 border-border text-foreground">
v0.1
</span>
<span class="inline-flex w-fit shrink-0 items-center gap-1 rounded-sm border px-1.5 py-0.5 text-xs font-medium whitespace-nowrap [&_svg]:size-3 border-transparent bg-destructive text-destructive-foreground">
Failed
</span>
</div>
<ul class="divide-y rounded-lg border text-sm">
<li class="flex items-center justify-between gap-3 px-3 py-2.5">
<span class="truncate">northwind.dev</span>
<span class="inline-flex w-fit shrink-0 items-center gap-1 rounded-sm border px-1.5 py-0.5 text-xs font-medium whitespace-nowrap [&_svg]:size-3 border-border text-foreground">
Live
</span>
</li>
<li class="flex items-center justify-between gap-3 px-3 py-2.5">
<span class="truncate">Preview #142</span>
<span class="inline-flex w-fit shrink-0 items-center gap-1 rounded-sm border px-1.5 py-0.5 text-xs font-medium whitespace-nowrap [&_svg]:size-3 border-transparent bg-secondary text-secondary-foreground">
Building
</span>
</li>
<li class="flex items-center justify-between gap-3 px-3 py-2.5">
<span class="truncate">Preview #141</span>
<span class="inline-flex w-fit shrink-0 items-center gap-1 rounded-sm border px-1.5 py-0.5 text-xs font-medium whitespace-nowrap [&_svg]:size-3 border-transparent bg-destructive text-destructive-foreground">
Failed
</span>
</li>
</ul>
</div>