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를 사용하지 않으려면asprop을 사용하세요::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는sizeprop에 따라 자동으로 설정돼요.
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]
altprop은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'
}
}
}
})
변경 기록
사이트맵
전체 페이지 목록은 사이트맵에서 확인할 수 있어요.