function getRealSize(size: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl') {
switch (size) {
case 'xs': return 'size-4';
case 'sm': return 'size-6';
case 'md': return 'size-8';
case 'lg': return 'size-10';
case 'xl': return 'size-12';
case '2xl': return 'size-14';
default: throw new Error('Unexpected size: ' + size);
}
}
interface AvatarProps {
shape?: "circle" | "rect"
src?: string
name?: string
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'
color?: string; // the color class
className?: string
}
export function Avatar({ size, src, name, shape = "circle", color = 'bg-gray-500', className }: AvatarProps) {
const rounded = shape === 'circle' ? 'rounded-full' : 'rounded-md';
const sizeClass = getRealSize(size || 'md');
if (src) {
return (
)
}
if (name) {
const [first, second] = name.split(' ');
let text = second ? `${first[0]}${second[0]}` : `${first[0]}${first[1]}`;
return (
{text}
)
}
return (
)
}
interface SvgAvatarProps extends Omit {
children?: React.ReactNode
className?: string
}
export function SvgAvatar({ size, shape = "circle", color = 'bg-gray-500', className, children }: SvgAvatarProps) {
const rounded = shape === 'circle' ? 'rounded-full' : 'rounded-md';
const sizeClass = getRealSize(size || 'md');
return (
{children}
)
}