1import { cva, type VariantProps } from "class-variance-authority";
2import { Slot as SlotPrimitive } from "radix-ui";
3import * as React from "react";
4import { cn } from "@/lib/utils";
5
6const avatarGroupVariants = cva("flex items-center", {
7 variants: {
8 orientation: {
9 horizontal: "flex-row",
10 vertical: "flex-col",
11 },
12 dir: {
13 ltr: "",
14 rtl: "",
15 },
16 },
17 compoundVariants: [
18 {
19 orientation: "horizontal",
20 dir: "ltr",
21 className: "-space-x-1",
22 },
23 {
24 orientation: "horizontal",
25 dir: "rtl",
26 className: "flex-row-reverse -space-x-1 space-x-reverse",
27 },
28 {
29 orientation: "vertical",
30 dir: "ltr",
31 className: "-space-y-1",
32 },
33 {
34 orientation: "vertical",
35 dir: "rtl",
36 className: "flex-col-reverse -space-y-1 space-y-reverse",
37 },
38 ],
39 defaultVariants: {
40 orientation: "horizontal",
41 dir: "ltr",
42 },
43});
44
45interface AvatarGroupProps
46 extends Omit<React.ComponentProps<"div">, "dir">,
47 VariantProps<typeof avatarGroupVariants> {
48 size?: number;
49 max?: number;
50 asChild?: boolean;
51 reverse?: boolean;
52 renderOverflow?: (count: number) => React.ReactNode;
53}
54
55function AvatarGroup(props: AvatarGroupProps) {
56 const {
57 orientation = "horizontal",
58 dir = "ltr",
59 size = 40,
60 max,
61 asChild,
62 reverse = false,
63 renderOverflow,
64 className,
65 children,
66 ...rootProps
67 } = props;
68
69 const childrenArray = React.Children.toArray(children).filter(
70 React.isValidElement,
71 );
72 const itemCount = childrenArray.length;
73 const shouldTruncate = max && itemCount > max;
74 const visibleItems = shouldTruncate
75 ? childrenArray.slice(0, max - 1)
76 : childrenArray;
77 const overflowCount = shouldTruncate ? itemCount - (max - 1) : 0;
78 const totalRenderedItems = shouldTruncate ? max : itemCount;
79
80 const RootPrimitive = asChild ? SlotPrimitive.Slot : "div";
81
82 return (
83 <RootPrimitive
84 data-orientation={orientation}
85 data-slot="avatar-group"
86 {...rootProps}
87 className={cn(avatarGroupVariants({ orientation, dir }), className)}
88 >
89 {visibleItems.map((child, index) => (
90 <AvatarGroupItem
91 key={index}
92 child={child}
93 index={index}
94 itemCount={totalRenderedItems}
95 orientation={orientation}
96 dir={dir}
97 size={size}
98 reverse={reverse}
99 />
100 ))}
101 {shouldTruncate && (
102 <AvatarGroupItem
103 key="overflow"
104 child={
105 renderOverflow ? (
106 renderOverflow(overflowCount)
107 ) : (
108 <div className="inline-flex size-full items-center justify-center rounded-full bg-muted font-medium text-muted-foreground text-xs">
109 +{overflowCount}
110 </div>
111 )
112 }
113 index={visibleItems.length}
114 itemCount={totalRenderedItems}
115 orientation={orientation}
116 dir={dir}
117 size={size}
118 reverse={reverse}
119 />
120 )}
121 </RootPrimitive>
122 );
123}
124
125interface AvatarGroupItemProps
126 extends Omit<React.ComponentProps<typeof SlotPrimitive.Slot>, "dir">,
127 VariantProps<typeof avatarGroupVariants> {
128 child: React.ReactNode;
129 index: number;
130 itemCount: number;
131 size: number;
132 reverse: boolean;
133}
134
135function AvatarGroupItem(props: AvatarGroupItemProps) {
136 const {
137 child,
138 index,
139 size,
140 orientation,
141 dir = "ltr",
142 reverse = false,
143 itemCount,
144 className,
145 style,
146 ...itemProps
147 } = props;
148
149 const maskStyle = React.useMemo<React.CSSProperties>(() => {
150 let maskImage = "";
151
152 let shouldMask = false;
153
154 if (orientation === "vertical" && dir === "rtl" && reverse) {
155 shouldMask = index !== itemCount - 1;
156 } else {
157 shouldMask = reverse ? index < itemCount - 1 : index > 0;
158 }
159
160 if (shouldMask) {
161 const maskRadius = size / 2;
162 const maskOffset = size / 4 + size / 10;
163
164 if (orientation === "vertical") {
165 if (dir === "ltr") {
166 if (reverse) {
167 maskImage = `radial-gradient(circle ${maskRadius}px at 50% ${size + maskOffset}px, transparent 99%, white 100%)`;
168 } else {
169 maskImage = `radial-gradient(circle ${maskRadius}px at 50% -${maskOffset}px, transparent 99%, white 100%)`;
170 }
171 } else {
172 if (reverse) {
173 maskImage = `radial-gradient(circle ${maskRadius}px at 50% -${maskOffset}px, transparent 99%, white 100%)`;
174 } else {
175 maskImage = `radial-gradient(circle ${maskRadius}px at 50% ${size + maskOffset}px, transparent 99%, white 100%)`;
176 }
177 }
178 } else {
179 if (dir === "ltr") {
180 if (reverse) {
181 maskImage = `radial-gradient(circle ${maskRadius}px at ${size + maskOffset}px 50%, transparent 99%, white 100%)`;
182 } else {
183 maskImage = `radial-gradient(circle ${maskRadius}px at -${maskOffset}px 50%, transparent 99%, white 100%)`;
184 }
185 } else {
186 if (reverse) {
187 maskImage = `radial-gradient(circle ${maskRadius}px at -${maskOffset}px 50%, transparent 99%, white 100%)`;
188 } else {
189 maskImage = `radial-gradient(circle ${maskRadius}px at ${size + maskOffset}px 50%, transparent 99%, white 100%)`;
190 }
191 }
192 }
193 }
194
195 return {
196 width: size,
197 height: size,
198 maskImage,
199 };
200 }, [size, index, orientation, dir, reverse, itemCount]);
201
202 return (
203 <SlotPrimitive.Slot
204 data-slot="avatar-group-item"
205 className={cn(
206 "size-full shrink-0 overflow-hidden rounded-full [&_img]:size-full",
207 className,
208 )}
209 style={{
210 ...maskStyle,
211 ...style,
212 }}
213 {...itemProps}
214 >
215 {child}
216 </SlotPrimitive.Slot>
217 );
218}
219
220export { AvatarGroup };
221