fix: text in avatar auto shrink with width

This commit is contained in:
Tristan Yang
2023-03-14 22:04:29 +08:00
parent f993b32051
commit a27ac2849e
+6 -1
View File
@@ -36,10 +36,15 @@ const Avatar: FC<Props> = ({
if (!error && src) { if (!error && src) {
return <img width={width} height={height} src={src} onError={handleError} {...rest} />; return <img width={width} height={height} src={src} onError={handleError} {...rest} />;
} }
// 长度限制在六个字符
let initials = getInitials(name).substring(0, 6);
const len = initials.length;
const scaleVal = len > 2 ? (11 - len) / 10 : 1;
return ( return (
<div <div
className={`rounded-full flex-center ${rest.className || ""}`} className={`rounded-full flex-center ${rest.className || ""}`}
style={{ style={{
width, width,
height, height,
fontSize: getFontSize(width), fontSize: getFontSize(width),
@@ -49,7 +54,7 @@ const Avatar: FC<Props> = ({
color: type === "channel" ? "#475467" : "#FFFFFF" color: type === "channel" ? "#475467" : "#FFFFFF"
}} }}
> >
{getInitials(name)} <span className="whitespace-nowrap" style={{ transform: `scale(${scaleVal})` }}>{initials}</span>
</div> </div>
); );
}; };