Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

💄 Colour avatar fallbacks by name (#3353)

Luke Vellaf099526298d4
diff --git a/apps/landing/src/components/home/hero-demo/demo-presets.ts b/apps/landing/src/components/home/hero-demo/demo-presets.ts
index 2fca57242..1acb41b80 100644
--- a/apps/landing/src/components/home/hero-demo/demo-presets.ts
+++ b/apps/landing/src/components/home/hero-demo/demo-presets.ts
@@ -195,7 +195,7 @@ const defaultParticipants: DemoPreset["participants"] = [
votes: ["ifNeedBe", "yes", "no", "yes", "yes", "yes", "no", "no"],
},
{
- name: "Priya Patel",
+ name: "Claire Fontaine",
votes: ["yes", "yes", "yes", "no", "yes", "yes", "no", "yes"],
},
{
diff --git a/apps/landing/src/components/home/hero-demo/desktop-demo.tsx b/apps/landing/src/components/home/hero-demo/desktop-demo.tsx
index 8e45c7e70..064824fe9 100644
--- a/apps/landing/src/components/home/hero-demo/desktop-demo.tsx
+++ b/apps/landing/src/components/home/hero-demo/desktop-demo.tsx
@@ -1,4 +1,5 @@
import { cn } from "@rallly/ui";
+import { Avatar, AvatarFallback } from "@rallly/ui/avatar";
import type { TFunction } from "i18next";
import {
ClockIcon,
@@ -194,9 +195,11 @@ export const DesktopDemo = ({
{preset.participants.map((participant) => (
<React.Fragment key={participant.name}>
<div className="flex items-center gap-2.5 border-gray-100 border-t px-3 py-3 text-left">
- <span className="flex size-7 shrink-0 items-center justify-center rounded-full bg-gray-100 font-medium text-[10px] text-gray-600 uppercase">
- {getInitials(participant.name)}
- </span>
+ <Avatar size="sm" className="size-7" bordered={false}>
+ <AvatarFallback seed={participant.name}>
+ {getInitials(participant.name)}
+ </AvatarFallback>
+ </Avatar>
<span className="min-w-0 flex-1 truncate text-gray-800 text-sm">
{participant.name}
</span>
diff --git a/apps/web/package.json b/apps/web/package.json
index cf3810747..7810caab7 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -62,7 +62,6 @@
"better-auth": "1.6.30",
"calendar-link": "^2.6.0",
"class-variance-authority": "^0.7.1",
- "color-hash": "^2.0.2",
"cookie": "^0.7.0",
"dayjs": "^1.11.13",
"effect": "4.0.0-rc.116",
@@ -115,7 +114,6 @@
"@testing-library/jest-dom": "^6.7.0",
"@testing-library/react": "^16.3.0",
"@testing-library/user-event": "^14.6.1",
- "@types/color-hash": "^1.0.2",
"@types/js-cookie": "^3.0.1",
"@types/lodash": "^4.14.178",
"@types/node": "^24.1.0",
diff --git a/apps/web/src/components/optimized-avatar-image.tsx b/apps/web/src/components/optimized-avatar-image.tsx
index 4ca91cfd4..f6a0c6143 100644
--- a/apps/web/src/components/optimized-avatar-image.tsx
+++ b/apps/web/src/components/optimized-avatar-image.tsx
@@ -1,6 +1,9 @@
+"use client";
+
import { cn } from "@rallly/ui";
import { Avatar, AvatarFallback, AvatarImage } from "@rallly/ui/avatar";
import { UserIcon } from "lucide-react";
+import * as React from "react";
import { resolveStorageUrl } from "@/lib/storage/resolve-storage-url";
export function OptimizedAvatarImage({
@@ -23,11 +26,27 @@ export function OptimizedAvatarImage({
.replace(/[^\p{L}]/gu, "")
.toUpperCase();
+ const hasInitials = /^\p{L}+$/u.test(initials);
+ // The border belongs to a photo. Keyed on load status rather than on src
+ // so a missing or failed image falls back to the edgeless tinted disc.
+ const [imageLoaded, setImageLoaded] = React.useState(false);
+
return (
- <Avatar className={className} size={size === "md" ? "default" : size}>
- <AvatarImage src={src ? resolveStorageUrl(src) : undefined} alt={name} />
- <AvatarFallback className={cn("shrink-0")}>
- {/^\p{L}+$/u.test(initials) ? (
+ <Avatar
+ className={className}
+ size={size === "md" ? "default" : size}
+ bordered={imageLoaded || !hasInitials}
+ >
+ <AvatarImage
+ src={src ? resolveStorageUrl(src) : undefined}
+ alt={name}
+ onLoadingStatusChange={(status) => setImageLoaded(status === "loaded")}
+ />
+ <AvatarFallback
+ className={cn("shrink-0")}
+ seed={hasInitials ? name.trim() : undefined}
+ >
+ {hasInitials ? (
initials
) : (
<UserIcon className="size-4 shrink-0 text-muted-foreground" />
diff --git a/packages/ui/src/avatar.tsx b/packages/ui/src/avatar.tsx
index e846c5934..a86537611 100644
--- a/packages/ui/src/avatar.tsx
+++ b/packages/ui/src/avatar.tsx
@@ -71,15 +71,39 @@ function AvatarImage({ className, ...props }: AvatarPrimitive.Image.Props) {
);
}
+// Cool half of the palette only, so a fallback never reads as an error or
+// a warning. Full class strings so Tailwind can see them.
+const avatarPalette = [
+ "bg-lime-50 text-lime-700 dark:bg-lime-950 dark:text-lime-300",
+ "bg-emerald-50 text-emerald-700 dark:bg-emerald-950 dark:text-emerald-300",
+ "bg-sky-50 text-sky-700 dark:bg-sky-950 dark:text-sky-300",
+ "bg-cyan-50 text-cyan-700 dark:bg-cyan-950 dark:text-cyan-300",
+ "bg-violet-50 text-violet-700 dark:bg-violet-950 dark:text-violet-300",
+ "bg-indigo-50 text-indigo-700 dark:bg-indigo-950 dark:text-indigo-300",
+ "bg-fuchsia-50 text-fuchsia-700 dark:bg-fuchsia-950 dark:text-fuchsia-300",
+ "bg-purple-50 text-purple-700 dark:bg-purple-950 dark:text-purple-300",
+];
+
+function paletteFromSeed(seed: string) {
+ let hash = 0;
+ for (const char of seed) {
+ hash = (hash * 31 + (char.codePointAt(0) ?? 0)) >>> 0;
+ }
+ return avatarPalette[hash % avatarPalette.length];
+}
+
function AvatarFallback({
className,
+ seed,
...props
-}: AvatarPrimitive.Fallback.Props) {
+}: AvatarPrimitive.Fallback.Props & { seed?: string }) {
return (
<AvatarPrimitive.Fallback
data-slot="avatar-fallback"
className={cn(
"flex size-full items-center justify-center rounded-[inherit] bg-muted text-muted-foreground text-xs group-data-[size=lg]/avatar:text-sm group-data-[size=sm]/avatar:text-[0.625rem] group-data-[size=xl]/avatar:text-base",
+ seed && "font-semibold",
+ seed && paletteFromSeed(seed),
className,
)}
{...props}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 29951ab2f..ece3960e4 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -97,7 +97,7 @@ importers:
version: 8.1.0([email protected])
'@vercel/analytics':
specifier: ^1.5.0
dayjs:
specifier: ^1.11.13
version: 1.11.19
@@ -139,7 +139,7 @@ importers:
version: 6.0.0(@types/[email protected])([email protected])
next-seo:
specifier: ^6.1.0
pg:
specifier: ^8.17.1
version: 8.18.0
@@ -321,9 +321,6 @@ importers:
class-variance-authority:
specifier: ^0.7.1
version: 0.7.1
- color-hash:
- specifier: ^2.0.2
- version: 2.0.2
cookie:
specifier: ^0.7.0
version: 0.7.2
@@ -475,9 +472,6 @@ importers:
'@testing-library/user-event':
specifier: ^14.6.1
version: 14.6.1(@testing-library/[email protected])
- '@types/color-hash':
- specifier: ^1.0.2
- version: 1.0.5
'@types/js-cookie':
specifier: ^3.0.1
version: 3.0.6
@@ -7620,9 +7614,6 @@ packages:
resolution: {integrity: sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==}
engines: {node: '>=7.0.0'}
- resolution: {integrity: sha512-6exeENAqBTuIR1wIo36mR8xVVBv6l1hSLd7Qmvf6158Ld1L15/dbahR9VUOiX7GmGJBCnQyS0EY+I8x+wa7egg==}
-
resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==}
@@ -19795,9 +19786,9 @@ snapshots:
dependencies:
uncrypto: 0.1.3
optionalDependencies:
react: 19.2.6
@@ -20667,8 +20658,6 @@ snapshots:
dependencies:
color-name: 1.1.4
-
@@ -23896,9 +23885,9 @@ snapshots:
react: 19.2.6
react-dom: 19.2.6([email protected])
dependencies:
react: 19.2.6
react-dom: 19.2.6([email protected])
@@ -23934,33 +23923,6 @@ snapshots:
- '@types/node'
- babel-plugin-macros
- dependencies:
- '@next/env': 16.3.3
- '@swc/helpers': 0.5.23
- baseline-browser-mapping: 2.11.19
- caniuse-lite: 1.0.30001810
- postcss: 8.5.23
- react: 19.2.6
- react-dom: 19.2.6([email protected])
- styled-jsx: 5.1.6(@babel/[email protected])([email protected])
- optionalDependencies:
- '@next/swc-darwin-arm64': 16.3.3
- '@next/swc-darwin-x64': 16.3.3
- '@next/swc-linux-arm64-gnu': 16.3.3
- '@next/swc-linux-arm64-musl': 16.3.3
- '@next/swc-linux-x64-gnu': 16.3.3
- '@next/swc-linux-x64-musl': 16.3.3
- '@next/swc-win32-arm64-msvc': 16.3.3
- '@next/swc-win32-x64-msvc': 16.3.3
- '@opentelemetry/api': 1.9.0
- '@playwright/test': 1.58.1
- sharp: 0.35.3(@types/[email protected])
- transitivePeerDependencies:
- - '@babel/core'
- - '@types/node'
- - babel-plugin-macros
-
dependencies:
'@next/env': 16.3.3