Skip to content

Commit adf8523

Browse files
authored
Added badge component (#666)
## Badge Component Usage The `Badge` component can be customized for various use cases. It is for both Markdown and TypeScript files, and we can modify its style by passing props such as `backgroundColor`, `textColor`, `textSize`, and `variant`. ### Props - `variant` (optional): Determines the badge style. Available options are: - `default` (default) - `secondary` - `destructive` - `outline` - `backgroundColor` (optional): Custom background color for the badge. Accepts any valid CSS color value. - `textColor` (optional): Custom text color for the badge. Accepts any valid CSS color value. - `textSize` (optional): Controls the text size. Available options: - `text-xs` (default) - `text-sm` - `text-base` - `text-lg` - `text-xl` - `text-2xl` - Additional props such as `className` or other standard HTML attributes are also supported. ### Usage Examples #### Default Badge ```tsx <Badge> Default Badge </Badge> ``` #### Custom Colors ```tsx <Badge backgroundColor="blue" textColor="white"> Custom Colors </Badge> ``` #### Large Heading Badge ```tsx <Badge textSize="text-2xl"> Large Heading Badge </Badge> ``` #### Custom Styled Badge ```tsx <Badge backgroundColor="green" textColor="black" textSize="text-lg"> Custom Styled Badge </Badge> ``` #### Destructive Variant with Custom Text ```tsx <Badge variant="destructive" textColor="yellow"> Destructive with Custom Text </Badge> ``` #### Secondary Variant with Custom Styles ```tsx <Badge backgroundColor="purple" textColor="white" textSize="text-base"> Secondary Variant with Custom Styles </Badge> ``` #### Red Background with Custom Small Text ```tsx <Badge backgroundColor="green" textColor="white" textSize="text-sm"> Red Background with Custom Small Text </Badge> ```
1 parent e963e70 commit adf8523

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/components/MdxComponents.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { QuickLink, QuickLinks } from './mdx/QuickLinks';
1111
import { Tab, Tabs } from './mdx/Tabs';
1212
import { PreCodeBlock } from './PreCodeBlock';
1313
import ResourceEstimator from './resource-estimator/ResourceEstimator';
14+
import { Badge } from './ui/badge';
1415

1516
const MdxComponents = (version?: string) => {
1617
return {
@@ -62,7 +63,8 @@ const MdxComponents = (version?: string) => {
6263
<div {...props} className="table-auto w-full overflow-x-auto">
6364
<table>{props.children}</table>
6465
</div>
65-
)
66+
),
67+
Badge
6668
};
6769
};
6870

src/components/ui/badge.tsx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as React from "react";
2+
import { cva, type VariantProps } from "class-variance-authority";
3+
4+
import { cn } from "@/lib/utils";
5+
6+
const badgeVariants = cva(
7+
"inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
8+
{
9+
variants: {
10+
variant: {
11+
default:
12+
"border-transparent bg-primary text-primary-foreground hover:bg-primary/80",
13+
secondary:
14+
"border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80",
15+
destructive:
16+
"border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80",
17+
outline: "text-foreground",
18+
},
19+
},
20+
defaultVariants: {
21+
variant: "default",
22+
},
23+
}
24+
);
25+
26+
export interface BadgeProps
27+
extends React.HTMLAttributes<HTMLDivElement>,
28+
VariantProps<typeof badgeVariants> {
29+
backgroundColor?: string;
30+
textColor?: string;
31+
textSize?: "text-xs" | "text-sm" | "text-base" | "text-lg" | "text-xl" | "text-2xl";
32+
}
33+
34+
function Badge({
35+
className,
36+
variant,
37+
backgroundColor,
38+
textColor,
39+
textSize = "text-xs",
40+
...props
41+
}: BadgeProps) {
42+
return (
43+
<div
44+
className={cn(
45+
badgeVariants({ variant }),
46+
className,
47+
backgroundColor,
48+
textColor,
49+
textSize
50+
)}
51+
style={{
52+
backgroundColor: backgroundColor,
53+
color: textColor,
54+
}}
55+
{...props}
56+
/>
57+
);
58+
}
59+
60+
export { Badge, badgeVariants };

0 commit comments

Comments
 (0)