Command Palette

Search for a command to run...

Avatar

An image element with a fallback for representing the user.

CN

Installation

Follow these simple steps to add the Avatar component to your project:

Install Dependencies

pnpm add @radix-ui/react-avatar 

Create a new file components/ui/avatar.tsx and copy the code below:

"use client";
 
import * as React from "react";
import * as AvatarPrimitive from "@radix-ui/react-avatar";
 
import { cn } from "@/lib/utils";
 
function Avatar({
    className,
    ...props
}: React.ComponentProps<typeof AvatarPrimitive.Root>) {
    return (
        <AvatarPrimitive.Root
            data-slot="avatar"
            className={cn(
                "relative flex size-8 shrink-0 overflow-hidden rounded-full",
                className,
            )}
            {...props}
        />
    );
}
 
function AvatarImage({
    className,
    ...props
}: React.ComponentProps<typeof AvatarPrimitive.Image>) {
    return (
        <AvatarPrimitive.Image
            data-slot="avatar-image"
            className={cn("aspect-square size-full object-cover", className)}
            {...props}
        />
    );
}
 
function AvatarFallback({
    className,
    ...props
}: React.ComponentProps<typeof AvatarPrimitive.Fallback>) {
    return (
        <AvatarPrimitive.Fallback
            data-slot="avatar-fallback"
            className={cn(
                "flex size-full items-center justify-center rounded-full dark:border-neutral-800 bg-muted",
                className,
            )}
            {...props}
        />
    );
}
 
export { Avatar, AvatarImage, AvatarFallback };

Adjust the import paths in both files according to your project's structure.

Examples

Default

CNCNCNCN

Trusted by 60K+ developers.

With fallback

CNCNCN