Skip to content

Commit 8e261cc

Browse files
committed
Add colored background to contact avatar
1 parent b7ea271 commit 8e261cc

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

src/components/atomic-crm/companies/CompanyCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const AvatarGroupIterator = () => {
6363

6464
const MAX_AVATARS = 3;
6565
return (
66-
<div className="*:data-[slot=avatar]:ring-background flex -space-x-0.5 *:data-[slot=avatar]:ring-2 *:data-[slot=avatar]:grayscale">
66+
<div className="*:data-[slot=avatar]:ring-background flex -space-x-0.5 *:data-[slot=avatar]:ring-2 *:data-[slot=avatar]:grayscale-50">
6767
{data.slice(0, MAX_AVATARS).map((record: any) => (
6868
<ContactAvatar
6969
key={record.id}

src/components/ui/avatar.tsx

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,67 @@ function AvatarImage({
3232
);
3333
}
3434

35+
/**
36+
* Simple hash function to generate a number from a string
37+
*/
38+
function hashString(str: string): number {
39+
let hash = 0;
40+
for (let i = 0; i < str.length; i++) {
41+
hash = (hash << 5) - hash + str.charCodeAt(i);
42+
hash = hash & hash;
43+
}
44+
return Math.abs(hash);
45+
}
46+
47+
const colorClasses = [
48+
"bg-[color:var(--avatar-0)]",
49+
"bg-[color:var(--avatar-1)]",
50+
"bg-[color:var(--avatar-2)]",
51+
"bg-[color:var(--avatar-3)]",
52+
"bg-[color:var(--avatar-4)]",
53+
"bg-[color:var(--avatar-5)]",
54+
"bg-[color:var(--avatar-6)]",
55+
"bg-[color:var(--avatar-7)]",
56+
"bg-[color:var(--avatar-8)]",
57+
"bg-[color:var(--avatar-9)]",
58+
];
59+
60+
function getAvatarColorClass(children: React.ReactNode): string {
61+
// Extract text content from children
62+
let childText: string | undefined;
63+
if (typeof children === "string") {
64+
childText = children;
65+
} else if (Array.isArray(children)) {
66+
// Handle array of children (e.g., multiple text nodes)
67+
childText = children.filter((child) => typeof child === "string").join("");
68+
} else if (
69+
React.isValidElement(children) &&
70+
typeof (children.props as any).children === "string"
71+
) {
72+
childText = (children.props as any).children;
73+
}
74+
if (!childText) return "bg-muted text-muted-foreground";
75+
76+
return colorClasses[hashString(childText) % colorClasses.length];
77+
}
78+
3579
function AvatarFallback({
3680
className,
81+
children,
3782
...props
3883
}: React.ComponentProps<typeof AvatarPrimitive.Fallback>) {
3984
return (
4085
<AvatarPrimitive.Fallback
4186
data-slot="avatar-fallback"
4287
className={cn(
43-
"bg-muted flex size-full items-center justify-center rounded-full",
88+
"flex size-full items-center justify-center rounded-full",
4489
className,
90+
getAvatarColorClass(children),
4591
)}
4692
{...props}
47-
/>
93+
>
94+
{children}
95+
</AvatarPrimitive.Fallback>
4896
);
4997
}
5098

src/index.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@
7474
--sidebar-accent-foreground: oklch(0.205 0 0);
7575
--sidebar-border: oklch(0.922 0 0);
7676
--sidebar-ring: oklch(0.708 0 0);
77+
--avatar-0: oklch(0.85 0.08 240);
78+
--avatar-1: oklch(0.88 0.1 340);
79+
--avatar-2: oklch(0.85 0.07 180);
80+
--avatar-3: oklch(0.9 0.09 40);
81+
--avatar-4: oklch(0.87 0.08 300);
82+
--avatar-5: oklch(0.85 0.08 150);
83+
--avatar-6: oklch(0.87 0.08 200);
84+
--avatar-7: oklch(0.88 0.09 10);
85+
--avatar-8: oklch(0.92 0.08 80);
86+
--avatar-9: oklch(0.86 0.09 270);
7787
}
7888

7989
.dark {
@@ -108,6 +118,16 @@
108118
--sidebar-accent-foreground: oklch(0.985 0 0);
109119
--sidebar-border: oklch(1 0 0 / 10%);
110120
--sidebar-ring: oklch(0.556 0 0);
121+
--avatar-0: oklch(0.35 0.12 240);
122+
--avatar-1: oklch(0.38 0.14 340);
123+
--avatar-2: oklch(0.35 0.11 180);
124+
--avatar-3: oklch(0.38 0.13 40);
125+
--avatar-4: oklch(0.36 0.12 300);
126+
--avatar-5: oklch(0.35 0.11 150);
127+
--avatar-6: oklch(0.37 0.12 200);
128+
--avatar-7: oklch(0.38 0.14 10);
129+
--avatar-8: oklch(0.4 0.11 80);
130+
--avatar-9: oklch(0.36 0.13 270);
111131
}
112132

113133
a,

0 commit comments

Comments
 (0)