Avatar

A person's picture, or their initials.

The fallback shows while the picture loads, and in its place if it can’t. Avatars stay round whatever a system’s radius; AvatarGroup overlaps several.

Preview
basic.tsx
import { Avatar, AvatarFallback, AvatarGroup, AvatarImage } from "~/components/ui/avatar"

export default function Example() {
  return (
    <div className="flex items-center gap-xl">
      <Avatar size="sm">
        <AvatarFallback>NB</AvatarFallback>
      </Avatar>
      <Avatar>
        <AvatarImage src="/examples/portrait.png" alt="Hollis Grant" />
        <AvatarFallback>HG</AvatarFallback>
      </Avatar>
      <Avatar size="lg">
        <AvatarFallback>AZ</AvatarFallback>
      </Avatar>
      <AvatarGroup>
        <Avatar>
          <AvatarFallback>AL</AvatarFallback>
        </Avatar>
        <Avatar>
          <AvatarFallback>BK</AvatarFallback>
        </Avatar>
        <Avatar>
          <AvatarFallback>+3</AvatarFallback>
        </Avatar>
      </AvatarGroup>
    </div>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesTextprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/avatar.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/avatar.tsx
import {
  Avatar as AvatarPrimitive,
  AvatarFallback as AvatarFallbackPrimitive,
  AvatarImage as AvatarImagePrimitive,
} from "@ui-sdk/react/avatar"
import type * as React from "react"

import { textVariants } from "~/components/ui/text"
import { cn, cnState } from "~/lib/utils"

type AvatarProps = React.ComponentProps<typeof AvatarPrimitive> & {
  /** Diameter: 24, 32 or 40 pixels. */
  size?: "sm" | "md" | "lg"
}

/** A person's picture, or their initials while it loads or when there's none. Round in every system's radius. */
function Avatar({ size = "md", className, ...props }: AvatarProps) {
  return (
    <AvatarPrimitive
      data-size={size}
      className={cnState(
        [
          textVariants({ size: size === "sm" ? "xs" : "sm" }),
          "group/avatar relative flex size-8 shrink-0 overflow-hidden rounded-full select-none after:absolute after:inset-0 after:rounded-full after:border after:border-foreground/10 data-[size=lg]:size-10 data-[size=sm]:size-6",
        ],
        className,
      )}
      {...props}
    />
  )
}

/** The picture. Give it the person's name as `alt`. */
function AvatarImage({ className, ...props }: React.ComponentProps<typeof AvatarImagePrimitive>) {
  return (
    <AvatarImagePrimitive
      className={cnState("aspect-square size-full object-cover", className)}
      {...props}
    />
  )
}

/** Shown until the picture loads, and in its place if it can't: usually initials. */
function AvatarFallback({
  className,
  ...props
}: React.ComponentProps<typeof AvatarFallbackPrimitive>) {
  return (
    <AvatarFallbackPrimitive
      className={cnState(
        [
          textVariants({ weight: "medium", tone: "muted" }),
          "flex size-full items-center justify-center bg-muted",
        ],
        className,
      )}
      {...props}
    />
  )
}

/** Overlapping avatars, like the people on a document. */
function AvatarGroup({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="avatar-group"
      className={cn(
        "flex -space-x-sm *:data-[slot=avatar]:ring-2 *:data-[slot=avatar]:ring-background",
        className,
      )}
      {...props}
    />
  )
}

export { Avatar, AvatarFallback, AvatarGroup, AvatarImage }
export type { AvatarProps }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Avatar

A person's picture, or their initials while it loads or when there's none. Round in every system's radius.

sizeours

Diameter: 24, 32 or 40 pixels.

Type "sm" | "md" | "lg" · Default md

className

Type string | ((state: NoInfer<AvatarState>) => string)

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

fallbackDelay

Type number

onLoadingStatusChange

Runs when the image's loading status changes: "loading" when it gets a source, "loaded", "error", and "idle" when it leaves the page. details.reason says why, and details.event is the image's load or error event, or undefined when the status was known at once, like for a cached image.

Type (status: AvatarLoadingStatus, details: ChangeDetails<AvatarLoadingStatusChangeReason>) => void

render

Type RenderProp<NoInfer<AvatarState>>

style

Type CSSProperties | ((state: NoInfer<AvatarState>) => CSSProperties)

AvatarFallback

Shown until the picture loads, and in its place if it can't: usually initials.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<AvatarState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<AvatarState>) => string)

delay

How long to wait, in milliseconds, before showing the fallback, so a fast image appears without the fallback flashing first. Without one, the fallback shows at once, server markup included.

Type number · Default 0

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<AvatarState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<AvatarState>) => CSSProperties)

AvatarGroup

Overlapping avatars, like the people on a document.

Takes the props of a native div.

AvatarImage

The picture. Give it the person's name as alt.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<AvatarState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<AvatarState>) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<AvatarState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<AvatarState>) => CSSProperties)