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

Avatar

원문 보기 위키 갱신

폴백(fallback)과 Nuxt Image 지원을 갖춘 img 요소 컴포넌트예요.

출처: Nuxt UI

본문

사용법

Avatar는 @nuxt/image가 설치되어 있으면 <NuxtImg> 컴포넌트를 사용하고, 그렇지 않으면 img로 대체해요.

<template>
  <UAvatar src="https://github.com/benjamincanac.png" />
</template>

[!NOTE]

HTML <img> 요소의 alt, loading 등 어떤 속성이든 전달할 수 있어요.

[!TIP]

@nuxt/image를 사용하지 않으려면 as prop을 사용하세요: :as="{ img: 'img' }".

Src

src prop으로 이미지 URL을 설정해 주세요.

<template>
  <UAvatar src="https://github.com/benjamincanac.png" loading="lazy" />
</template>

Size

size prop으로 Avatar의 크기를 설정해 주세요.

<template>
  <UAvatar src="https://github.com/benjamincanac.png" size="xl" loading="lazy" />
</template>

[!NOTE]

<img> 요소의 width와 height는 size prop에 따라 자동으로 설정돼요.

Icon

icon prop으로 폴백 Icon을 표시할 수 있어요.

<template>
  <UAvatar icon="i-lucide-image" size="md" />
</template>

Text

text prop으로 폴백 텍스트를 표시할 수 있어요.

<template>
  <UAvatar text="+1" size="md" />
</template>

Alt

아이콘이나 텍스트가 없으면 alt prop의 이니셜(initials) 이 폴백으로 사용돼요.

<template>
  <UAvatar alt="Benjamin Canac" size="md" />
</template>

[!NOTE]

alt prop은 img 요소의 alt 속성으로 전달돼요.

Color 4.8+

color prop으로 Avatar의 색상을 바꿀 수 있어요.

<template>
  <UAvatar color="primary" alt="Benjamin Canac" />
</template>

Chip

chip prop으로 Avatar 주위에 칩(chip)을 표시할 수 있어요.

<template>
  <UAvatar src="https://github.com/benjamincanac.png" loading="lazy" :chip="{
  inset: true
}" />
</template>

예시

툴팁과 함께 사용하기

Tooltip 컴포넌트를 사용하면 Avatar에 마우스를 올렸을 때 툴팁을 표시할 수 있어요.

<template>
  <UTooltip text="Benjamin Canac">
    <UAvatar
      src="https://github.com/benjamincanac.png"
      alt="Benjamin Canac"
      loading="lazy"
    />
  </UTooltip>
</template>

마스크와 함께 사용하기

CSS 마스크를 사용하면 단순한 원 대신 커스텀 모양의 Avatar를 표시할 수 있어요.

<template>
  <UAvatar class="rounded-none squircle" src="https://avatars.githubusercontent.com/u/739984?v=4" alt="Benjamin Canac" loading="lazy" />
</template>

<style>
.squircle {
  mask-image: url("data:image/svg+xml,%3csvg width='200' height='200' xmlns='http://www.w3.org/2000/svg'%3e%3cpath d='M100 0C20 0 0 20 0 100s20 100 100 100 100-20 100-100S180 0 100 0Z'/%3e%3c/svg%3e");
  mask-size: contain;
  mask-position: center;
  mask-repeat: no-repeat;
}
</style>

API

Props

/**
 * Props for the Avatar component
 */
interface AvatarProps {
  /**
   * The element or component this component should render as.
   * @default 'span'
   */
  as?: any;
  src?: string | undefined;
  alt?: string | undefined;
  icon?: any;
  text?: string | undefined;
  /**
   * @default 'md'
   */
  size?: "md" | "xs" | "sm" | "lg" | "xl" | "3xs" | "2xs" | "2xl" | "3xl" | undefined;
  /**
   * @default 'neutral'
   */
  color?: "error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral" | undefined;
  chip?: boolean | ChipProps | undefined;
  ui?: { root?: SlotClass; image?: SlotClass; fallback?: SlotClass; icon?: SlotClass; } | undefined;
  loading?: "lazy" | "eager" | undefined;
  referrerpolicy?: "" | "no-referrer" | "no-referrer-when-downgrade" | "origin" | "origin-when-cross-origin" | "same-origin" | "strict-origin" | "strict-origin-when-cross-origin" | "unsafe-url" | undefined;
  decoding?: "async" | "auto" | "sync" | undefined;
  height?: string | number | undefined;
  sizes?: string | undefined;
  srcset?: string | undefined;
  usemap?: string | undefined;
  width?: string | number | undefined;
  crossorigin?: "anonymous" | "use-credentials" | undefined;
}

[!NOTE] 참고: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attributes

이 컴포넌트는 네이티브 <img> HTML 속성도 모두 지원해요.

테마

export default defineAppConfig({
  ui: {
    avatar: {
      slots: {
        root: 'inline-flex items-center justify-center shrink-0 select-none rounded-full align-middle',
        image: 'h-full w-full rounded-[inherit] object-cover',
        fallback: 'font-medium truncate',
        icon: 'shrink-0'
      },
      variants: {
        color: {
          primary: {
            root: 'bg-primary/10',
            fallback: 'text-primary',
            icon: 'text-primary'
          },
          secondary: {
            root: 'bg-secondary/10',
            fallback: 'text-secondary',
            icon: 'text-secondary'
          },
          success: {
            root: 'bg-success/10',
            fallback: 'text-success',
            icon: 'text-success'
          },
          info: {
            root: 'bg-info/10',
            fallback: 'text-info',
            icon: 'text-info'
          },
          warning: {
            root: 'bg-warning/10',
            fallback: 'text-warning',
            icon: 'text-warning'
          },
          error: {
            root: 'bg-error/10',
            fallback: 'text-error',
            icon: 'text-error'
          },
          neutral: {
            root: 'bg-elevated',
            fallback: 'text-muted',
            icon: 'text-muted'
          }
        },
        size: {
          '3xs': {
            root: 'size-4 text-[8px]'
          },
          '2xs': {
            root: 'size-5 text-[10px]'
          },
          xs: {
            root: 'size-6 text-xs'
          },
          sm: {
            root: 'size-7 text-sm'
          },
          md: {
            root: 'size-8 text-base'
          },
          lg: {
            root: 'size-9 text-lg'
          },
          xl: {
            root: 'size-10 text-xl'
          },
          '2xl': {
            root: 'size-11 text-[22px]'
          },
          '3xl': {
            root: 'size-12 text-2xl'
          }
        }
      },
      defaultVariants: {
        size: 'md',
        color: 'neutral'
      }
    }
  }
})

변경 기록

컴포넌트와 테마의 커밋 기록을 확인해 주세요.


사이트맵

전체 페이지 목록은 사이트맵에서 확인할 수 있어요.

더 알아보기 (Learn more)