|
| 1 | +import React from "react"; |
| 2 | +import { Avatar as AvatarPrimitive } from "@base-ui-components/react/avatar"; |
| 3 | +// utils |
| 4 | +import { cn } from "@plane/utils"; |
| 5 | + |
| 6 | +export type TAvatarSize = "sm" | "md" | "base" | "lg" | number; |
| 7 | + |
| 8 | +type Props = { |
| 9 | + name?: string; //The name of the avatar which will be displayed on the tooltip |
| 10 | + fallbackBackgroundColor?: string; //The background color if the avatar image fails to load |
| 11 | + fallbackText?: string; |
| 12 | + fallbackTextColor?: string; //The text color if the avatar image fails to load |
| 13 | + showTooltip?: boolean; |
| 14 | + size?: TAvatarSize; //The size of the avatars |
| 15 | + shape?: "circle" | "square"; |
| 16 | + src?: string; //The source of the avatar image |
| 17 | + className?: string; |
| 18 | +}; |
| 19 | + |
| 20 | +/** |
| 21 | + * Get the size details based on the size prop |
| 22 | + * @param size The size of the avatar |
| 23 | + * @returns The size details |
| 24 | + */ |
| 25 | +export const getSizeInfo = (size: TAvatarSize) => { |
| 26 | + switch (size) { |
| 27 | + case "sm": |
| 28 | + return { |
| 29 | + avatarSize: "h-4 w-4", |
| 30 | + fontSize: "text-xs", |
| 31 | + spacing: "-space-x-1", |
| 32 | + }; |
| 33 | + case "md": |
| 34 | + return { |
| 35 | + avatarSize: "h-5 w-5", |
| 36 | + fontSize: "text-xs", |
| 37 | + spacing: "-space-x-1", |
| 38 | + }; |
| 39 | + case "base": |
| 40 | + return { |
| 41 | + avatarSize: "h-6 w-6", |
| 42 | + fontSize: "text-sm", |
| 43 | + spacing: "-space-x-1.5", |
| 44 | + }; |
| 45 | + case "lg": |
| 46 | + return { |
| 47 | + avatarSize: "h-7 w-7", |
| 48 | + fontSize: "text-sm", |
| 49 | + spacing: "-space-x-1.5", |
| 50 | + }; |
| 51 | + default: |
| 52 | + return { |
| 53 | + avatarSize: "h-5 w-5", |
| 54 | + fontSize: "text-xs", |
| 55 | + spacing: "-space-x-1", |
| 56 | + }; |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * Get the border radius based on the shape prop |
| 62 | + * @param shape The shape of the avatar |
| 63 | + * @returns The border radius |
| 64 | + */ |
| 65 | +export const getBorderRadius = (shape: "circle" | "square") => { |
| 66 | + switch (shape) { |
| 67 | + case "circle": |
| 68 | + return "rounded-full"; |
| 69 | + case "square": |
| 70 | + return "rounded"; |
| 71 | + default: |
| 72 | + return "rounded-full"; |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +/** |
| 77 | + * Check if the value is a valid number |
| 78 | + * @param value The value to check |
| 79 | + * @returns Whether the value is a valid number or not |
| 80 | + */ |
| 81 | +export const isAValidNumber = (value: any) => typeof value === "number" && !isNaN(value); |
| 82 | + |
| 83 | +export const Avatar: React.FC<Props> = (props) => { |
| 84 | + const { |
| 85 | + name, |
| 86 | + fallbackBackgroundColor, |
| 87 | + fallbackText, |
| 88 | + fallbackTextColor, |
| 89 | + showTooltip = true, |
| 90 | + size = "md", |
| 91 | + shape = "circle", |
| 92 | + src, |
| 93 | + className = "", |
| 94 | + } = props; |
| 95 | + |
| 96 | + // get size details based on the size prop |
| 97 | + const sizeInfo = getSizeInfo(size); |
| 98 | + |
| 99 | + const fallbackLetter = name?.[0]?.toUpperCase() ?? fallbackText ?? "?"; |
| 100 | + return ( |
| 101 | + <div |
| 102 | + className={cn("grid place-items-center overflow-hidden", getBorderRadius(shape), { |
| 103 | + [sizeInfo.avatarSize]: !isAValidNumber(size), |
| 104 | + })} |
| 105 | + tabIndex={-1} |
| 106 | + > |
| 107 | + <AvatarPrimitive.Root className={cn("h-full w-full", getBorderRadius(shape), className)}> |
| 108 | + <AvatarPrimitive.Image src={src} width="48" height="48" /> |
| 109 | + <AvatarPrimitive.Fallback |
| 110 | + className={cn(sizeInfo.fontSize, "grid h-full w-full place-items-center", getBorderRadius(shape), className)} |
| 111 | + style={{ |
| 112 | + backgroundColor: fallbackBackgroundColor ?? "rgba(var(--color-primary-500))", |
| 113 | + color: fallbackTextColor ?? "#ffffff", |
| 114 | + }} |
| 115 | + > |
| 116 | + {fallbackLetter} |
| 117 | + </AvatarPrimitive.Fallback> |
| 118 | + </AvatarPrimitive.Root> |
| 119 | + </div> |
| 120 | + ); |
| 121 | +}; |
0 commit comments