Badge
Displays a badge or a component that looks like a badge.
BadgeSecondaryDestructiveOutline
import { Badge } from "~/components/ui/badge"
export default function BadgeDemo() {
return (
<div class="flex w-full flex-wrap justify-center gap-2">
<Badge>Badge</Badge>
<Badge variant="secondary">Secondary</Badge>
<Badge variant="destructive">Destructive</Badge>
<Badge variant="outline">Outline</Badge>
</div>
)
}Installation
Install the following dependencies:
Copy and paste the following code into your project.
import {
dynamic,
type JSX,
type ComponentProps,
type ValidComponent
} from "@solidjs/web"
import { cva, type VariantProps } from "class-variance-authority"
import { isStatic, omit } from "solid-js"
import { cn } from "~/lib/utils"
type PolymorphicProps<T extends ValidComponent, P> = Omit<
P,
"as" | "class" | "children"
> &
Omit<
ComponentProps<T>,
keyof P | "as" | "class" | "children" | "onChange"
> & {
as?: T
class?: string | undefined
children?: JSX.Element
}
const badgeVariants = cva(
"group/badge h-5 gap-1 rounded-4xl border border-transparent px-2 py-0.5 text-xs font-medium transition-all has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 [&>svg]:size-3! inline-flex w-fit shrink-0 items-center justify-center overflow-hidden whitespace-nowrap transition-colors focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 [&_svg]:pointer-events-none",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground [a]:hover:bg-primary/80",
secondary: "bg-secondary text-secondary-foreground [a]:hover:bg-secondary/80",
destructive: "bg-destructive/10 text-destructive focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:focus-visible:ring-destructive/40 [a]:hover:bg-destructive/20",
outline: "border-border text-foreground [a]:hover:bg-muted [a]:hover:text-muted-foreground",
ghost: "hover:bg-muted hover:text-muted-foreground dark:hover:bg-muted/50",
link: "text-primary underline-offset-4 hover:underline"
}
},
defaultVariants: {
variant: "default"
}
}
)
type BadgeProps<T extends ValidComponent = "span"> = PolymorphicProps<
T,
ComponentProps<"span">
> &
VariantProps<typeof badgeVariants> &
Pick<ComponentProps<T>, "class">
const Badge = <T extends ValidComponent = "span">(props: BadgeProps<T>) => {
const others = omit(props as BadgeProps, "class", "as", "variant")
const Comp = dynamic(() => props.as ?? "span", {
static: isStatic(props, "as")
})
return (
<Comp
class={cn(badgeVariants({ variant: props.variant }), props.class)}
data-slot="badge"
data-variant={props.variant}
{...others}
/>
)
}
export { Badge, badgeVariants, type BadgeProps }Update the import paths to match your project setup.
Usage
import { Badge } from "~/components/ui/badge"<Badge variant="secondary">Badge</Badge>Variants
Use the variant prop to change the variant of the badge.
DefaultSecondaryDestructiveOutlineGhostLink
import { Badge } from "~/components/ui/badge"
export default function BadgeVariants() {
return (
<div class="flex flex-wrap items-center gap-2">
<Badge>Default</Badge>
<Badge variant="secondary">Secondary</Badge>
<Badge variant="destructive">Destructive</Badge>
<Badge variant="outline">Outline</Badge>
<Badge variant="ghost">Ghost</Badge>
<Badge variant="link">Link</Badge>
</div>
)
}With Icon
You can render an icon inside the badge. Use data-icon="inline-start" to render the icon on the left and data-icon="inline-end" to render the icon on the right.
VerifiedNewExternal
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { Badge } from "~/components/ui/badge"
export default function BadgeIcon() {
return (
<div class="flex flex-wrap items-center gap-2">
<Badge>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-3"
data-icon="inline-start"
>
<circle cx="12" cy="12" r="10" />
<path d="m16 9-5.5 5.5L8 12" />
</svg>
Verified
</Badge>
<Badge variant="secondary">
New
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-3"
data-icon="inline-end"
>
<path d="M5 12h14" />
<path d="m12 5 7 7-7 7" />
</svg>
</Badge>
<Badge variant="outline">
External
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-3"
data-icon="inline-end"
>
<path d="M7 7h10v10" />
<path d="M7 17 17 7" />
</svg>
</Badge>
</div>
)
}With Spinner
You can render a spinner inside the badge. Remember to add the data-icon="inline-start" or data-icon="inline-end" prop to the spinner.
DeletingGenerating
import { Badge } from "~/components/ui/badge"
import { Spinner } from "~/components/ui/spinner"
export default function BadgeWithSpinner() {
return (
<div class="flex flex-wrap gap-2">
<Badge variant="destructive">
<Spinner data-icon="inline-start" />
Deleting
</Badge>
<Badge variant="secondary">
Generating
<Spinner data-icon="inline-end" />
</Badge>
</div>
)
}Link
Use the as prop to render a link as a badge.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { Badge } from "~/components/ui/badge"
export default function BadgeAsLink() {
return (
<Badge as="a" href="#link">
Open Link{" "}
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
data-icon="inline-end"
>
<path d="M7 7h10v10" />
<path d="M7 17 17 7" />
</svg>
</Badge>
)
}Custom Colors
You can customize the colors of a badge by adding custom classes such as bg-green-50 dark:bg-green-800 to the Badge component.
BlueGreenSkyPurpleRed
import { Badge } from "~/components/ui/badge"
export default function BadgeColors() {
return (
<div class="flex flex-wrap gap-2">
<Badge class="bg-blue-50 text-blue-700 dark:bg-blue-950 dark:text-blue-300">
Blue
</Badge>
<Badge class="bg-green-50 text-green-700 dark:bg-green-950 dark:text-green-300">
Green
</Badge>
<Badge class="bg-sky-50 text-sky-700 dark:bg-sky-950 dark:text-sky-300">
Sky
</Badge>
<Badge class="bg-purple-50 text-purple-700 dark:bg-purple-950 dark:text-purple-300">
Purple
</Badge>
<Badge class="bg-red-50 text-red-700 dark:bg-red-950 dark:text-red-300">
Red
</Badge>
</div>
)
}API Reference
Badge
Badge forwards native span attributes and supports as for links.