Files
buzz/desktop/src/features/agents/ui/AgentCreationPreview.tsx
Luke Tornquist 313f793c87 feat(desktop): add search to agent emoji picker (#2630)
## Why
The agent avatar picker disables Emoji Mart search, making emojis
difficult to find when creating or editing an agent.

## What
- Enable sticky search in the shared agent avatar picker
- Focus the search field when the Emoji tab opens
- Add end-to-end coverage for search, focus, and selection

## Risk Assessment
Low — this is limited to the desktop agent avatar picker and does not
change avatar persistence or agent configuration.

## References
- `pnpm --dir desktop test` — 3,448 passed
- `pnpm --dir desktop typecheck` — passed
- `pnpm --dir desktop exec playwright test tests/e2e/agents.spec.ts` —
20 passed

---
**Update Jul 24, 16:28 EDT:** Completed `CONTRIBUTING.md` validation.

### Manual test
1. Create an agent and open Add avatar → Emoji.
2. Confirm the search field is focused and filters results.
3. Select an emoji and confirm it becomes the avatar.
4. Repeat while editing an existing agent.

### Validation
- `just ci` — passed
- `just test` — passed

### Follow-up work
None.

Generated with Codex
2026-07-27 09:47:31 -07:00

1026 lines
38 KiB
TypeScript

import emojiData from "@emoji-mart/data";
import Picker from "@emoji-mart/react";
import * as React from "react";
import { Link2, Pencil, Plus, UploadCloud } from "lucide-react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { MaskedAvatarBadgeFrame } from "@/features/profile/ui/MaskedAvatarBadgeFrame";
import { ProfileAvatar } from "@/features/profile/ui/ProfileAvatar";
import {
AVATAR_COLORS,
AVATAR_COLOR_SWATCHES,
CUSTOM_AVATAR_COLOR_SWATCH,
DEFAULT_CUSTOM_HUE,
DEFAULT_CUSTOM_SATURATION,
DEFAULT_CUSTOM_VALUE,
DEFAULT_EMOJI_AVATAR_COLOR,
EMOJI_MART_CATEGORIES,
type AvatarColorSwatch,
contrastColorForBackground,
emojiAvatarDataUrl,
hexToHsv,
hsvToHex,
normalizeHue,
parseEmojiAvatarDataUrl,
useEmojiMartStyles,
useEmojiMartThemeVars,
} from "@/features/profile/ui/ProfileAvatarEditor.utils";
import { AvatarCustomColorPanel } from "@/features/profile/ui/AvatarCustomColorPanel";
import { useAvatarUpload } from "@/features/profile/useAvatarUpload";
import { cn } from "@/shared/lib/cn";
import { Button } from "@/shared/ui/button";
import { useEmojiBurst } from "@/shared/ui/EmojiBurstProvider";
import {
Popover,
PopoverAnchor,
PopoverContent,
PopoverTrigger,
} from "@/shared/ui/popover";
import { Spinner } from "@/shared/ui/spinner";
import { Tabs, TabsList, TabsTrigger } from "@/shared/ui/tabs";
import {
AVATAR_APPLY_MOTION_TRANSITION,
type AvatarTab,
type EmojiMartEmoji,
isAvatarFileDrag,
} from "./AgentCreationPreview.utils";
export function AgentCreationPreview({
assetLabel = "avatar",
avatarUrl,
disabled = false,
hideEditControl = false,
label,
onClearAvatar,
onCommitAvatar,
onUploadPendingChange,
onSelectAvatar,
processImage,
shape = "circle",
testIdPrefix = "agent-avatar",
variant = "default",
}: {
assetLabel?: string;
avatarUrl: string | null;
disabled?: boolean;
/** When true, omit all upload/edit controls and render the avatar as a
* plain display element. Use in contexts where avatar editing is
* handled by an external affordance (e.g. AgentInstanceEditDialog). */
hideEditControl?: boolean;
label: string;
onClearAvatar?: () => void;
onCommitAvatar?: (avatarUrl: string) => void;
onUploadPendingChange?: (isPending: boolean) => void;
onSelectAvatar: (avatarUrl: string) => void;
processImage?: (file: File) => Promise<string>;
shape?: "circle" | "rounded-square";
testIdPrefix?: string;
variant?: "compact" | "default";
}) {
const [isDragOverAvatar, setIsDragOverAvatar] = React.useState(false);
const [isAvatarMenuOpen, setIsAvatarMenuOpen] = React.useState(false);
const [avatarUrlDraft, setAvatarUrlDraft] = React.useState("");
const [activeTab, setActiveTab] = React.useState<AvatarTab>("image");
const [selectedEmoji, setSelectedEmoji] = React.useState<string | null>(null);
const [selectedColor, setSelectedColor] = React.useState(
DEFAULT_EMOJI_AVATAR_COLOR,
);
// Whether the user has explicitly picked a color swatch (vs. the default).
// The color grid is always visible, so a user can choose a background color
// before their first emoji — in that case the first emoji must honor the
// chosen color instead of a random one.
const [hasChosenColor, setHasChosenColor] = React.useState(false);
const [customHue, setCustomHue] = React.useState(DEFAULT_CUSTOM_HUE);
const [customSaturation, setCustomSaturation] = React.useState(
DEFAULT_CUSTOM_SATURATION,
);
const [customValue, setCustomValue] = React.useState(DEFAULT_CUSTOM_VALUE);
const [isCustomColorPickerOpen, setIsCustomColorPickerOpen] =
React.useState(false);
const [isPopoverDragOver, setIsPopoverDragOver] = React.useState(false);
const [squishKey, setSquishKey] = React.useState(0);
const avatarDragDepthRef = React.useRef(0);
const popoverDragDepthRef = React.useRef(0);
const shouldReduceMotion = useReducedMotion();
const emojiPickerContainerRef = React.useRef<HTMLDivElement | null>(null);
const emojiMartThemeVars = useEmojiMartThemeVars();
const { burstEmoji } = useEmojiBurst();
const assetLabelTitle =
assetLabel.charAt(0).toUpperCase() + assetLabel.slice(1);
const isRoundedSquare = shape === "rounded-square";
const isCompact = variant === "compact";
const emojiShape = isRoundedSquare ? "rounded-square" : "circle";
const {
inputRef: avatarUploadInputRef,
isUploading,
errorMessage: uploadErrorMessage,
clearError: clearUploadError,
openPicker: openUploadPicker,
uploadFile: uploadAvatarFile,
handleFileChange: handleAvatarUploadFileChange,
} = useAvatarUpload({
fallbackErrorMessage: `Could not use that ${assetLabel}.`,
onUploadSuccess: (url) => {
onSelectAvatar(url);
onCommitAvatar?.(url);
setIsAvatarMenuOpen(false);
},
processImage,
});
useEmojiMartStyles(
emojiPickerContainerRef,
isAvatarMenuOpen && activeTab === "emoji",
);
// Emoji Mart mounts its search input inside a shadow root. Wait for it
// before focusing so the surrounding Radix popover cannot win the race.
React.useEffect(() => {
if (!isAvatarMenuOpen || activeTab !== "emoji") {
return;
}
let animationFrame = 0;
const focusSearchInput = () => {
const searchInput =
emojiPickerContainerRef.current
?.querySelector("em-emoji-picker")
?.shadowRoot?.querySelector<HTMLInputElement>(
'input[type="search"]',
) ?? null;
if (!searchInput) {
animationFrame = window.requestAnimationFrame(focusSearchInput);
return;
}
searchInput.focus();
};
animationFrame = window.requestAnimationFrame(focusSearchInput);
return () => window.cancelAnimationFrame(animationFrame);
}, [activeTab, isAvatarMenuOpen]);
const customColorDraft = React.useMemo(
() => hsvToHex(customHue, customSaturation, customValue),
[customHue, customSaturation, customValue],
);
React.useEffect(() => {
onUploadPendingChange?.(isUploading);
return () => {
onUploadPendingChange?.(false);
};
}, [isUploading, onUploadPendingChange]);
// Sync emoji state from avatarUrl when the popover opens
React.useEffect(() => {
if (isAvatarMenuOpen) {
setAvatarUrlDraft("");
setIsPopoverDragOver(false);
popoverDragDepthRef.current = 0;
const parsed = parseEmojiAvatarDataUrl(avatarUrl ?? "");
if (parsed) {
setSelectedEmoji(parsed.emoji);
setSelectedColor(parsed.color);
setHasChosenColor(true);
setActiveTab("emoji");
} else {
// Non-emoji avatar (image/URL or empty): clear any stale emoji
// selection so a later color-swatch tap can't re-apply an old emoji
// over the current avatar.
setSelectedEmoji(null);
setSelectedColor(DEFAULT_EMOJI_AVATAR_COLOR);
setHasChosenColor(false);
setActiveTab("image");
}
}
}, [isAvatarMenuOpen, avatarUrl]);
// Keep the custom color picker in sync when the selected color changes
React.useEffect(() => {
if (!isCustomColorPickerOpen || !selectedEmoji) {
return;
}
const nextAvatarUrl = emojiAvatarDataUrl(
selectedEmoji,
customColorDraft,
emojiShape,
);
if (avatarUrl === nextAvatarUrl) {
return;
}
onSelectAvatar(nextAvatarUrl);
}, [
avatarUrl,
customColorDraft,
isCustomColorPickerOpen,
onSelectAvatar,
selectedEmoji,
emojiShape,
]);
function applyAvatarUrl() {
const nextUrl = avatarUrlDraft.trim();
if (nextUrl.length === 0) {
return;
}
clearUploadError();
onSelectAvatar(nextUrl);
onCommitAvatar?.(nextUrl);
setIsAvatarMenuOpen(false);
}
function applyEmojiAvatar(emoji: string, color = selectedColor) {
const nextAvatarUrl = emojiAvatarDataUrl(emoji, color, emojiShape);
onSelectAvatar(nextAvatarUrl);
onCommitAvatar?.(nextAvatarUrl);
setSquishKey((key) => key + 1);
}
function clearAvatar() {
onClearAvatar?.();
onCommitAvatar?.("");
}
function handleColorSelect(swatch: AvatarColorSwatch) {
if (disabled) {
return;
}
if (swatch === CUSTOM_AVATAR_COLOR_SWATCH) {
if (!selectedEmoji) {
return;
}
openCustomColorPicker();
return;
}
setSelectedColor(swatch);
setHasChosenColor(true);
if (selectedEmoji) {
applyEmojiAvatar(selectedEmoji, swatch);
}
}
function openCustomColorPicker() {
const nextColor = hexToHsv(selectedColor);
setCustomHue(normalizeHue(nextColor.hue));
setCustomSaturation(nextColor.saturation);
setCustomValue(nextColor.value);
setIsCustomColorPickerOpen(true);
}
function commitCustomColor() {
setSelectedColor(customColorDraft);
setHasChosenColor(true);
if (selectedEmoji) {
applyEmojiAvatar(selectedEmoji, customColorDraft);
}
setIsCustomColorPickerOpen(false);
}
const hasAvatar = (avatarUrl?.trim().length ?? 0) > 0;
const emojiAvatarPreview = React.useMemo(
() => parseEmojiAvatarDataUrl(avatarUrl ?? ""),
[avatarUrl],
);
const applyButtonTransition = shouldReduceMotion
? { duration: 0 }
: AVATAR_APPLY_MOTION_TRANSITION;
// Outer avatar drag — only active when popover is closed
const handleAvatarDragEnter = React.useCallback(
(event: React.DragEvent<HTMLFieldSetElement>) => {
if (disabled || isAvatarMenuOpen || !isAvatarFileDrag(event)) {
return;
}
event.preventDefault();
event.stopPropagation();
avatarDragDepthRef.current += 1;
event.dataTransfer.dropEffect = "copy";
setIsDragOverAvatar(true);
},
[disabled, isAvatarMenuOpen],
);
const handleAvatarDragOver = React.useCallback(
(event: React.DragEvent<HTMLFieldSetElement>) => {
if (disabled || isAvatarMenuOpen || !isAvatarFileDrag(event)) {
return;
}
event.preventDefault();
event.stopPropagation();
event.dataTransfer.dropEffect = "copy";
setIsDragOverAvatar(true);
},
[disabled, isAvatarMenuOpen],
);
const handleAvatarDragLeave = React.useCallback(
(event: React.DragEvent<HTMLFieldSetElement>) => {
if (isAvatarMenuOpen || !isAvatarFileDrag(event)) {
return;
}
event.preventDefault();
event.stopPropagation();
avatarDragDepthRef.current = Math.max(0, avatarDragDepthRef.current - 1);
if (avatarDragDepthRef.current === 0) {
setIsDragOverAvatar(false);
}
},
[isAvatarMenuOpen],
);
const handleAvatarDrop = React.useCallback(
(event: React.DragEvent<HTMLFieldSetElement>) => {
if (isAvatarMenuOpen || !isAvatarFileDrag(event)) {
return;
}
event.preventDefault();
event.stopPropagation();
avatarDragDepthRef.current = 0;
setIsDragOverAvatar(false);
const file = event.dataTransfer.files[0];
if (!file || disabled || isUploading) {
return;
}
clearUploadError();
void uploadAvatarFile(file);
},
[
clearUploadError,
disabled,
isAvatarMenuOpen,
isUploading,
uploadAvatarFile,
],
);
// Popover-level drag — one big drop zone for the entire popover
const handlePopoverDragEnter = React.useCallback(
(event: React.DragEvent<HTMLElement>) => {
if (disabled || !isAvatarFileDrag(event)) {
return;
}
event.preventDefault();
event.stopPropagation();
popoverDragDepthRef.current += 1;
event.dataTransfer.dropEffect = "copy";
setIsPopoverDragOver(true);
setActiveTab("image");
},
[disabled],
);
const handlePopoverDragOver = React.useCallback(
(event: React.DragEvent<HTMLElement>) => {
if (disabled || !isAvatarFileDrag(event)) {
return;
}
event.preventDefault();
event.stopPropagation();
event.dataTransfer.dropEffect = "copy";
},
[disabled],
);
const handlePopoverDragLeave = React.useCallback(
(event: React.DragEvent<HTMLElement>) => {
if (!isAvatarFileDrag(event)) {
return;
}
event.preventDefault();
event.stopPropagation();
popoverDragDepthRef.current = Math.max(
0,
popoverDragDepthRef.current - 1,
);
if (popoverDragDepthRef.current === 0) {
setIsPopoverDragOver(false);
}
},
[],
);
const handlePopoverDrop = React.useCallback(
(event: React.DragEvent<HTMLElement>) => {
if (!isAvatarFileDrag(event)) {
return;
}
event.preventDefault();
event.stopPropagation();
popoverDragDepthRef.current = 0;
setIsPopoverDragOver(false);
const file = event.dataTransfer.files[0];
if (!file || disabled || isUploading) {
return;
}
clearUploadError();
void uploadAvatarFile(file);
},
[clearUploadError, disabled, isUploading, uploadAvatarFile],
);
const isCustomColorPickerVisible =
isCustomColorPickerOpen && selectedEmoji !== null;
const avatarMenuContent = (
<PopoverContent
align="center"
className="w-[400px] p-3"
side="bottom"
sideOffset={8}
>
{/* Single drop zone covering the entire popover */}
<fieldset
aria-label={`${assetLabelTitle} picker`}
className={cn(
"relative m-0 rounded-lg border-2 border-transparent p-0 transition-[border-color,background-color] duration-150",
isPopoverDragOver && "border-dashed border-primary bg-primary/5",
)}
onDragEnter={handlePopoverDragEnter}
onDragLeave={handlePopoverDragLeave}
onDragOver={handlePopoverDragOver}
onDrop={handlePopoverDrop}
>
<Tabs
className="w-full"
onValueChange={(tab) => {
setActiveTab(tab as AvatarTab);
setIsCustomColorPickerOpen(false);
}}
value={activeTab}
>
<TabsList className="relative isolate mb-3 grid h-9 w-full grid-cols-2 overflow-hidden rounded-lg bg-muted p-0.5">
<div
aria-hidden="true"
className="absolute bottom-0.5 left-0.5 top-0.5 z-0 rounded-md bg-background shadow-sm transition-transform duration-[250ms] ease-out"
style={{
transform: `translateX(${activeTab === "emoji" ? 100 : 0}%)`,
width: "calc((100% - 4px) / 2)",
}}
/>
<TabsTrigger
className="relative z-10 h-full rounded-md bg-transparent text-xs font-medium shadow-none transition-colors data-[state=active]:bg-transparent data-[state=active]:shadow-none"
value="image"
>
Image
</TabsTrigger>
<TabsTrigger
className="relative z-10 h-full rounded-md bg-transparent text-xs font-medium shadow-none transition-colors data-[state=active]:bg-transparent data-[state=active]:shadow-none"
value="emoji"
>
Emoji
</TabsTrigger>
</TabsList>
</Tabs>
{activeTab === "image" ? (
<div className="grid gap-2.5">
{/* Click to browse zone */}
<button
className="relative flex h-[80px] flex-col items-center justify-center gap-1.5 overflow-hidden rounded-lg border border-transparent bg-muted text-foreground transition-[background-color,border-color,box-shadow,color] duration-200 ease-out hover:bg-muted/80 disabled:opacity-60"
disabled={disabled || isUploading}
onClick={() => {
clearUploadError();
openUploadPicker();
}}
type="button"
>
{isUploading ? (
<Spinner
aria-hidden
className="h-5 w-5 border-2 text-muted-foreground"
/>
) : (
<UploadCloud className="h-5 w-5 text-muted-foreground" />
)}
<span className="text-xs font-medium text-muted-foreground">
{isUploading ? "Uploading..." : "Drop or browse"}
</span>
</button>
{/* URL input */}
<div className="flex h-10 items-center gap-2.5 rounded-lg bg-muted px-3">
<Link2 className="h-3.5 w-3.5 shrink-0 text-muted-foreground/60" />
<input
autoCapitalize="none"
autoCorrect="off"
className="min-w-0 flex-1 bg-transparent text-xs font-medium text-foreground outline-none placeholder:text-muted-foreground/50"
disabled={disabled || isUploading}
onChange={(event) => setAvatarUrlDraft(event.target.value)}
onKeyDown={(event) => {
if (event.key === "Enter") {
event.preventDefault();
applyAvatarUrl();
}
}}
placeholder="Paste a URL"
spellCheck={false}
type="url"
value={avatarUrlDraft}
/>
<AnimatePresence initial={false}>
{avatarUrlDraft.trim().length > 0 ? (
<motion.div
animate={{ opacity: 1, scale: 1, width: "auto" }}
className="overflow-hidden"
exit={{ opacity: 0, scale: 0.96, width: 0 }}
initial={{ opacity: 0, scale: 0.96, width: 0 }}
key="apply-url"
transition={applyButtonTransition}
>
<Button
className="h-6 px-2 text-2xs"
disabled={disabled || isUploading}
onClick={() => applyAvatarUrl()}
size="xs"
type="button"
>
Apply
</Button>
</motion.div>
) : null}
</AnimatePresence>
</div>
{uploadErrorMessage ? (
<p className="rounded-lg bg-destructive/10 px-3 py-2 text-xs font-medium text-destructive">
{uploadErrorMessage}
</p>
) : null}
{hasAvatar && onClearAvatar ? (
<button
className="flex min-h-8 w-full items-center justify-center rounded-lg text-xs text-destructive outline-hidden transition-colors duration-150 ease-out hover:bg-destructive/10 focus-visible:bg-destructive/10 focus-visible:outline-none disabled:pointer-events-none disabled:opacity-50"
disabled={disabled || isUploading}
onClick={() => {
clearAvatar();
setIsAvatarMenuOpen(false);
}}
type="button"
>
Remove {assetLabel}
</button>
) : null}
</div>
) : null}
{activeTab === "emoji" ? (
<div className="relative grid content-start gap-3">
<div
className="buzz-emoji-mart relative z-0 h-[280px] overflow-hidden rounded-lg bg-muted"
ref={emojiPickerContainerRef}
style={emojiMartThemeVars}
>
<Picker
autoFocus
categories={EMOJI_MART_CATEGORIES}
data={emojiData}
dynamicWidth
emojiButtonRadius="999px"
emojiButtonSize={44}
emojiSize={32}
icons="outline"
navPosition="bottom"
onEmojiSelect={(emoji: EmojiMartEmoji, event?: MouseEvent) => {
if (disabled) {
return;
}
if (!emoji.native) {
return;
}
const nextColor =
selectedEmoji === null && !hasChosenColor
? (AVATAR_COLORS[
Math.floor(Math.random() * AVATAR_COLORS.length)
] ?? DEFAULT_EMOJI_AVATAR_COLOR)
: selectedColor;
burstEmoji(emoji.native, event);
setSelectedEmoji(emoji.native);
setSelectedColor(nextColor);
applyEmojiAvatar(emoji.native, nextColor);
}}
previewPosition="none"
searchPosition="sticky"
set="native"
skinTonePosition="none"
theme="auto"
/>
</div>
{/* Color swatches — always visible */}
<div className="grid grid-cols-12 justify-items-center gap-1.5 rounded-lg bg-muted p-3">
{AVATAR_COLOR_SWATCHES.map((swatch) => {
const isCustomSwatch = swatch === CUSTOM_AVATAR_COLOR_SWATCH;
const isSelected = isCustomSwatch
? !AVATAR_COLORS.some(
(color) =>
color.toUpperCase() === selectedColor.toUpperCase(),
)
: swatch.toUpperCase() === selectedColor.toUpperCase();
return (
<button
aria-label={
isCustomSwatch
? selectedEmoji
? "Choose custom color"
: "Choose an emoji first"
: `Use ${swatch} background`
}
aria-pressed={isSelected}
className={cn(
"relative h-6 w-6 rounded-full border border-border transition-transform duration-150 ease-out hover:scale-[1.15] focus-visible:scale-[1.15] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
isCustomSwatch &&
!selectedEmoji &&
"cursor-not-allowed opacity-45 hover:scale-100 focus-visible:scale-100",
)}
disabled={isCustomSwatch && !selectedEmoji}
key={swatch}
onClick={() => handleColorSelect(swatch)}
style={{
background: isCustomSwatch
? isSelected
? selectedColor
: "conic-gradient(from 0deg, #ff4d4d, #ffe75c, #73ef75, #63c6f2, #b141ff, #ff4d4d)"
: swatch,
}}
type="button"
>
{isSelected ? (
<span
className="absolute inset-0.5 rounded-full border-2"
style={{
borderColor: contrastColorForBackground(
isCustomSwatch ? selectedColor : swatch,
),
}}
/>
) : null}
</button>
);
})}
</div>
<AvatarCustomColorPanel
colorDraft={customColorDraft}
hue={customHue}
onCommit={commitCustomColor}
onHueChange={setCustomHue}
onSaturationValueChange={(nextSaturation, nextValue) => {
setCustomSaturation(nextSaturation);
setCustomValue(nextValue);
}}
saturation={customSaturation}
testIdPrefix={testIdPrefix}
value={customValue}
visible={isCustomColorPickerVisible}
/>
{hasAvatar && onClearAvatar ? (
<button
className="flex min-h-8 w-full items-center justify-center rounded-lg text-xs text-destructive outline-hidden transition-colors duration-150 ease-out hover:bg-destructive/10 focus-visible:bg-destructive/10 focus-visible:outline-none disabled:pointer-events-none disabled:opacity-50"
disabled={disabled}
onClick={() => {
clearAvatar();
setSelectedEmoji(null);
setIsAvatarMenuOpen(false);
}}
type="button"
>
Remove {assetLabel}
</button>
) : null}
</div>
) : null}
</fieldset>
</PopoverContent>
);
// Display-only path: no upload controls, no pencil badge, no popover.
// Used when the caller provides its own edit affordance.
if (hideEditControl) {
return (
<div
className={cn(
"w-full",
isCompact ? "w-auto" : "mx-auto max-w-[220px] lg:sticky lg:top-0",
)}
>
<div
className={cn(
"group/avatar-preview relative m-0 flex min-w-0 flex-col items-center justify-center rounded-xl border border-transparent p-0",
isCompact ? "min-h-0" : "min-h-[190px] gap-3",
)}
>
<div
className={cn("relative", isCompact ? "h-16 w-16" : "h-36 w-36")}
>
{emojiAvatarPreview ? (
<div
aria-label={`${label} ${assetLabel}`}
className={cn(
"relative flex h-full w-full shrink-0 items-center justify-center overflow-hidden shadow-xs transition-[background-color] duration-200 ease-out",
isRoundedSquare
? isCompact
? "rounded-2xl"
: "rounded-[2rem]"
: "rounded-full",
)}
role="img"
style={{ backgroundColor: emojiAvatarPreview.color }}
>
<span
className={cn(
"flex h-full w-full items-center justify-center leading-none",
isCompact ? "text-2xl" : "text-[4rem]",
)}
>
{emojiAvatarPreview.emoji}
</span>
</div>
) : isRoundedSquare && avatarUrl ? (
<img
alt={`${label} ${assetLabel}`}
className={cn(
"h-full w-full object-cover shadow-xs",
isCompact ? "rounded-2xl" : "rounded-[2rem]",
)}
src={avatarUrl}
/>
) : (
<ProfileAvatar
avatarUrl={avatarUrl}
className={cn(
"h-full w-full",
isCompact ? "text-base" : "text-4xl",
)}
label={label}
/>
)}
</div>
</div>
</div>
);
}
return (
<div
className={cn(
"w-full",
isCompact ? "w-auto" : "mx-auto max-w-[220px] lg:sticky lg:top-0",
)}
>
<fieldset
aria-label={`${assetLabelTitle} preview`}
className={cn(
"group/avatar-preview relative m-0 flex min-w-0 flex-col items-center justify-center rounded-xl border border-transparent p-0 transition-[background-color,border-color,box-shadow] duration-150",
isCompact ? "min-h-0" : "min-h-[190px] gap-3",
isDragOverAvatar &&
!isAvatarMenuOpen &&
"border-dashed border-primary/70 bg-primary/5 ring-2 ring-primary/15",
)}
onDragEnter={handleAvatarDragEnter}
onDragLeave={handleAvatarDragLeave}
onDragOver={handleAvatarDragOver}
onDrop={handleAvatarDrop}
>
<input
accept="image/gif,image/jpeg,image/png,image/webp"
className="hidden"
onChange={handleAvatarUploadFileChange}
ref={avatarUploadInputRef}
type="file"
/>
<Popover open={isAvatarMenuOpen} onOpenChange={setIsAvatarMenuOpen}>
<PopoverAnchor asChild>
<div
className={cn("relative", isCompact ? "h-16 w-16" : "h-36 w-36")}
>
{hasAvatar && isRoundedSquare ? (
<MaskedAvatarBadgeFrame
badge={
<PopoverTrigger asChild>
<button
aria-label={`Edit ${assetLabel}`}
className={cn(
"flex items-center justify-center rounded-full bg-sidebar-active text-sidebar-active-foreground shadow-lg transition-[background-color,scale] duration-150 ease-out hover:scale-[1.04] hover:bg-sidebar-active focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-default disabled:opacity-90 disabled:hover:scale-100",
isCompact ? "h-6 w-6" : "h-9 w-9",
)}
disabled={disabled || isUploading}
title={`Edit ${assetLabel}`}
type="button"
>
{isUploading ? (
<Spinner
aria-label={`Uploading ${assetLabel}`}
className={cn(
"border-2",
isCompact ? "h-3 w-3" : "h-4 w-4",
)}
/>
) : (
<Pencil
className={isCompact ? "h-3 w-3" : "h-4 w-4"}
/>
)}
</button>
</PopoverTrigger>
}
badgeBox={
isCompact
? { bottom: 0, height: 28, right: 0, width: 28 }
: { bottom: 0, height: 42, right: 0, width: 42 }
}
className={isCompact ? "h-16 w-16" : "h-36 w-36"}
clipTestId={`${testIdPrefix}-mask`}
cornerRadius={isCompact ? 16 : 32}
cutout={
isCompact
? { cx: 58, cy: 58, r: 16.5 }
: { cx: 123, cy: 123, r: 24 }
}
maskMode={isCompact ? "radial" : "clip-path"}
size={isCompact ? 64 : 144}
>
{emojiAvatarPreview ? (
<div
aria-label={`${label} ${assetLabel}`}
className={cn(
"relative flex h-full w-full shrink-0 items-center justify-center overflow-hidden shadow-xs transition-[background-color] duration-200 ease-out",
isCompact ? "rounded-2xl" : "rounded-[2rem]",
)}
role="img"
style={{
backgroundColor: emojiAvatarPreview.color,
}}
>
<span
className={cn(
"flex h-full w-full items-center justify-center leading-none",
isCompact ? "text-2xl" : "text-[4rem]",
squishKey > 0 && "buzz-avatar-squish",
)}
key={squishKey}
style={
{
"--buzz-avatar-emoji-offset-x": "0px",
"--buzz-avatar-emoji-offset-y": "0px",
} as React.CSSProperties
}
>
{emojiAvatarPreview.emoji}
</span>
</div>
) : (
<img
alt={`${label} ${assetLabel}`}
className={cn(
"h-full w-full object-cover shadow-xs transition-shadow duration-150",
isCompact ? "rounded-2xl" : "rounded-[2rem]",
isDragOverAvatar &&
!isAvatarMenuOpen &&
"ring-2 ring-primary/30",
)}
src={avatarUrl ?? ""}
/>
)}
</MaskedAvatarBadgeFrame>
) : hasAvatar ? (
<MaskedAvatarBadgeFrame
badge={
<PopoverTrigger asChild>
<button
aria-label={`Edit ${assetLabel}`}
className={cn(
"flex items-center justify-center rounded-full bg-sidebar-active text-sidebar-active-foreground shadow-lg transition-[background-color,scale] duration-150 ease-out hover:scale-[1.04] hover:bg-sidebar-active focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-default disabled:opacity-90 disabled:hover:scale-100",
isCompact ? "h-6 w-6" : "h-9 w-9",
)}
disabled={disabled || isUploading}
title={`Edit ${assetLabel}`}
type="button"
>
{isUploading ? (
<Spinner
aria-label={`Uploading ${assetLabel}`}
className={cn(
"border-2",
isCompact ? "h-3 w-3" : "h-4 w-4",
)}
/>
) : (
<Pencil
className={isCompact ? "h-3 w-3" : "h-4 w-4"}
/>
)}
</button>
</PopoverTrigger>
}
badgeBox={
isCompact
? { bottom: 0, height: 28, right: 0, width: 28 }
: { bottom: 0, height: 42, right: 0, width: 42 }
}
className={isCompact ? "h-16 w-16" : "h-36 w-36"}
clipTestId={`${testIdPrefix}-mask`}
cutout={
isCompact
? { cx: 58, cy: 58, r: 16.5 }
: { cx: 123, cy: 123, r: 24 }
}
maskMode={isCompact ? "radial" : "clip-path"}
size={isCompact ? 64 : 144}
>
{emojiAvatarPreview ? (
<div
aria-label={`${label} ${assetLabel}`}
className="relative flex h-full w-full shrink-0 items-center justify-center overflow-hidden rounded-full shadow-xs transition-[background-color] duration-200 ease-out"
role="img"
style={{
backgroundColor: emojiAvatarPreview.color,
}}
>
<span
className={cn(
"flex h-full w-full items-center justify-center leading-none",
isCompact ? "text-2xl" : "text-[4rem]",
squishKey > 0 && "buzz-avatar-squish",
)}
key={squishKey}
style={
{
"--buzz-avatar-emoji-offset-x": "0px",
"--buzz-avatar-emoji-offset-y": "0px",
} as React.CSSProperties
}
>
{emojiAvatarPreview.emoji}
</span>
</div>
) : (
<ProfileAvatar
avatarUrl={avatarUrl}
className={cn(
"h-full w-full transition-shadow duration-150",
isCompact ? "text-base" : "text-4xl",
isDragOverAvatar &&
!isAvatarMenuOpen &&
"ring-2 ring-primary/30",
)}
label={label}
/>
)}
</MaskedAvatarBadgeFrame>
) : (
<PopoverTrigger asChild>
<button
aria-label={`Add ${assetLabel}`}
className={cn(
"flex items-center justify-center border-2 border-dashed border-border bg-background text-primary shadow-xs transition-[background-color,border-color,color,box-shadow,scale] duration-150 ease-out hover:scale-[1.02] hover:border-primary/60 hover:bg-primary/5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-default disabled:opacity-60 disabled:hover:scale-100",
isCompact ? "h-16 w-16" : "h-36 w-36",
isRoundedSquare
? isCompact
? "rounded-2xl"
: "rounded-[2rem]"
: "rounded-full",
isDragOverAvatar &&
!isAvatarMenuOpen &&
"border-primary/70 bg-primary/5 ring-2 ring-primary/15",
)}
disabled={disabled || isUploading}
title={`Add ${assetLabel}`}
type="button"
>
{isUploading ? (
<Spinner
aria-label={`Uploading ${assetLabel}`}
className="h-4 w-4 border-2"
/>
) : (
<Plus
aria-hidden="true"
className={isCompact ? "h-6 w-6" : "h-14 w-14"}
/>
)}
</button>
</PopoverTrigger>
)}
</div>
</PopoverAnchor>
{avatarMenuContent}
</Popover>
{uploadErrorMessage ? (
<p className="max-w-full rounded-md bg-background/95 px-2 py-1 text-center text-xs text-destructive shadow-xs">
{uploadErrorMessage}
</p>
) : null}
</fieldset>
</div>
);
}