Skip to content
← Components

Avatar

Avatar with thoughtful defaults, semantic styling, and editable source.

Open ↗

Make it yours.

Install the editable source and its dependencies into your React + Tailwind project.

Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/avatar.json

Use it

Scroll horizontally for long lines
"use client";
import { Avatar } from "@/components/jez-ui/ui/avatar";

export default function Example() {
  return (
    <>
      <div className="grid justify-items-center gap-6">
        <div className="flex -space-x-3">
          {["AM", "JL", "SK", "RT"].map((person, i) => (
            <Avatar
              key={person}
              alt={["Alex Morgan", "Jamie Lee", "Sam Kim", "Rory Tate"][i]}
              fallback={person}
              className="size-14 border-4 border-background bg-muted text-base"
            />
          ))}
        </div>
        <div className="text-center">
          <h3 className="text-2xl">Better, together.</h3>
          <p className="mt-2 text-sm text-muted-foreground">
            Four people. One shared idea.
          </p>
        </div>
      </div>
    </>
  );
}

Compose your own

These styled parts are included in the same install. Use children to build your layout; add className only when you want an override.

Scroll horizontally for long lines
import { Avatar, AvatarImage, AvatarFallback } from "@/components/jez-ui/ui/avatar";
Read the composition guide →

Props & types

Scroll horizontally for long lines
React.ComponentProps<typeof P.Root> & {
  src?: string;
  alt?: string;
  fallback?: React.ReactNode;
}

Source

View 2 source files

registry/ui/avatar.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { Avatar as P } from "radix-ui";
import { cn } from "./utils";
export function Avatar({
  src,
  alt,
  fallback = "JU",
  children,
  className,
  ...props
}: React.ComponentProps<typeof P.Root> & {
  src?: string;
  alt?: string;
  fallback?: React.ReactNode;
}) {
  return (
    <P.Root
      className={cn(
        "relative inline-flex size-10 shrink-0 items-center justify-center overflow-hidden rounded-full bg-accent text-sm font-medium text-[#171817]",
        className,
      )}
      {...props}
    >
      {children ?? (
        <>
          <AvatarImage src={src} alt={alt} />
          <AvatarFallback role={alt ? "img" : undefined} aria-label={alt}>
            {fallback}
          </AvatarFallback>
        </>
      )}
    </P.Root>
  );
}
export function AvatarImage({
  className,
  ...props
}: React.ComponentProps<typeof P.Image>) {
  return (
    <P.Image className={cn("size-full object-cover", className)} {...props} />
  );
}
export function AvatarFallback({
  className,
  ...props
}: React.ComponentProps<typeof P.Fallback>) {
  return (
    <P.Fallback
      className={cn(
        "flex size-full items-center justify-center rounded-full bg-accent",
        className,
      )}
      {...props}
    />
  );
}

registry/ui/utils.ts

Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
  return twMerge(clsx(values));
}

Dependencies

radix-ui@1.6.7 · clsx@2.1.1 · tailwind-merge@3.5.0

Accessibility & behaviour

Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.

Read the accessibility and performance guide →