Badge
用于表示状态或类别的短文本。
用法
使用默认插槽来设置 Badge 的标签内容。
Badge
<template>
<UBadge>Badge</UBadge>
</template>
标签
使用 label 属性来设置 Badge 的标签文本。
Badge
<template>
<UBadge label="Badge" />
</template>
颜色
使用 color 属性来更改 Badge 的颜色。
Badge
<template>
<UBadge color="neutral">Badge</UBadge>
</template>
变体
使用 variant 属性来更改 Badge 的变体样式。
Badge
<template>
<UBadge color="neutral" variant="outline">Badge</UBadge>
</template>
尺寸
使用 size 属性来更改 Badge 的尺寸。
Badge
<template>
<UBadge size="xl">Badge</UBadge>
</template>
Icon
使用 icon 属性在 Badge 内显示一个 图标 (Icon)。
Badge
<template>
<UBadge icon="i-lucide-rocket" size="md" color="primary" variant="solid">Badge</UBadge>
</template>
使用 leading 和 trailing 属性设置图标位置,或者使用 leading-icon 和 trailing-icon 属性为每个位置设置不同的图标。
Badge
<template>
<UBadge trailing-icon="i-lucide-arrow-right" size="md">Badge</UBadge>
</template>
Avatar
使用 avatar 属性在 Badge 内显示一个 头像 (Avatar)。
<template>
<UBadge
:avatar="{
src: 'https://github.com/nuxt.png',
loading: 'lazy'
}"
size="md"
color="neutral"
variant="outline"
>
Badge
</UBadge>
</template>
示例
class 属性
使用 class 属性来覆盖 Badge 的基础样式。
Badge
<template>
<UBadge class="font-bold rounded-full">Badge</UBadge>
</template>
API
属性
| 属性 | 默认值 | 类型 |
|---|---|---|
as | 'span' | any此组件应渲染为的元素或组件。 |
label | string | number | |
color | 'primary' | "error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral" |
variant | 'solid' | "solid" | "outline" | "soft" | "subtle" |
尺寸 | 'md' | "xs" | "sm" | "md" | "lg" | "xl" |
正方形 | boolean以各边等距填充渲染徽章。 | |
图标 | any根据 | |
avatar | AvatarProps在左侧显示头像。 | |
前置 | boolean当为 | |
leadingIcon | any在左侧显示图标。 | |
尾部 | boolean当为 | |
trailingIcon | any在右侧显示图标。 | |
ui | { base?: ClassNameValue; label?: ClassNameValue; leadingIcon?: ClassNameValue; leadingAvatar?: ClassNameValue; leadingAvatarSize?: ClassNameValue; trailingIcon?: ClassNameValue; } |
插槽
| 插槽 | 类型 |
|---|---|
前置 | { ui: object; } |
default | { ui: object; } |
尾部 | { ui: object; } |
主题
app.config.ts
export default defineAppConfig({
ui: {
badge: {
slots: {
base: 'font-medium inline-flex items-center',
label: 'truncate',
leadingIcon: 'shrink-0',
leadingAvatar: 'shrink-0',
leadingAvatarSize: '',
trailingIcon: 'shrink-0'
},
variants: {
fieldGroup: {
horizontal: 'not-only:first:rounded-e-none not-only:last:rounded-s-none not-last:not-first:rounded-none focus-visible:z-[1]',
vertical: 'not-only:first:rounded-b-none not-only:last:rounded-t-none not-last:not-first:rounded-none focus-visible:z-[1]'
},
color: {
primary: '',
secondary: '',
success: '',
info: '',
warning: '',
error: '',
neutral: ''
},
variant: {
solid: '',
outline: '',
soft: '',
subtle: ''
},
size: {
xs: {
base: 'text-[8px]/3 px-1 py-0.5 gap-1 rounded-sm',
leadingIcon: 'size-3',
leadingAvatarSize: '3xs',
trailingIcon: 'size-3'
},
sm: {
base: 'text-[10px]/3 px-1.5 py-1 gap-1 rounded-sm',
leadingIcon: 'size-3',
leadingAvatarSize: '3xs',
trailingIcon: 'size-3'
},
md: {
base: 'text-xs px-2 py-1 gap-1 rounded-md',
leadingIcon: 'size-4',
leadingAvatarSize: '3xs',
trailingIcon: 'size-4'
},
lg: {
base: 'text-sm px-2 py-1 gap-1.5 rounded-md',
leadingIcon: 'size-5',
leadingAvatarSize: '2xs',
trailingIcon: 'size-5'
},
xl: {
base: 'text-base px-2.5 py-1 gap-1.5 rounded-md',
leadingIcon: 'size-6',
leadingAvatarSize: '2xs',
trailingIcon: 'size-6'
}
},
square: {
true: ''
}
},
compoundVariants: [
{
color: 'primary',
variant: 'solid',
class: 'bg-primary text-inverted'
},
{
color: 'primary',
variant: 'outline',
class: 'text-primary ring ring-inset ring-primary/50'
},
{
color: 'primary',
variant: 'soft',
class: 'bg-primary/10 text-primary'
},
{
color: 'primary',
variant: 'subtle',
class: 'bg-primary/10 text-primary ring ring-inset ring-primary/25'
},
{
color: 'neutral',
variant: 'solid',
class: 'text-inverted bg-inverted'
},
{
color: 'neutral',
variant: 'outline',
class: 'ring ring-inset ring-accented text-default bg-default'
},
{
color: 'neutral',
variant: 'soft',
class: 'text-default bg-elevated'
},
{
color: 'neutral',
variant: 'subtle',
class: 'ring ring-inset ring-accented text-default bg-elevated'
},
{
size: 'xs',
square: true,
class: 'p-0.5'
},
{
size: 'sm',
square: true,
class: 'p-1'
},
{
size: 'md',
square: true,
class: 'p-1'
},
{
size: 'lg',
square: true,
class: 'p-1'
},
{
size: 'xl',
square: true,
class: 'p-1'
}
],
defaultVariants: {
color: 'primary',
variant: 'solid',
size: 'md'
}
}
}
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
badge: {
slots: {
base: 'font-medium inline-flex items-center',
label: 'truncate',
leadingIcon: 'shrink-0',
leadingAvatar: 'shrink-0',
leadingAvatarSize: '',
trailingIcon: 'shrink-0'
},
variants: {
fieldGroup: {
horizontal: 'not-only:first:rounded-e-none not-only:last:rounded-s-none not-last:not-first:rounded-none focus-visible:z-[1]',
vertical: 'not-only:first:rounded-b-none not-only:last:rounded-t-none not-last:not-first:rounded-none focus-visible:z-[1]'
},
color: {
primary: '',
secondary: '',
success: '',
info: '',
warning: '',
error: '',
neutral: ''
},
variant: {
solid: '',
outline: '',
soft: '',
subtle: ''
},
size: {
xs: {
base: 'text-[8px]/3 px-1 py-0.5 gap-1 rounded-sm',
leadingIcon: 'size-3',
leadingAvatarSize: '3xs',
trailingIcon: 'size-3'
},
sm: {
base: 'text-[10px]/3 px-1.5 py-1 gap-1 rounded-sm',
leadingIcon: 'size-3',
leadingAvatarSize: '3xs',
trailingIcon: 'size-3'
},
md: {
base: 'text-xs px-2 py-1 gap-1 rounded-md',
leadingIcon: 'size-4',
leadingAvatarSize: '3xs',
trailingIcon: 'size-4'
},
lg: {
base: 'text-sm px-2 py-1 gap-1.5 rounded-md',
leadingIcon: 'size-5',
leadingAvatarSize: '2xs',
trailingIcon: 'size-5'
},
xl: {
base: 'text-base px-2.5 py-1 gap-1.5 rounded-md',
leadingIcon: 'size-6',
leadingAvatarSize: '2xs',
trailingIcon: 'size-6'
}
},
square: {
true: ''
}
},
compoundVariants: [
{
color: 'primary',
variant: 'solid',
class: 'bg-primary text-inverted'
},
{
color: 'primary',
variant: 'outline',
class: 'text-primary ring ring-inset ring-primary/50'
},
{
color: 'primary',
variant: 'soft',
class: 'bg-primary/10 text-primary'
},
{
color: 'primary',
variant: 'subtle',
class: 'bg-primary/10 text-primary ring ring-inset ring-primary/25'
},
{
color: 'neutral',
variant: 'solid',
class: 'text-inverted bg-inverted'
},
{
color: 'neutral',
variant: 'outline',
class: 'ring ring-inset ring-accented text-default bg-default'
},
{
color: 'neutral',
variant: 'soft',
class: 'text-default bg-elevated'
},
{
color: 'neutral',
variant: 'subtle',
class: 'ring ring-inset ring-accented text-default bg-elevated'
},
{
size: 'xs',
square: true,
class: 'p-0.5'
},
{
size: 'sm',
square: true,
class: 'p-1'
},
{
size: 'md',
square: true,
class: 'p-1'
},
{
size: 'lg',
square: true,
class: 'p-1'
},
{
size: 'xl',
square: true,
class: 'p-1'
}
],
defaultVariants: {
color: 'primary',
variant: 'solid',
size: 'md'
}
}
}
})
]
})
更新日志
暂无近期更新