Skip to content
2k
On This Page

Avatar

An image element with a fallback for representing the user.

AMER
AMLRER
+3
import {
  Avatar,
  AvatarBadge,
  AvatarFallback,
  AvatarGroup,
  AvatarGroupCount,
  AvatarImage
} from "~/components/ui/avatar"

export default function AvatarDemo() {
  return (
    <div class="flex flex-row flex-wrap items-center gap-6 md:gap-12">
      <Avatar>
        <AvatarImage src="/avatars/03.png" alt="@alex" class="grayscale" />
        <AvatarFallback>AM</AvatarFallback>
      </Avatar>
      <Avatar>
        <AvatarImage
          src="https://github.com/evilrabbit.png"
          alt="@evilrabbit"
        />
        <AvatarFallback>ER</AvatarFallback>
        <AvatarBadge class="bg-green-600 dark:bg-green-800" />
      </Avatar>
      <AvatarGroup class="grayscale">
        <Avatar>
          <AvatarImage src="/avatars/03.png" alt="@alex" />
          <AvatarFallback>AM</AvatarFallback>
        </Avatar>
        <Avatar>
          <AvatarImage
            src="https://github.com/maxleiter.png"
            alt="@maxleiter"
          />
          <AvatarFallback>LR</AvatarFallback>
        </Avatar>
        <Avatar>
          <AvatarImage
            src="https://github.com/evilrabbit.png"
            alt="@evilrabbit"
          />
          <AvatarFallback>ER</AvatarFallback>
        </Avatar>
        <AvatarGroupCount>+3</AvatarGroupCount>
      </AvatarGroup>
    </div>
  )
}

Installation

pnpm dlx solidui-cli@latest add avatar

Install the following dependencies:

pnpm add @ui-sdk/solid @ui-sdk/core

Copy and paste the following code into your project.

components/ui/avatar.tsx
import { type ComponentProps } from "@solidjs/web"
import {
  Avatar as SdkAvatar,
  AvatarImage as SdkAvatarImage,
  AvatarFallback as SdkAvatarFallback
} from "@ui-sdk/solid/avatar"
import { merge, omit } from "solid-js"

import { cn } from "~/lib/utils"
type AvatarProps = (Omit<ComponentProps<typeof SdkAvatar>, "class"> & {
  class?: string
}) & {
  size?: "sm" | "default" | "lg"
}
const Avatar = (props: AvatarProps) => {
  const local = merge({ size: "default" } as const, props) as AvatarProps
  const others = omit(props, "class", "size")
  return (
    <SdkAvatar
      class={cn(
        "size-8 rounded-full after:rounded-full data-[size=lg]:size-10 data-[size=sm]:size-6 group/avatar relative flex shrink-0 select-none after:absolute after:inset-0 after:border after:border-border after:mix-blend-darken dark:after:mix-blend-lighten",
        local.class
      )}
      data-size={local.size}

      {...others}
    />
  )
}
type AvatarImageProps = Omit<ComponentProps<typeof SdkAvatarImage>, "class"> & {
  class?: string
}
const AvatarImage = (props: AvatarImageProps) => (
  <SdkAvatarImage
    {...props}
    class={cn(
      "rounded-full aspect-square size-full object-cover",
      props.class
    )}
  />
)
type AvatarFallbackProps = Omit<
  ComponentProps<typeof SdkAvatarFallback>,
  "class"
> & {
  class?: string
}
const AvatarFallback = (props: AvatarFallbackProps) => (
  <SdkAvatarFallback
    {...props}
    class={cn(
      "rounded-full bg-muted text-muted-foreground flex size-full items-center justify-center text-sm group-data-[size=sm]/avatar:text-xs",
      props.class
    )}
  />
)
type AvatarBadgeProps = ComponentProps<"span">
const AvatarBadge = (props: AvatarBadgeProps) => (
  <span
    data-slot="avatar-badge"
    {...props}
    class={cn(
      "bg-primary text-primary-foreground ring-background absolute right-0 bottom-0 z-10 inline-flex items-center justify-center rounded-full bg-blend-color ring-2 select-none",
      "group-data-[size=sm]/avatar:size-2 group-data-[size=sm]/avatar:[&>svg]:hidden",
      "group-data-[size=default]/avatar:size-2.5 group-data-[size=default]/avatar:[&>svg]:size-2",
      "group-data-[size=lg]/avatar:size-3 group-data-[size=lg]/avatar:[&>svg]:size-2",
      props.class
    )}
  />
)
type AvatarGroupProps = ComponentProps<"div">
const AvatarGroup = (props: AvatarGroupProps) => (
  <div
    data-slot="avatar-group"
    {...props}
    class={cn(
      "group/avatar-group flex -space-x-2 *:data-[slot=avatar]:ring-2 *:data-[slot=avatar]:ring-background",
      props.class
    )}
  />
)
type AvatarGroupCountProps = ComponentProps<"div">
const AvatarGroupCount = (props: AvatarGroupCountProps) => (
  <div
    data-slot="avatar-group-count"
    {...props}
    class={cn(
      "size-8 rounded-full bg-muted text-sm text-muted-foreground group-has-data-[size=lg]/avatar-group:size-10 group-has-data-[size=sm]/avatar-group:size-6 [&>svg]:size-4 group-has-data-[size=lg]/avatar-group:[&>svg]:size-5 group-has-data-[size=sm]/avatar-group:[&>svg]:size-3 relative flex shrink-0 items-center justify-center ring-2 ring-background",
      props.class
    )}
  />
)
export {
  Avatar,
  AvatarBadge,
  AvatarFallback,
  AvatarGroup,
  AvatarGroupCount,
  AvatarImage
}

Update the import paths to match your project setup.

Usage

import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar"
<Avatar>
  <AvatarImage src="/avatars/03.png" alt="@alex" />
  <AvatarFallback>AM</AvatarFallback>
</Avatar>

Composition

Use the following composition to build an Avatar:

Avatar
โ”œโ”€โ”€ AvatarImage
โ”œโ”€โ”€ AvatarFallback
โ””โ”€โ”€ AvatarBadge

Use the following composition to build an AvatarGroup:

AvatarGroup
โ”œโ”€โ”€ Avatar
โ”‚   โ”œโ”€โ”€ AvatarImage
โ”‚   โ”œโ”€โ”€ AvatarFallback
โ”‚   โ””โ”€โ”€ AvatarBadge
โ”œโ”€โ”€ Avatar
โ”‚   โ”œโ”€โ”€ AvatarImage
โ”‚   โ”œโ”€โ”€ AvatarFallback
โ”‚   โ””โ”€โ”€ AvatarBadge
โ””โ”€โ”€ AvatarGroupCount

Basic

A basic avatar component with an image and a fallback.

AM
import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar"

export default function AvatarDemo() {
  return (
    <Avatar>
      <AvatarImage src="/avatars/03.png" alt="@alex" class="grayscale" />
      <AvatarFallback>AM</AvatarFallback>
    </Avatar>
  )
}

Badge

Use the AvatarBadge component to add a badge to the avatar. The badge is positioned at the bottom right of the avatar.

AM
import {
  Avatar,
  AvatarBadge,
  AvatarFallback,
  AvatarImage
} from "~/components/ui/avatar"

export default function AvatarWithBadge() {
  return (
    <Avatar>
      <AvatarImage src="/avatars/03.png" alt="@alex" />
      <AvatarFallback>AM</AvatarFallback>
      <AvatarBadge class="bg-green-600 dark:bg-green-800" />
    </Avatar>
  )
}

Use the class prop to customize badge colors and sizing.

<Avatar>
  <AvatarImage src="/avatars/03.png" alt="@alex" />
  <AvatarFallback>AM</AvatarFallback>
  <AvatarBadge class="bg-green-600 dark:bg-green-800" />
</Avatar>

Badge with Icon

You can also use an icon inside <AvatarBadge>.

PP
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.

import {
  Avatar,
  AvatarBadge,
  AvatarFallback,
  AvatarImage
} from "~/components/ui/avatar"

export default function AvatarBadgeIconExample() {
  return (
    <Avatar class="grayscale">
      <AvatarImage src="https://github.com/pranathip.png" alt="@pranathip" />
      <AvatarFallback>PP</AvatarFallback>
      <AvatarBadge>
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M5 12h14" />
          <path d="M12 5v14" />
        </svg>
      </AvatarBadge>
    </Avatar>
  )
}

Avatar Group

Use the AvatarGroup component to add a group of avatars.

AMLRER
import {
  Avatar,
  AvatarFallback,
  AvatarGroup,
  AvatarImage
} from "~/components/ui/avatar"

export default function AvatarGroupExample() {
  return (
    <AvatarGroup class="grayscale">
      <Avatar>
        <AvatarImage src="/avatars/03.png" alt="@alex" />
        <AvatarFallback>AM</AvatarFallback>
      </Avatar>
      <Avatar>
        <AvatarImage src="https://github.com/maxleiter.png" alt="@maxleiter" />
        <AvatarFallback>LR</AvatarFallback>
      </Avatar>
      <Avatar>
        <AvatarImage
          src="https://github.com/evilrabbit.png"
          alt="@evilrabbit"
        />
        <AvatarFallback>ER</AvatarFallback>
      </Avatar>
    </AvatarGroup>
  )
}

Avatar Group Count

Use <AvatarGroupCount> to add a count to the group.

AMLRER
+3
import {
  Avatar,
  AvatarFallback,
  AvatarGroup,
  AvatarGroupCount,
  AvatarImage
} from "~/components/ui/avatar"

export default function AvatarGroupCountExample() {
  return (
    <AvatarGroup class="grayscale">
      <Avatar>
        <AvatarImage src="/avatars/03.png" alt="@alex" />
        <AvatarFallback>AM</AvatarFallback>
      </Avatar>
      <Avatar>
        <AvatarImage src="https://github.com/maxleiter.png" alt="@maxleiter" />
        <AvatarFallback>LR</AvatarFallback>
      </Avatar>
      <Avatar>
        <AvatarImage
          src="https://github.com/evilrabbit.png"
          alt="@evilrabbit"
        />
        <AvatarFallback>ER</AvatarFallback>
      </Avatar>
      <AvatarGroupCount>+3</AvatarGroupCount>
    </AvatarGroup>
  )
}

Avatar Group with Icon

You can also use an icon inside <AvatarGroupCount>.

AMLRER
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.

import {
  Avatar,
  AvatarFallback,
  AvatarGroup,
  AvatarGroupCount,
  AvatarImage
} from "~/components/ui/avatar"

export default function AvatarGroupCountIconExample() {
  return (
    <AvatarGroup class="grayscale">
      <Avatar>
        <AvatarImage src="/avatars/03.png" alt="@alex" />
        <AvatarFallback>AM</AvatarFallback>
      </Avatar>
      <Avatar>
        <AvatarImage src="https://github.com/maxleiter.png" alt="@maxleiter" />
        <AvatarFallback>LR</AvatarFallback>
      </Avatar>
      <Avatar>
        <AvatarImage
          src="https://github.com/evilrabbit.png"
          alt="@evilrabbit"
        />
        <AvatarFallback>ER</AvatarFallback>
      </Avatar>
      <AvatarGroupCount>
        <svg
          width="24"
          height="24"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
        >
          <path d="M5 12h14" />
          <path d="M12 5v14" />
        </svg>
      </AvatarGroupCount>
    </AvatarGroup>
  )
}

Sizes

Use the size prop to change the size of the avatar.

AMAMAM
import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar"

export default function AvatarSizeExample() {
  return (
    <div class="flex flex-wrap items-center gap-2 grayscale">
      <Avatar size="sm">
        <AvatarImage src="/avatars/03.png" alt="@alex" />
        <AvatarFallback>AM</AvatarFallback>
      </Avatar>
      <Avatar>
        <AvatarImage src="/avatars/03.png" alt="@alex" />
        <AvatarFallback>AM</AvatarFallback>
      </Avatar>
      <Avatar size="lg">
        <AvatarImage src="/avatars/03.png" alt="@alex" />
        <AvatarFallback>AM</AvatarFallback>
      </Avatar>
    </div>
  )
}

You can use the Avatar component as a trigger for a dropdown menu.

import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar"
import { Button } from "~/components/ui/button"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuGroup,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger
} from "~/components/ui/dropdown-menu"

export default function AvatarDropdown() {
  return (
    <DropdownMenu>
      <DropdownMenuTrigger
        render={(partProps) => (
          <Button {...partProps} size="icon" variant="ghost" />
        )}
        class="rounded-full"
      >
        <Avatar>
          <AvatarImage
            alt="@stefan-karger"
            src="https://github.com/stefan-karger.png"
          />
          <AvatarFallback>SK</AvatarFallback>
        </Avatar>
      </DropdownMenuTrigger>
      <DropdownMenuContent class="w-32">
        <DropdownMenuGroup>
          <DropdownMenuItem>Profile</DropdownMenuItem>
          <DropdownMenuItem>Billing</DropdownMenuItem>
          <DropdownMenuItem>Settings</DropdownMenuItem>
        </DropdownMenuGroup>
        <DropdownMenuSeparator />
        <DropdownMenuGroup>
          <DropdownMenuItem variant="destructive">Log out</DropdownMenuItem>
        </DropdownMenuGroup>
      </DropdownMenuContent>
    </DropdownMenu>
  )
}

API Reference

Avatar

Root container for avatar image and fallback content.

PropTypeDefault
size"default" | "sm" | "lg""default"
classstring-

AvatarImage

Image element displayed when src loads successfully.

PropTypeDefault
srcstring-
altstring-
classstring-

AvatarFallback

Fallback element rendered when the image is unavailable.

PropTypeDefault
classstring-

AvatarBadge

Badge indicator positioned on top of the avatar.

PropTypeDefault
classstring-

AvatarGroup

Container for overlapping avatars.

PropTypeDefault
classstring-

AvatarGroupCount

Count indicator for additional avatars in a group.

PropTypeDefault
classstring-

See the UI SDK documentation for more information.