Badge 배지
상태나 카테고리를 나타내는 짧은 텍스트를 표시하는 컴포넌트예요. 기본 슬롯이나 label prop을 이용해 내용을 넣을 수 있고, 색상·변형·크기·아이콘·아바타까지 다양하게 꾸밀 수 있어요.
출처: Nuxt UI
본문
상태나 카테고리를 나타내는 짧은 텍스트예요.
사용법 (Usage)
기본 슬롯을 이용해 배지의 레이블을 설정해요.
<template>
<UBadge>
Badge
</UBadge>
</template>
Label
label prop을 이용해 배지의 레이블을 설정할 수 있어요.
<template>
<UBadge label="Badge" />
</template>
Color
color prop을 이용해 배지의 색상을 바꿀 수 있어요.
<template>
<UBadge color="neutral">
Badge
</UBadge>
</template>
Variant
variant prop을 이용해 배지의 변형을 바꿀 수 있어요.
<template>
<UBadge color="neutral" variant="outline">
Badge
</UBadge>
</template>
Size
size prop을 이용해 배지의 크기를 바꿀 수 있어요.
<template>
<UBadge size="xl">
Badge
</UBadge>
</template>
Icon
icon prop을 이용해 배지 안에 Icon을 표시할 수 있어요.
<template>
<UBadge icon="i-lucide-rocket" size="md" color="primary" variant="solid">
Badge
</UBadge>
</template>
leading과 trailing prop을 이용해 아이콘 위치를 정하거나, leading-icon과 trailing-icon prop을 이용해 각 위치에 서로 다른 아이콘을 지정할 수 있어요.
<template>
<UBadge trailing-icon="i-lucide-arrow-right" size="md">
Badge
</UBadge>
</template>
Avatar
avatar prop을 이용해 배지 안에 Avatar를 표시할 수 있어요.
<template>
<UBadge :avatar="{
src: 'https://github.com/nuxt.png',
loading: 'lazy'
}" size="md" color="neutral" variant="outline">
Badge
</UBadge>
</template>
예시 (Examples)
class prop
class prop을 이용해 배지의 기본 스타일을 덮어쓸 수 있어요.
<template>
<UBadge class="font-bold rounded-full">
Badge
</UBadge>
</template>
API
Props
/**
* Props for the Badge component
*/
interface BadgeProps {
/**
* The element or component this component should render as.
* @default 'span'
*/
as?: any;
label?: string | number | undefined;
/**
* @default 'primary'
*/
color?: "error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral" | undefined;
/**
* @default 'solid'
*/
variant?: "solid" | "outline" | "soft" | "subtle" | undefined;
/**
* @default 'md'
*/
size?: "xs" | "sm" | "md" | "lg" | "xl" | undefined;
/**
* Render the badge with equal padding on all sides.
*/
square?: boolean | undefined;
ui?: { base?: SlotClass; label?: SlotClass; leadingIcon?: SlotClass; leadingAvatar?: SlotClass; leadingAvatarSize?: SlotClass; trailingIcon?: SlotClass; } | undefined;
/**
* Display an icon based on the `leading` and `trailing` props.
*/
icon?: any;
/**
* Display an avatar on the left side.
*/
avatar?: AvatarProps | undefined;
/**
* When `true`, the icon will be displayed on the left side.
*/
leading?: boolean | undefined;
/**
* Display an icon on the left side.
*/
leadingIcon?: any;
/**
* When `true`, the icon will be displayed on the right side.
*/
trailing?: boolean | undefined;
/**
* Display an icon on the right side.
*/
trailingIcon?: any;
}
Slots
/**
* Slots for the Badge component
*/
interface BadgeSlots {
leading(): any;
default(): any;
trailing(): any;
}
테마 (Theme)
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: 'secondary',
variant: 'solid',
class: 'bg-secondary text-inverted'
},
{
color: 'success',
variant: 'solid',
class: 'bg-success text-inverted'
},
{
color: 'info',
variant: 'solid',
class: 'bg-info text-inverted'
},
{
color: 'warning',
variant: 'solid',
class: 'bg-warning text-inverted'
},
{
color: 'error',
variant: 'solid',
class: 'bg-error text-inverted'
},
{
color: 'primary',
variant: 'outline',
class: 'text-primary ring ring-inset ring-primary/50'
},
{
color: 'secondary',
variant: 'outline',
class: 'text-secondary ring ring-inset ring-secondary/50'
},
{
color: 'success',
variant: 'outline',
class: 'text-success ring ring-inset ring-success/50'
},
{
color: 'info',
variant: 'outline',
class: 'text-info ring ring-inset ring-info/50'
},
{
color: 'warning',
variant: 'outline',
class: 'text-warning ring ring-inset ring-warning/50'
},
{
color: 'error',
variant: 'outline',
class: 'text-error ring ring-inset ring-error/50'
},
{
color: 'primary',
variant: 'soft',
class: 'bg-primary/10 text-primary'
},
{
color: 'secondary',
variant: 'soft',
class: 'bg-secondary/10 text-secondary'
},
{
color: 'success',
variant: 'soft',
class: 'bg-success/10 text-success'
},
{
color: 'info',
variant: 'soft',
class: 'bg-info/10 text-info'
},
{
color: 'warning',
variant: 'soft',
class: 'bg-warning/10 text-warning'
},
{
color: 'error',
variant: 'soft',
class: 'bg-error/10 text-error'
},
{
color: 'primary',
variant: 'subtle',
class: 'bg-primary/10 text-primary ring ring-inset ring-primary/25'
},
{
color: 'secondary',
variant: 'subtle',
class: 'bg-secondary/10 text-secondary ring ring-inset ring-secondary/25'
},
{
color: 'success',
variant: 'subtle',
class: 'bg-success/10 text-success ring ring-inset ring-success/25'
},
{
color: 'info',
variant: 'subtle',
class: 'bg-info/10 text-info ring ring-inset ring-info/25'
},
{
color: 'warning',
variant: 'subtle',
class: 'bg-warning/10 text-warning ring ring-inset ring-warning/25'
},
{
color: 'error',
variant: 'subtle',
class: 'bg-error/10 text-error ring ring-inset ring-error/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'
}
}
}
})