Avatar
An image element with a fallback for representing the user.
AQ
Installation
Install the following dependencies:
pnpm add @radix-ui/react-avatar
Copy and paste the following code into your project.
import * as AvatarPrimitive from "@radix-ui/react-avatar";
import { cn } from "~/utils/tailwind";
function Avatar({
className,
...props
}: React.ComponentProps<typeof AvatarPrimitive.Root>) {
return (
<AvatarPrimitive.Root
className={cn(
"relative flex size-10 shrink-0 overflow-hidden rounded-full",
className,
)}
{...props}
/>
);
}
function AvatarImage({
className,
...props
}: React.ComponentProps<typeof AvatarPrimitive.Image>) {
return (
<AvatarPrimitive.Image
className={cn("aspect-square size-full", className)}
{...props}
/>
);
}
function AvatarFallback({
className,
...props
}: React.ComponentProps<typeof AvatarPrimitive.Fallback>) {
return (
<AvatarPrimitive.Fallback
className={cn(
"flex size-full items-center justify-center rounded-full bg-muted",
className,
)}
{...props}
/>
);
}
export { Avatar, AvatarImage, AvatarFallback };
Update the import paths to match your project setup.
Usage
import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/Avatar";
<Avatar>
<AvatarImage src="https://github.com/qiaoandrew.png" alt="@qiaoandrew" />
<AvatarFallback>AQ</AvatarFallback>
</Avatar>
Examples
Fallback
<AvatarFallback>
is rendered while <AvatarImage>
is loading.
AQ
Initials
Remove <AvatarImage>
to display static content, like initials in this case.
AQ
Tooltip
AQ