Search for a command to run...
An image element with a fallback for representing the user.
npx shadcn@latest add @zaidan/avatarpnpx shadcn add @zaidan/avataryarn dlx shadcn@latest add @zaidan/avatarbunx shadcn@latest add @zaidan/avatarCopy and paste the following code into your project.
1import * as ImagePrimitive from "@kobalte/core/image";2import type { PolymorphicProps } from "@kobalte/core/polymorphic";3import type { ComponentProps, ValidComponent } from "solid-js";4import { mergeProps, splitProps } from "solid-js";5
6import { cn } from "~/lib/utils";7
8type AvatarRootProps<T extends ValidComponent = "span"> = PolymorphicProps<9 T,10 ImagePrimitive.ImageRootProps<T>11> &12 Pick<ComponentProps<T>, "class"> & {13 size?: "sm" | "default" | "lg";14 };15
16const Avatar = <T extends ValidComponent = "span">(props: AvatarRootProps<T>) => {17 const mergedProps = mergeProps({ size: "default" }, props);18 const [local, others] = splitProps(mergedProps as AvatarRootProps, ["class", "size"]);19 return (20 <ImagePrimitive.Root21 class={cn(22 "group/avatar relative z-avatar flex shrink-0 select-none after:absolute after:inset-0 after:border after:border-border after:mix-blend-darken dark:after:mix-blend-lighten",23 local.class,24 )}25 data-size={local.size}26 data-slot="avatar"27 {...others}28 />29 );30};31
32type AvatarImageProps<T extends ValidComponent = "img"> = PolymorphicProps<33 T,34 ImagePrimitive.ImageImgProps<T>35> &36 Pick<ComponentProps<T>, "class">;37
38const AvatarImage = <T extends ValidComponent = "img">(props: AvatarImageProps<T>) => {39 const [local, others] = splitProps(props as AvatarImageProps, ["class"]);40 return (41 <ImagePrimitive.Img42 class={cn("z-avatar-image aspect-square size-full object-cover", local.class)}43 data-slot="avatar-image"44 {...others}45 />46 );47};48
49type AvatarFallbackProps<T extends ValidComponent = "span"> = PolymorphicProps<50 T,51 ImagePrimitive.ImageFallbackProps<T>52> &53 Pick<ComponentProps<T>, "class">;54
55const AvatarFallback = <T extends ValidComponent = "span">(props: AvatarFallbackProps<T>) => {56 const [local, others] = splitProps(props as AvatarFallbackProps, ["class"]);57 return (58 <ImagePrimitive.Fallback59 class={cn(60 "z-avatar-fallback flex size-full items-center justify-center text-sm group-data-[size=sm]/avatar:text-xs",61 local.class,62 )}63 data-slot="avatar-fallback"64 {...others}65 />66 );67};68
69type AvatarBadgeProps = ComponentProps<"span">;70
71function AvatarBadge(props: AvatarBadgeProps) {72 const [local, others] = splitProps(props as AvatarBadgeProps, ["class"]);73 return (74 <span75 data-slot="avatar-badge"76 class={cn(77 "absolute right-0 bottom-0 z-10 z-avatar-badge inline-flex select-none items-center justify-center rounded-full bg-blend-color ring-2",78 "group-data-[size=sm]/avatar:size-2 group-data-[size=sm]/avatar:[&>svg]:hidden",79 "group-data-[size=default]/avatar:size-2.5 group-data-[size=default]/avatar:[&>svg]:size-2",80 "group-data-[size=lg]/avatar:size-3 group-data-[size=lg]/avatar:[&>svg]:size-2",81 local.class,82 )}83 {...others}84 />85 );86}87
88type AvatarGroupProps = ComponentProps<"div">;89
90function AvatarGroup(props: AvatarGroupProps) {91 const [local, others] = splitProps(props as AvatarGroupProps, ["class"]);92 return (93 <div94 data-slot="avatar-group"95 class={cn(96 "group/avatar-group z-avatar-group flex -space-x-2 *:data-[slot=avatar]:ring-2 *:data-[slot=avatar]:ring-background",97 local.class,98 )}99 {...others}100 />101 );102}103
104type AvatarGroupCountProps = ComponentProps<"div">;105
106function AvatarGroupCount(props: AvatarGroupCountProps) {107 const [local, others] = splitProps(props as AvatarGroupCountProps, ["class"]);108 return (109 <div110 data-slot="avatar-group-count"111 class={cn(112 "relative z-avatar-group-count flex shrink-0 items-center justify-center ring-2 ring-background",113 "",114 local.class,115 )}116 {...others}117 />118 );119}120
121export { Avatar, AvatarBadge, AvatarFallback, AvatarGroup, AvatarGroupCount, AvatarImage };Here are the source code of all the examples from the preview page:
import { Avatar, AvatarFallback, AvatarImage,} from "~/components/ui/avatar";function AvatarSizes() { return ( <Example title="Sizes"> <div class="flex flex-wrap items-center gap-2"> <Avatar size="sm"> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar size="lg"> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <AvatarFallback>CN</AvatarFallback> </Avatar> </div> <div class="flex flex-wrap items-center gap-2"> <Avatar size="sm"> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar size="lg"> <AvatarFallback>CN</AvatarFallback> </Avatar> </div> </Example> );}import { Avatar, AvatarBadge, AvatarFallback, AvatarImage,} from "~/components/ui/avatar";function AvatarWithBadge() { return ( <Example title="Badge"> <div class="flex flex-wrap items-center gap-2"> <Avatar size="sm"> <AvatarImage src="https://github.com/jorgezreik.png" alt="@jorgezreik" /> <AvatarFallback>JZ</AvatarFallback> <AvatarBadge /> </Avatar> <Avatar> <AvatarImage src="https://github.com/jorgezreik.png" alt="@jorgezreik" /> <AvatarFallback>JZ</AvatarFallback> <AvatarBadge /> </Avatar> <Avatar size="lg"> <AvatarImage src="https://github.com/jorgezreik.png" alt="@jorgezreik" /> <AvatarFallback>JZ</AvatarFallback> <AvatarBadge /> </Avatar> </div> <div class="flex flex-wrap items-center gap-2"> <Avatar size="sm"> <AvatarFallback>JZ</AvatarFallback> <AvatarBadge /> </Avatar> <Avatar> <AvatarFallback>JZ</AvatarFallback> <AvatarBadge /> </Avatar> <Avatar size="lg"> <AvatarFallback>JZ</AvatarFallback> <AvatarBadge /> </Avatar> </div> </Example> );}import { Check, Plus } from "lucide-solid";import { Avatar, AvatarBadge, AvatarFallback, AvatarImage,} from "~/components/ui/avatar";function AvatarWithBadgeIcon() { return ( <Example title="Badge with Icon"> <div class="flex flex-wrap items-center gap-2"> <Avatar size="sm"> <AvatarImage src="https://github.com/pranathip.png" alt="@pranathip" /> <AvatarFallback>PP</AvatarFallback> <AvatarBadge> <Plus /> </AvatarBadge> </Avatar> <Avatar> <AvatarImage src="https://github.com/pranathip.png" alt="@pranathip" /> <AvatarFallback>PP</AvatarFallback> <AvatarBadge> <Plus /> </AvatarBadge> </Avatar> <Avatar size="lg"> <AvatarImage src="https://github.com/pranathip.png" alt="@pranathip" /> <AvatarFallback>PP</AvatarFallback> <AvatarBadge> <Plus /> </AvatarBadge> </Avatar> </div> <div class="flex flex-wrap items-center gap-2"> <Avatar size="sm"> <AvatarFallback>PP</AvatarFallback> <AvatarBadge> <Check /> </AvatarBadge> </Avatar> <Avatar> <AvatarFallback>PP</AvatarFallback> <AvatarBadge> <Check /> </AvatarBadge> </Avatar> <Avatar size="lg"> <AvatarFallback>PP</AvatarFallback> <AvatarBadge> <Check /> </AvatarBadge> </Avatar> </div> </Example> );}import { Avatar, AvatarFallback, AvatarGroup, AvatarImage,} from "~/components/ui/avatar";function AvatarGroupExample() { return ( <Example title="Group"> <AvatarGroup> <Avatar size="sm"> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar size="sm"> <AvatarImage src="https://github.com/maxleiter.png" alt="@maxleiter" /> <AvatarFallback>LR</AvatarFallback> </Avatar> <Avatar size="sm"> <AvatarImage src="https://github.com/evilrabbit.png" alt="@evilrabbit" /> <AvatarFallback>ER</AvatarFallback> </Avatar> </AvatarGroup> <AvatarGroup> <Avatar> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <AvatarFallback>CN</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> <AvatarGroup> <Avatar size="lg"> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar size="lg"> <AvatarImage src="https://github.com/maxleiter.png" alt="@maxleiter" /> <AvatarFallback>LR</AvatarFallback> </Avatar> <Avatar size="lg"> <AvatarImage src="https://github.com/evilrabbit.png" alt="@evilrabbit" /> <AvatarFallback>ER</AvatarFallback> </Avatar> </AvatarGroup> </Example> );}import { Avatar, AvatarFallback, AvatarGroup, AvatarGroupCount, AvatarImage,} from "~/components/ui/avatar";function AvatarGroupWithCount() { return ( <Example title="Group with Count"> <AvatarGroup> <Avatar size="sm"> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar size="sm"> <AvatarImage src="https://github.com/maxleiter.png" alt="@maxleiter" /> <AvatarFallback>LR</AvatarFallback> </Avatar> <Avatar size="sm"> <AvatarImage src="https://github.com/evilrabbit.png" alt="@evilrabbit" /> <AvatarFallback>ER</AvatarFallback> </Avatar> <AvatarGroupCount>+3</AvatarGroupCount> </AvatarGroup> <AvatarGroup> <Avatar> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <AvatarFallback>CN</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> <AvatarGroup> <Avatar size="lg"> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar size="lg"> <AvatarImage src="https://github.com/maxleiter.png" alt="@maxleiter" /> <AvatarFallback>LR</AvatarFallback> </Avatar> <Avatar size="lg"> <AvatarImage src="https://github.com/evilrabbit.png" alt="@evilrabbit" /> <AvatarFallback>ER</AvatarFallback> </Avatar> <AvatarGroupCount>+3</AvatarGroupCount> </AvatarGroup> </Example> );}import { Plus } from "lucide-solid";import { Avatar, AvatarFallback, AvatarGroup, AvatarGroupCount, AvatarImage,} from "~/components/ui/avatar";function AvatarGroupWithIconCount() { return ( <Example title="Group with Icon Count"> <AvatarGroup> <Avatar size="sm"> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar size="sm"> <AvatarImage src="https://github.com/maxleiter.png" alt="@maxleiter" /> <AvatarFallback>LR</AvatarFallback> </Avatar> <Avatar size="sm"> <AvatarImage src="https://github.com/evilrabbit.png" alt="@evilrabbit" /> <AvatarFallback>ER</AvatarFallback> </Avatar> <AvatarGroupCount> <Plus /> </AvatarGroupCount> </AvatarGroup> <AvatarGroup> <Avatar> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <AvatarFallback>CN</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> <Plus /> </AvatarGroupCount> </AvatarGroup> <AvatarGroup> <Avatar size="lg"> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" class="grayscale" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar size="lg"> <AvatarImage src="https://github.com/maxleiter.png" alt="@maxleiter" class="grayscale" /> <AvatarFallback>LR</AvatarFallback> </Avatar> <Avatar size="lg"> <AvatarImage src="https://github.com/evilrabbit.png" alt="@evilrabbit" class="grayscale" /> <AvatarFallback>ER</AvatarFallback> </Avatar> <AvatarGroupCount> <Plus /> </AvatarGroupCount> </AvatarGroup> </Example> );}import { Plus } from "lucide-solid";import { Avatar, AvatarFallback, AvatarGroup, AvatarGroupCount, AvatarImage,} from "~/components/ui/avatar";import { Button } from "~/components/ui/button";import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle,} from "~/components/ui/empty";function AvatarInEmpty() { return ( <Example title="In Empty"> <Empty class="w-full flex-none border"> <EmptyHeader> <EmptyMedia> <AvatarGroup> <Avatar size="lg"> <AvatarImage src="https://github.com/shadcn.png" alt="@shadcn" class="grayscale" /> <AvatarFallback>CN</AvatarFallback> </Avatar> <Avatar size="lg"> <AvatarImage src="https://github.com/maxleiter.png" alt="@maxleiter" class="grayscale" /> <AvatarFallback>LR</AvatarFallback> </Avatar> <Avatar size="lg"> <AvatarImage src="https://github.com/evilrabbit.png" alt="@evilrabbit" class="grayscale" /> <AvatarFallback>ER</AvatarFallback> </Avatar> <AvatarGroupCount> <Plus /> </AvatarGroupCount> </AvatarGroup> </EmptyMedia> <EmptyTitle>No Team Members</EmptyTitle> <EmptyDescription>Invite your team to collaborate on this project.</EmptyDescription> </EmptyHeader> <EmptyContent> <Button> <Plus /> Invite Members </Button> </EmptyContent> </Empty> </Example> );}