Skip to content

Commit 799f61e

Browse files
committed
feat: update badge to support icons and warning variant
1 parent 0472a88 commit 799f61e

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

site/src/components/Badge/Badge.tsx

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
* @see {@link https://ui.shadcn.com/docs/components/badge}
44
*/
55
import { type VariantProps, cva } from "class-variance-authority";
6-
import type { FC } from "react";
6+
import { forwardRef } from "react";
7+
import { Slot } from "@radix-ui/react-slot";
78
import { cn } from "utils/cn";
89

910
export const badgeVariants = cva(
10-
"inline-flex items-center rounded-md border px-2 py-1 transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
11+
`inline-flex items-center rounded-md border px-2 py-1 transition-colors
12+
focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2
13+
[&_svg]:pointer-events-none [&_svg]:pr-0.5 [&_svg]:py-0.5 [&_svg]:mr-0.5`,
1114
{
1215
variants: {
1316
variant: {
1417
default:
1518
"border-transparent bg-surface-secondary text-content-secondary shadow",
19+
warning:
20+
"border-transparent bg-surface-orange text-content-warning shadow",
1621
},
1722
size: {
18-
sm: "text-2xs font-regular",
19-
md: "text-xs font-medium",
23+
sm: "text-2xs font-regular h-5.5 [&_svg]:size-icon-xs",
24+
md: "text-xs font-medium [&_svg]:size-icon-sm",
2025
},
2126
},
2227
defaultVariants: {
@@ -28,18 +33,20 @@ export const badgeVariants = cva(
2833

2934
export interface BadgeProps
3035
extends React.HTMLAttributes<HTMLDivElement>,
31-
VariantProps<typeof badgeVariants> {}
36+
VariantProps<typeof badgeVariants> {
37+
asChild?: boolean;
38+
}
3239

33-
export const Badge: FC<BadgeProps> = ({
34-
className,
35-
variant,
36-
size,
37-
...props
38-
}) => {
39-
return (
40-
<div
41-
className={cn(badgeVariants({ variant, size }), className)}
42-
{...props}
43-
/>
44-
);
45-
};
40+
export const Badge = forwardRef<HTMLDivElement, BadgeProps>(
41+
({ className, variant, size, asChild = false, ...props }, ref) => {
42+
const Comp = asChild ? Slot : "div"
43+
44+
return (
45+
<Comp
46+
{...props}
47+
ref={ref}
48+
className={cn(badgeVariants({ variant, size }), className)}
49+
/>
50+
);
51+
},
52+
);

0 commit comments

Comments
 (0)