A component that shows a user image with a text fallback. The Avatar component extends Base UI's Avatar. It adds a squircle shape and fallback text that scales with the avatar size.
Here is the component implementation code that you can copy to your project:
// https://devie-ui.com/components/avatar
// https://base-ui.com/react/components/avatar
import { Avatar as BaseAvatar } from "@base-ui/react/avatar";
import clsx from "clsx";
import styles from "./Avatar.module.scss";
function Root({ className, ...props }: Avatar.Root.Props) {
return (
<BaseAvatar.Root className={clsx(styles.root, className)} {...props} />
);
}
function Image({ className, ...props }: Avatar.Image.Props) {
return (
<BaseAvatar.Image className={clsx(styles.image, className)} {...props} />
);
}
function Fallback({ className, ...props }: Avatar.Fallback.Props) {
return (
<BaseAvatar.Fallback
className={clsx(styles.fallback, className)}
{...props}
/>
);
}
const Avatar = {
Root,
Image,
Fallback,
};
namespace Avatar {
export namespace Root {
export type Props = Omit<BaseAvatar.Root.Props, "className"> & {
className?: string;
};
}
export namespace Image {
export type Props = Omit<BaseAvatar.Image.Props, "className"> & {
className?: string;
};
}
export namespace Fallback {
export type Props = Omit<BaseAvatar.Fallback.Props, "className"> & {
className?: string;
};
}
}
export default Avatar;Place Avatar.Image and Avatar.Fallback inside Avatar.Root. The fallback shows while the image loads or if it fails.
Put the user's initials in Avatar.Fallback. It replaces the image when the image fails or Avatar.Image is omitted.
Set width and height on Avatar.Root with style or a className. The default is 48px, and the fallback text scales with the avatar.