Badge
Displays a Badge.
Preview
Preview
Code
documentation
Installation
Update your tailwind.config.js
file to include the following configuration:
Copy and paste the following code into your project.
badge.tsx
import * as React from "react";
import { tv, type VariantProps } from "tailwind-variants";
import { cn } from "@/lib/utils";
const specialColors = [
"primary",
"secondary",
"destructive",
"accent",
] as const;
const regularColors = [
"slate",
"gray",
"zinc",
"neutral",
"stone",
"red",
"orange",
"amber",
"yellow",
"lime",
"green",
"emerald",
"teal",
"cyan",
"sky",
"blue",
"indigo",
"violet",
"purple",
"fuchsia",
"pink",
"rose",
] as const;
const colors = [...specialColors, ...regularColors] as const;
type Color = (typeof colors)[number];
const badgeVariants = tv({
base: "inline-flex items-center gap-x-1.5 rounded-md px-2 py-1 text-sm/5 font-medium sm:text-xs/5 forced-colors:outline",
variants: {
variant: {
light: "",
filled: "",
},
color: colors.reduce((acc, color) => {
acc[color] = color;
return acc;
}, {} as Record<Color, Color>),
size: {
sm: "h-5 px-1.5 [&_svg]:size-3 ",
md: "h-6 px-2 [&_svg]:size-3.5",
lg: "h-7 px-4 text-sm [&_svg]:size-4",
},
},
compoundVariants: [
...regularColors.flatMap((color) => [
{
color,
variant: "light" as const,
class: `bg-${color}-400/10 text-${color}-700 hover:bg-${color}-400/30 dark:bg-${color}-400/20 dark:text-${color}-300 `,
},
{
color,
variant: "filled" as const,
class: `bg-${color}-300 text-${color}-800 hover:bg-${color}-400 dark:bg-${color}-800 dark:text-${color}-200 dark:hover:bg-${color}-700`,
},
]),
...specialColors.flatMap((color) => [
{
color,
variant: "light" as const,
class: `bg-${color}/10 text-${color} hover:bg-${color}/20`,
},
{
color,
variant: "filled" as const,
class: `bg-${color} text-${color}-foreground hover:bg-${color}/90 `,
},
]),
],
defaultVariants: {
color: "primary",
variant: "light",
size: "md",
},
});
type BadgeProps = React.HTMLAttributes<HTMLSpanElement> &
VariantProps<typeof badgeVariants> & {
leftSection?: React.ReactNode;
rightSection?: React.ReactNode;
};
function Badge({
children,
className,
variant,
size,
leftSection,
rightSection,
color,
...props
}: BadgeProps) {
return (
<span
role="presentation"
className={cn(badgeVariants({ variant, size, color }), className)}
{...props}
>
{leftSection && <span>{leftSection}</span>}
{children}
{rightSection && <span>{rightSection}</span>}
</span>
);
}
export type { BadgeProps };
export { Badge, badgeVariants };
Update the import paths to match your project setup.
Usage
The Badge
component is a container that displays a badge with a text and color.
Props
Variants
The Badge
component 2 variants: filled
and light
.
default variant is filled
Preview
Code
documentationdocumentation
Color
The Badge
component has multiple color variants: primary
, secondary
, destructive
, and accent
.
also along with them you can use your regular tailwind colors like red
, blue
, green
etc.
default color is primary
Preview
Code
PrimaryAmberEmeraldRedBlueGreenYellowGrayIndigoPinkPurpleTealOrangeCyanSky BlueLimeRoseFuchsiaNeutralSlateStoneZincAccentDestructive
Size
The Badge
component has 3 sizes: sm
, md
, and lg
.
default size is md
Preview
Code
size smsime md(default)sime md(default)
Sections
The Badge
component has 2 sections: leftSection
and rightSection
.
Preview
Code
15Ban
Props
Name | Type | Default | Description |
---|---|---|---|
variant | string | filled | The variant of the badge. |
color | string | primary | The color of the badge. |
size | string | md | The size of the badge. |
leftSection | ReactNode | null | The left section of the badge. |
rightSection | ReactNode | null | The right section of the badge. |
rest of the props are passed to the span
element