1 "use client"
2
3 import { cva, type VariantProps } from "cva"
4 import { Avatar as RadixAvatar } from "radix-ui"
5 import * as React from "react"
6
7 import { clx } from "@/utils/clx"
8
9 const avatarVariants = cva({
10 base: "flex shrink-0 items-center justify-center overflow-hidden shadow-borders-base bg-ui-bg-base",
11 variants: {
12 variant: {
13 squared: "",
14 rounded: "rounded-full",
15 },
16 size: {
17 "2xsmall": "h-5 w-5",
18 xsmall: "h-6 w-6",
19 small: "h-7 w-7",
20 base: "h-8 w-8",
21 large: "h-10 w-10",
22 xlarge: "h-12 w-12",
23 },
24 },
25 compoundVariants: [
26 {
27 variant: "squared",
28 size: "2xsmall",
29 className: "rounded",
30 },
31 {
32 variant: "squared",
33 size: "xsmall",
34 className: "rounded-md",
35 },
36 {
37 variant: "squared",
38 size: "small",
39 className: "rounded-md",
40 },
41 {
42 variant: "squared",
43 size: "base",
44 className: "rounded-md",
45 },
46 {
47 variant: "squared",
48 size: "large",
49 className: "rounded-lg",
50 },
51 {
52 variant: "squared",
53 size: "xlarge",
54 className: "rounded-xl",
55 },
56 ],
57 defaultVariants: {
58 variant: "rounded",
59 size: "base",
60 },
61 })
62
63 const innerVariants = cva({
64 base: "aspect-square object-cover object-center",
65 variants: {
66 variant: {
67 squared: "",
68 rounded: "rounded-full",
69 },
70 size: {
71 "2xsmall": "txt-compact-xsmall-plus size-4",
72 xsmall: "txt-compact-xsmall-plus size-5",
73 small: "txt-compact-small-plus size-6",
74 base: "txt-compact-small-plus size-7",
75 large: "txt-compact-medium-plus size-9",
76 xlarge: "txt-compact-large-plus size-11",
77 },
78 },
79 compoundVariants: [
80 {
81 variant: "squared",
82 size: "2xsmall",
83 className: "rounded-sm",
84 },
85 {
86 variant: "squared",
87 size: "xsmall",
88 className: "rounded",
89 },
90 {
91 variant: "squared",
92 size: "small",
93 className: "rounded",
94 },
95 {
96 variant: "squared",
97 size: "base",
98 className: "rounded",
99 },
100 {
101 variant: "squared",
102 size: "large",
103 className: "rounded-md",
104 },
105 {
106 variant: "squared",
107 size: "xlarge",
108 className: "rounded-[10px]",
109 },
110 ],
111 defaultVariants: {
112 variant: "rounded",
113 size: "base",
114 },
115 })
116
117 interface AvatarProps
118 extends Omit<
119 React.ComponentPropsWithoutRef<typeof RadixAvatar.Root>,
120 "asChild" | "children" | "size"
121 >,
122 VariantProps<typeof avatarVariants> {
123 src?: string
124 fallback: string
125 }
126
127 /**
128 * This component is based on the [Radix UI Avatar](https://www.radix-ui.com/primitives/docs/components/avatar) primitive.
129 */
130 const Avatar = React.forwardRef<
131 React.ElementRef<typeof RadixAvatar.Root>,
132 AvatarProps
133 >(
134 (
135 {
136 /**
137 * The URL of the image used in the Avatar.
138 */
139 src,
140 /**
141 * Text to show in the avatar if the URL provided in `src` can't be opened.
142 */
143 fallback,
144 /**
145 * The style of the avatar.
146 */
147 variant = "rounded",
148 /**
149 * The size of the avatar's border radius.
150 */
151 size = "base",
152 className,
153 ...props
154 }: AvatarProps,
155 ref
156 ) => {
157 return (
158 <RadixAvatar.Root
159 ref={ref}
160 {...props}
161 className={clx(
162 avatarVariants({ variant, size }),
163 className
164 )}
165 >
166 {src && (
167 <RadixAvatar.Image
168 src={src}
169 className={innerVariants({ variant, size })}
170 />
171 )}
172 <RadixAvatar.Fallback
173 className={clx(
174 innerVariants({ variant, size }),
175 "bg-ui-bg-component-hover text-ui-fg-subtle pointer-events-none flex select-none items-center justify-center"
176 )}
177 >
178 {fallback}
179 </RadixAvatar.Fallback>
180 </RadixAvatar.Root>
181 )
182 }
183 )
184 Avatar.displayName = "Avatar"
185
186 export { Avatar }