avatar.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import React from 'react';
  2. interface AvatarProps {
  3. name: string;
  4. src?: string | null;
  5. size?: 'sm' | 'md' | 'lg';
  6. className?: string;
  7. style?: React.CSSProperties;
  8. }
  9. const sizes: Record<string, string> = {
  10. sm: 'w-6 h-6 text-[10px]',
  11. md: 'w-8 h-8 text-xs',
  12. lg: 'w-10 h-10 text-sm',
  13. };
  14. export function Avatar({ name, src, size = 'md', className = '', style = {} }: AvatarProps) {
  15. const initials = name
  16. .split(' ')
  17. .map(n => n[0])
  18. .filter(Boolean)
  19. .slice(0, 2)
  20. .join('')
  21. .toUpperCase();
  22. const base = [
  23. 'rounded-full flex items-center justify-center font-semibold shrink-0',
  24. sizes[size] ?? sizes.md,
  25. className,
  26. ].join(' ');
  27. if (src) {
  28. return (
  29. <img
  30. src={src}
  31. alt={name}
  32. className={base}
  33. style={{ objectFit: 'cover', ...style }}
  34. />
  35. );
  36. }
  37. return (
  38. <div
  39. className={base}
  40. style={{
  41. background: 'rgba(99,102,241,0.20)',
  42. color: '#A5B4FC',
  43. border: '1px solid rgba(99,102,241,0.25)',
  44. ...style,
  45. }}
  46. title={name}
  47. >
  48. {initials}
  49. </div>
  50. );
  51. }