'use client'; import { Avatar as GuiAvatar, XStack } from '@hanzo/gui'; import * as React from 'react'; import { ink } from './ink'; type ImageProps = React.ImgHTMLAttributes; // -- Size & variant mappings ------------------------------------------------ type AvatarSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'; type AvatarVariant = 'solid' | 'subtle' | 'outline'; const SIZE: Record = { xs: { width: 24, height: 24, fontSize: 10 }, sm: { width: 32, height: 32, fontSize: 12 }, md: { width: 40, height: 40, fontSize: 14 }, lg: { width: 48, height: 48, fontSize: 16 }, xl: { width: 64, height: 64, fontSize: 18 }, '2xl': { width: 80, height: 80, fontSize: 20 }, }; // The ring colour matches the page ground rather than staying a fixed white, // so a stack of overlapping avatars reads as cut out of the page in both // themes — the same job `ring-white dark:ring-[var(--color-border-divider)]` did. const RING = 'var(--color-bg-body)' as never; const VARIANT: Record> = { solid: { backgroundColor: 'var(--color-gray-500)' as never, color: 'var(--color-white)' as never }, subtle: { backgroundColor: 'var(--color-badge-gray-bg)' as never, color: 'var(--color-badge-gray-fg)' as never }, outline: { borderWidth: 2, borderColor: 'var(--color-input-border)' as never, backgroundColor: 'transparent', color: 'var(--color-text-secondary)' as never, }, }; const DEFAULT_SIZE: AvatarSize = 'md'; const DEFAULT_VARIANT: AvatarVariant = 'subtle'; // -- Context for AvatarGroup ------------------------------------------------ interface AvatarGroupContextValue { readonly size?: AvatarSize; readonly variant?: AvatarVariant; readonly borderless?: boolean; } const AvatarGroupContext = React.createContext({}); function useAvatarGroupContext(): AvatarGroupContextValue { return React.useContext(AvatarGroupContext); } // -- Avatar ----------------------------------------------------------------- export interface AvatarProps extends React.ComponentPropsWithoutRef<'span'> { name?: string; src?: string; srcSet?: string; loading?: ImageProps['loading']; icon?: React.ReactElement; fallback?: React.ReactNode; size?: AvatarSize; variant?: AvatarVariant; borderless?: boolean; } export const Avatar = React.forwardRef( function Avatar(props, ref) { const group = useAvatarGroupContext(); const { name, src, srcSet, loading, icon, fallback, children, size: sizeProp, variant: variantProp, borderless, ...rest } = props; const size = sizeProp ?? group.size ?? DEFAULT_SIZE; const variant = variantProp ?? group.variant ?? DEFAULT_VARIANT; const isBorderless = borderless ?? group.borderless ?? false; // gui's Avatar — the same load/fallback state machine, but as a gui // primitive, so it themes and lays out identically on native. return ( { fallback } { src != null && ( ) } { children } ); }, ); // -- AvatarFallback (internal) ---------------------------------------------- interface AvatarFallbackInternalProps extends React.ComponentPropsWithoutRef<'span'> { name?: string; icon?: React.ReactElement; } const AvatarFallback = React.forwardRef( function AvatarFallback(props, ref) { const { name, icon, children, ...rest } = props; return ( { ink(children ?? (name != null ? getInitials(name) : icon), undefined, { fontWeight: '500' }) } ); }, ); // -- getInitials ------------------------------------------------------------ function getInitials(name: string): string { const names = name.trim().split(' '); const firstName = names[0] != null ? names[0] : ''; const lastName = names.length > 1 ? names[names.length - 1] : ''; return firstName && lastName ? `${ firstName.charAt(0) }${ lastName.charAt(0) }` : firstName.charAt(0); } // -- AvatarGroup ------------------------------------------------------------ interface AvatarGroupProps extends React.ComponentPropsWithoutRef<'div'> { size?: AvatarSize; variant?: AvatarVariant; borderless?: boolean; } export const AvatarGroup = React.forwardRef( function AvatarGroup(props, ref) { const { size, variant, borderless, children, ...rest } = props; const contextValue = React.useMemo( () => ({ size, variant, borderless }), [ size, variant, borderless ], ); return ( { /* `-space-x-3`'s sibling-margin trick, done by cloning: each avatar after the first overlaps the one before it by 12px. */ } { React.Children.map(children, (child, index) => ( index === 0 || !React.isValidElement(child) ? child : React.cloneElement(child as React.ReactElement<{ marginLeft?: number }>, { marginLeft: -12 }) )) } ); }, );