Avatar
用法
当安装了<NuxtImg>
组件时,头像使用该组件,@nuxt/image
否则回退到img
。

<template>
<UAvatar src="https://github.com/benjamincanac.png" />
</template>
<img>
元素的任何属性,例如 alt
、loading
等。Src
使用 src
属性设置图片 URL。

<template>
<UAvatar src="https://github.com/benjamincanac.png" />
</template>
尺寸
使用 size
属性设置头像的大小。

<template>
<UAvatar src="https://github.com/benjamincanac.png" size="xl" />
</template>
<img>
元素的 width
和 height
会根据 size
属性自动设置。Icon
使用 icon
属性显示备用图标。
<template>
<UAvatar icon="i-lucide-image" size="md" />
</template>
文本
使用 text
属性显示备用文本。
<template>
<UAvatar text="+1" size="md" />
</template>
Alt
当未提供图标或文本时,alt
属性的首字母将用作备用。
<template>
<UAvatar alt="Benjamin Canac" size="md" />
</template>
alt
属性作为 alt
属性传递给 img
元素。Chip
使用 chip
属性在头像周围显示一个徽章。

<template>
<UAvatar
src="https://github.com/benjamincanac.png"
:chip="{
inset: true
}"
/>
</template>
示例
带工具提示
您可以使用提示组件在悬停头像时显示提示。

<template>
<UTooltip text="Benjamin Canac">
<UAvatar
src="https://github.com/benjamincanac.png"
alt="Benjamin Canac"
/>
</UTooltip>
</template>
带遮罩
您可以使用 CSS 遮罩来显示具有自定义形状而不是简单圆形的头像。
<template>
<UAvatar class="rounded-none squircle" src="https://avatars.githubusercontent.com/u/739984?v=4" alt="Benjamin Canac" />
</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
属性
属性 | 默认值 | 类型 |
---|---|---|
as |
|
此组件应渲染为的元素或组件。 |
src |
| |
alt |
| |
图标 |
| |
文本 |
| |
尺寸 |
|
|
chip |
| |
ui |
|
主题
export default defineAppConfig({
ui: {
avatar: {
slots: {
root: 'inline-flex items-center justify-center shrink-0 select-none rounded-full align-middle bg-elevated',
image: 'h-full w-full rounded-[inherit] object-cover',
fallback: 'font-medium leading-none text-muted truncate',
icon: 'text-muted shrink-0'
},
variants: {
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'
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
avatar: {
slots: {
root: 'inline-flex items-center justify-center shrink-0 select-none rounded-full align-middle bg-elevated',
image: 'h-full w-full rounded-[inherit] object-cover',
fallback: 'font-medium leading-none text-muted truncate',
icon: 'text-muted shrink-0'
},
variants: {
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'
}
}
}
})
]
})