1 import { VariantProps, cva } from "cva"
2 import { Slot } from "radix-ui"
3 import * as React from "react"
4
5 import { clx } from "@/utils/clx"
6
7 const badgeColorVariants = cva({
8 variants: {
9 color: {
10 green:
11 "bg-ui-tag-green-bg text-ui-tag-green-text [&_svg]:text-ui-tag-green-icon border-ui-tag-green-border",
12 red: "bg-ui-tag-red-bg text-ui-tag-red-text [&_svg]:text-ui-tag-red-icon border-ui-tag-red-border",
13 blue: "bg-ui-tag-blue-bg text-ui-tag-blue-text [&_svg]:text-ui-tag-blue-icon border-ui-tag-blue-border",
14 orange:
15 "bg-ui-tag-orange-bg text-ui-tag-orange-text [&_svg]:text-ui-tag-orange-icon border-ui-tag-orange-border",
16 grey: "bg-ui-tag-neutral-bg text-ui-tag-neutral-text [&_svg]:text-ui-tag-neutral-icon border-ui-tag-neutral-border",
17 purple:
18 "bg-ui-tag-purple-bg text-ui-tag-purple-text [&_svg]:text-ui-tag-purple-icon border-ui-tag-purple-border",
19 },
20 },
21 defaultVariants: {
22 color: "grey",
23 },
24 })
25
26 const badgeSizeVariants = cva({
27 base: "inline-flex items-center gap-x-0.5 border box-border",
28 variants: {
29 size: {
30 "2xsmall": "txt-compact-xsmall-plus h-5",
31 xsmall: "txt-compact-xsmall-plus py-px h-6",
32 small: "txt-compact-xsmall-plus py-[3px] h-7",
33 base: "txt-compact-small-plus py-[5px] h-8",
34 large: "txt-compact-medium-plus py-[7px] h-10",
35 },
36 rounded: {
37 base: "rounded-md",
38 full: "rounded-full",
39 },
40 },
41 compoundVariants: [
42 {
43 size: "2xsmall",
44 rounded: "full",
45 className: "px-1.5",
46 },
47 {
48 size: "2xsmall",
49 rounded: "base",
50 className: "px-1",
51 },
52 {
53 size: "xsmall",
54 rounded: "full",
55 className: "px-2",
56 },
57 {
58 size: "xsmall",
59 rounded: "base",
60 className: "px-1.5",
61 },
62 {
63 size: "small",
64 rounded: "full",
65 className: "px-2.5",
66 },
67 {
68 size: "small",
69 rounded: "base",
70 className: "px-2",
71 },
72 {
73 size: "base",
74 rounded: "full",
75 className: "px-3",
76 },
77 {
78 size: "base",
79 rounded: "base",
80 className: "px-2.5",
81 },
82 {
83 size: "large",
84 rounded: "full",
85 className: "px-3.5",
86 },
87 {
88 size: "large",
89 rounded: "base",
90 className: "px-3",
91 },
92 ],
93 defaultVariants: {
94 size: "base",
95 rounded: "base",
96 },
97 })
98
99 interface BadgeProps
100 extends Omit<React.HTMLAttributes<HTMLSpanElement>, "color">,
101 VariantProps<typeof badgeSizeVariants>,
102 VariantProps<typeof badgeColorVariants> {
103 asChild?: boolean
104 }
105
106 /**
107 * This component is based on the `div` element and supports all of its props
108 */
109 const Badge = React.forwardRef<HTMLSpanElement, BadgeProps>(
110 (
111 {
112 className,
113 /**
114 * The badge's size.
115 */
116 size = "base",
117 /**
118 * The style of the badge's border radius.
119 */
120 rounded = "base",
121 /**
122 * The badge's color.
123 */
124 color = "grey",
125 /**
126 * Whether to remove the wrapper `span` element and use the
127 * passed child element instead.
128 */
129 asChild = false,
130 ...props
131 }: BadgeProps,
132 ref
133 ) => {
134 const Component = asChild ? Slot.Root : "span"
135
136 return (
137 <Component
138 ref={ref}
139 className={clx(
140 badgeColorVariants({ color }),
141 badgeSizeVariants({ size, rounded }),
142 className
143 )}
144 {...props}
145 />
146 )
147 }
148 )
149 Badge.displayName = "Badge"
150
151 export { Badge, badgeColorVariants }