본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

아바타 (Avatar)

원문 보기 위키 갱신

사용자를 나타내기 위한 폴백(fallback)이 있는 이미지 엘리먼트예요. 이미지가 없거나 로딩에 실패하면 이니셜 같은 대체 콘텐츠가 대신 보여요.

출처: 문서

본문

<script lang="ts">
  import * as Avatar from "$lib/components/ui/avatar/index.js";
</script>
<div class="flex flex-row flex-wrap items-center gap-6 md:gap-12">
  <Avatar.Root>
    <Avatar.Image
      src="https://github.com/shadcn.png"
      alt="@shadcn"
      class="grayscale"
    />
    <Avatar.Fallback>CN</Avatar.Fallback>
  </Avatar.Root>
  <Avatar.Root>
    <Avatar.Image src="https://github.com/evilrabbit.png" alt="@evilrabbit" />
    <Avatar.Fallback>ER</Avatar.Fallback>
    <Avatar.Badge class="bg-green-600 dark:bg-green-800" />
  </Avatar.Root>
  <Avatar.Group class="grayscale">
    <Avatar.Root>
      <Avatar.Image src="https://github.com/shadcn.png" alt="@shadcn" />
      <Avatar.Fallback>CN</Avatar.Fallback>
    </Avatar.Root>
    <Avatar.Root>
      <Avatar.Image src="https://github.com/maxleiter.png" alt="@maxleiter" />
      <Avatar.Fallback>LR</Avatar.Fallback>
    </Avatar.Root>
    <Avatar.Root>
      <Avatar.Image src="https://github.com/evilrabbit.png" alt="@evilrabbit" />
      <Avatar.Fallback>ER</Avatar.Fallback>
    </Avatar.Root>
    <Avatar.GroupCount>+3</Avatar.GroupCount>
  </Avatar.Group>
</div>

View Code

설치

pnpm dlx shadcn-svelte@latest add avatar
npx shadcn-svelte@latest add avatar
bun x shadcn-svelte@latest add avatar

사용법

<script lang="ts">
  import * as Avatar from "$lib/components/ui/avatar/index.js";
</script>
<Avatar.Root>
  <Avatar.Image src="https://github.com/shadcn.png" alt="@shadcn" />
  <Avatar.Fallback>CN</Avatar.Fallback>
</Avatar.Root>

기본형

이미지와 폴백을 함께 갖춘 기본 아바타 컴포넌트예요.

<script lang="ts">
  import * as Avatar from "$lib/components/ui/avatar/index.js";
</script>
<Avatar.Root>
  <Avatar.Image
    src="https://github.com/shadcn.png"
    alt="@shadcn"
    class="grayscale"
  />
  <Avatar.Fallback>CN</Avatar.Fallback>
</Avatar.Root>

View Code

배지 (Badge)

Avatar.Badge 컴포넌트를 사용하면 아바타에 배지를 추가할 수 있어요. 배지는 아바타의 오른쪽 아래에 배치돼요.

<script lang="ts">
  import * as Avatar from "$lib/components/ui/avatar/index.js";
</script>
<Avatar.Root>
  <Avatar.Image src="https://github.com/shadcn.png" alt="@shadcn" />
  <Avatar.Fallback>CN</Avatar.Fallback>
  <Avatar.Badge class="bg-green-600 dark:bg-green-800" />
</Avatar.Root>

View Code

class prop을 사용하면 배지에 커스텀 색상, 크기 같은 스타일을 더할 수 있어요.

<Avatar.Root>
  <Avatar.Image src="https://github.com/shadcn.png" alt="@shadcn" />
  <Avatar.Fallback>CN</Avatar.Fallback>
  <Avatar.Badge class="bg-green-600 dark:bg-green-800" />
</Avatar.Root>

아이콘 배지

<Avatar.Badge> 안에 아이콘을 넣을 수도 있어요.

<script lang="ts">
  import PlusIcon from "@lucide/svelte/icons/plus";
  import * as Avatar from "$lib/components/ui/avatar/index.js";
</script>
<Avatar.Root class="grayscale">
  <Avatar.Image src="https://github.com/pranathip.png" alt="@pranathip" />
  <Avatar.Fallback>PP</Avatar.Fallback>
  <Avatar.Badge>
    <PlusIcon />
  </Avatar.Badge>
</Avatar.Root>

View Code

아바타 그룹

Avatar.Group 컴포넌트를 사용하면 여러 아바타를 하나의 그룹으로 묶을 수 있어요.

<script lang="ts">
  import * as Avatar from "$lib/components/ui/avatar/index.js";
</script>
<Avatar.Group class="grayscale">
  <Avatar.Root>
    <Avatar.Image src="https://github.com/shadcn.png" alt="@shadcn" />
    <Avatar.Fallback>CN</Avatar.Fallback>
  </Avatar.Root>
  <Avatar.Root>
    <Avatar.Image src="https://github.com/maxleiter.png" alt="@maxleiter" />
    <Avatar.Fallback>LR</Avatar.Fallback>
  </Avatar.Root>
  <Avatar.Root>
    <Avatar.Image src="https://github.com/evilrabbit.png" alt="@evilrabbit" />
    <Avatar.Fallback>ER</Avatar.Fallback>
  </Avatar.Root>
</Avatar.Group>

View Code

아바타 그룹 카운트

<Avatar.GroupCount>를 사용하면 그룹에 남은 인원 수 같은 카운트를 표시할 수 있어요.

<script lang="ts">
  import * as Avatar from "$lib/components/ui/avatar/index.js";
</script>
<Avatar.Group class="grayscale">
  <Avatar.Root>
    <Avatar.Image src="https://github.com/shadcn.png" alt="@shadcn" />
    <Avatar.Fallback>CN</Avatar.Fallback>
  </Avatar.Root>
  <Avatar.Root>
    <Avatar.Image src="https://github.com/maxleiter.png" alt="@maxleiter" />
    <Avatar.Fallback>LR</Avatar.Fallback>
  </Avatar.Root>
  <Avatar.Root>
    <Avatar.Image src="https://github.com/evilrabbit.png" alt="@evilrabbit" />
    <Avatar.Fallback>ER</Avatar.Fallback>
  </Avatar.Root>
  <Avatar.GroupCount>+3</Avatar.GroupCount>
</Avatar.Group>

View Code

아이콘 있는 그룹 카운트

<Avatar.GroupCount> 안에 아이콘을 넣을 수도 있어요.

<script lang="ts">
  import PlusIcon from "@lucide/svelte/icons/plus";
  import * as Avatar from "$lib/components/ui/avatar/index.js";
</script>
<Avatar.Group class="grayscale">
  <Avatar.Root>
    <Avatar.Image src="https://github.com/shadcn.png" alt="@shadcn" />
    <Avatar.Fallback>CN</Avatar.Fallback>
  </Avatar.Root>
  <Avatar.Root>
    <Avatar.Image src="https://github.com/maxleiter.png" alt="@maxleiter" />
    <Avatar.Fallback>LR</Avatar.Fallback>
  </Avatar.Root>
  <Avatar.Root>
    <Avatar.Image src="https://github.com/evilrabbit.png" alt="@evilrabbit" />
    <Avatar.Fallback>ER</Avatar.Fallback>
  </Avatar.Root>
  <Avatar.GroupCount>
    <PlusIcon />
  </Avatar.GroupCount>
</Avatar.Group>

View Code

크기

size prop을 사용하면 아바타의 크기를 바꿀 수 있어요.

<script lang="ts">
  import * as Avatar from "$lib/components/ui/avatar/index.js";
</script>
<div class="flex flex-wrap items-center gap-2 grayscale">
  <Avatar.Root size="sm">
    <Avatar.Image src="https://github.com/shadcn.png" alt="@shadcn" />
    <Avatar.Fallback>CN</Avatar.Fallback>
  </Avatar.Root>
  <Avatar.Root>
    <Avatar.Image src="https://github.com/shadcn.png" alt="@shadcn" />
    <Avatar.Fallback>CN</Avatar.Fallback>
  </Avatar.Root>
  <Avatar.Root size="lg">
    <Avatar.Image src="https://github.com/shadcn.png" alt="@shadcn" />
    <Avatar.Fallback>CN</Avatar.Fallback>
  </Avatar.Root>
</div>

View Code

드롭다운과 조합

Avatar 컴포넌트를 드롭다운 메뉴의 트리거로 사용할 수도 있어요.

<script lang="ts">
  import * as Avatar from "$lib/components/ui/avatar/index.js";
  import * as DropdownMenu from "$lib/components/ui/dropdown-menu/index.js";
  import { Button } from "$lib/components/ui/button/index.js";
</script>
<DropdownMenu.Root>
  <DropdownMenu.Trigger>
    {#snippet child({ props })}
      <Button {...props} variant="ghost" size="icon" class="rounded-full">
        <Avatar.Root>
          <Avatar.Image src="https://github.com/shadcn.png" alt="shadcn" />
          <Avatar.Fallback>CN</Avatar.Fallback>
        </Avatar.Root>
      </Button>
    {/snippet}
  </DropdownMenu.Trigger>
  <DropdownMenu.Content class="w-32">
    <DropdownMenu.Group>
      <DropdownMenu.Item>Profile</DropdownMenu.Item>
      <DropdownMenu.Item>Billing</DropdownMenu.Item>
      <DropdownMenu.Item>Settings</DropdownMenu.Item>
    </DropdownMenu.Group>
    <DropdownMenu.Separator />
    <DropdownMenu.Group>
      <DropdownMenu.Item variant="destructive">Log out</DropdownMenu.Item>
    </DropdownMenu.Group>
  </DropdownMenu.Content>
</DropdownMenu.Root>

View Code

더 알아보기 (Learn more)

원문 문서에서는 bits-ui의 Avatar 문서와 API 레퍼런스 링크를 통해 컴포넌트의 동작 원리와 전체 prop 목록을 더 자세히 살펴볼 수 있어요. 설치 방법이나 다른 컴포넌트와의 조합이 궁금하면 shadcn-svelte 공식 문서를 참고해 보세요.