forked from mtusi2026/mtusi2026lab4
27 lines
558 B
TypeScript
27 lines
558 B
TypeScript
import { initials } from "../lib/format";
|
|
|
|
type AvatarProps = {
|
|
label: string;
|
|
seed?: string;
|
|
size?: "small" | "medium" | "large";
|
|
};
|
|
|
|
function tone(seed: string): number {
|
|
let hash = 0;
|
|
for (const character of seed) {
|
|
hash = (hash * 31 + character.codePointAt(0)!) >>> 0;
|
|
}
|
|
return hash % 5;
|
|
}
|
|
|
|
export function Avatar({ label, seed = label, size = "medium" }: AvatarProps) {
|
|
return (
|
|
<span
|
|
aria-hidden="true"
|
|
className={`avatar avatar--${size} avatar--tone-${tone(seed)}`}
|
|
>
|
|
{initials(label)}
|
|
</span>
|
|
);
|
|
}
|