Badge
Display a number or dot at the corner of a button, icon, avatar, or other element to indicate unread messages, pending tasks, and similar counts. A badge can also stand alone beside text.
Import
import { HuiBadge } from '@nscode/handy-ui'
Basic Usage
Place the target element in the default slot and pass the displayed content as value. The badge's center aligns with the element's top-right corner.
<script setup lang="ts">
import { HuiBadge, HuiButton } from '@nscode/handy-ui'
</script>
<template>
<HuiBadge :value="12">
<HuiButton>Messages</HuiButton>
</HuiBadge>
</template>
Wrapping Other Elements
The default slot accepts any element, including buttons, icons, avatars, and text links. The badge overlays its top-right corner.
<script setup lang="ts">
import { HuiAvatar, HuiBadge, HuiButton, HuiIcon, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap size="large" align="center">
<HuiBadge :value="3">
<HuiButton icon="bell" aria-label="Notifications" />
</HuiBadge>
<HuiBadge :value="7">
<HuiIcon name="mail" size="xl" />
</HuiBadge>
<HuiBadge dot>
<HuiAvatar icon="user" shape="square" />
</HuiBadge>
<HuiBadge :value="2" type="primary">
<HuiAvatar label="张" />
</HuiBadge>
<HuiBadge value="new">
<a href="https://example.com" target="_blank">Changelog</a>
</HuiBadge>
</HuiSpace>
</template>
Maximum Value
max caps numeric display: values above it appear as max+, while other values are shown unchanged. It applies only to numbers; string values are displayed as provided. A numeric value of 0 is also visible.
<script setup lang="ts">
import { HuiBadge, HuiButton, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap size="large">
<HuiBadge :value="15" :max="10">
<HuiButton>15, max 10</HuiButton>
</HuiBadge>
<HuiBadge :value="100" :max="99">
<HuiButton>100, max 99</HuiButton>
</HuiBadge>
<HuiBadge :value="8" :max="99">
<HuiButton>8, max 99</HuiButton>
</HuiBadge>
<HuiBadge :value="1000">
<HuiButton>1000, no limit</HuiButton>
</HuiBadge>
</HuiSpace>
<HuiSpace wrap size="large">
<HuiBadge value="999" :max="99">
<HuiButton>Strings ignore max</HuiButton>
</HuiBadge>
<HuiBadge value="new">
<HuiButton>Text</HuiButton>
</HuiBadge>
<HuiBadge value="Hot">
<HuiButton>A word</HuiButton>
</HuiBadge>
<HuiBadge :value="0">
<HuiButton>Value 0</HuiButton>
</HuiBadge>
</HuiSpace>
</HuiSpace>
</template>
Dots
dot displays only a small dot, hiding any value even when one is provided.
<script setup lang="ts">
import { HuiBadge, HuiButton, HuiIcon, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap size="large" align="center">
<HuiBadge dot>
<HuiButton>Messages</HuiButton>
</HuiBadge>
<HuiBadge dot :value="99">
<HuiButton>A dot even with value provided</HuiButton>
</HuiBadge>
<HuiBadge dot>
<HuiIcon name="bell" size="lg" />
</HuiBadge>
</HuiSpace>
</template>
Types and Colors
type accepts primary, success, warning, danger, info, or help. The default without a type is red. color sets a custom background and takes precedence over type.
The first row below shows numeric badges; the second shows dots. From left to right: no type, all six types, and a custom color:
<script setup lang="ts">
import { HuiBadge, HuiButton, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap size="large">
<span class="row-label">Number</span>
<HuiBadge :value="5">
<HuiButton>Omitted</HuiButton>
</HuiBadge>
<HuiBadge :value="5" type="primary">
<HuiButton>Primary</HuiButton>
</HuiBadge>
<HuiBadge :value="5" type="success">
<HuiButton>Success</HuiButton>
</HuiBadge>
<HuiBadge :value="5" type="warning">
<HuiButton>Warning</HuiButton>
</HuiBadge>
<HuiBadge :value="5" type="danger">
<HuiButton>Danger</HuiButton>
</HuiBadge>
<HuiBadge :value="5" type="info">
<HuiButton>Info</HuiButton>
</HuiBadge>
<HuiBadge :value="5" type="help">
<HuiButton>Help</HuiButton>
</HuiBadge>
<HuiBadge :value="5" color="#722ed1">
<HuiButton>Custom</HuiButton>
</HuiBadge>
</HuiSpace>
<HuiSpace wrap size="large">
<span class="row-label">dot</span>
<HuiBadge dot>
<HuiButton>Omitted</HuiButton>
</HuiBadge>
<HuiBadge dot type="primary">
<HuiButton>Primary</HuiButton>
</HuiBadge>
<HuiBadge dot type="success">
<HuiButton>Success</HuiButton>
</HuiBadge>
<HuiBadge dot type="warning">
<HuiButton>Warning</HuiButton>
</HuiBadge>
<HuiBadge dot type="danger">
<HuiButton>Danger</HuiButton>
</HuiBadge>
<HuiBadge dot type="info">
<HuiButton>Info</HuiButton>
</HuiBadge>
<HuiBadge dot type="help">
<HuiButton>Help</HuiButton>
</HuiBadge>
<HuiBadge dot color="#722ed1">
<HuiButton>Custom</HuiButton>
</HuiBadge>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Position and Offset
position selects a corner: top-right (the default), top-left, bottom-right, or bottom-left. offset adds an [x, y] pixel offset: positive x moves right and positive y moves down; negative values reverse those directions.
<script setup lang="ts">
import { HuiBadge, HuiButton, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap size="large">
<HuiBadge :value="8">
<HuiButton>Top-right (top-right)</HuiButton>
</HuiBadge>
<HuiBadge :value="8" position="top-left">
<HuiButton>Top-left (top-left)</HuiButton>
</HuiBadge>
<HuiBadge :value="8" position="bottom-right">
<HuiButton>Bottom-right (bottom-right)</HuiButton>
</HuiBadge>
<HuiBadge :value="8" position="bottom-left">
<HuiButton>Bottom-left (bottom-left)</HuiButton>
</HuiBadge>
</HuiSpace>
<HuiSpace wrap size="large">
<HuiBadge :value="8" :offset="[10, 0]">
<HuiButton>10px right</HuiButton>
</HuiBadge>
<HuiBadge :value="8" :offset="[0, 10]">
<HuiButton>10px down</HuiButton>
</HuiBadge>
<HuiBadge :value="8" :offset="[-10, 6]">
<HuiButton>10px left, 6px down</HuiButton>
</HuiBadge>
<HuiBadge dot position="bottom-left" :offset="[6, -6]">
<HuiButton>Bottom-left, inset 6px</HuiButton>
</HuiBadge>
</HuiSpace>
</HuiSpace>
</template>
Shapes and Sizes
shape accepts circle (the default, with fully rounded ends), rounded (small rounded corners), or square (square corners). size accepts small, default, or large, with heights of 14, 18, and 22px and dot diameters of 6, 8, and 10px respectively. When omitted, size follows HuiConfigProvider.size, falling back to default.
The legacy size="medium" is deprecated and behaves like default; existing code can continue to use it.
The first three rows compare each shape in three sizes, followed by a text badge. The final row compares dots in all three sizes:
<script setup lang="ts">
import { HuiBadge, HuiButton, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap size="large">
<span class="row-label">circle</span>
<HuiBadge :value="12" size="large">
<HuiButton>large</HuiButton>
</HuiBadge>
<HuiBadge :value="12">
<HuiButton>default</HuiButton>
</HuiBadge>
<HuiBadge :value="12" size="small">
<HuiButton>small</HuiButton>
</HuiBadge>
<HuiBadge value="new" size="large">
<HuiButton>Text</HuiButton>
</HuiBadge>
</HuiSpace>
<HuiSpace wrap size="large">
<span class="row-label">rounded</span>
<HuiBadge :value="12" shape="rounded" size="large">
<HuiButton>large</HuiButton>
</HuiBadge>
<HuiBadge :value="12" shape="rounded">
<HuiButton>default</HuiButton>
</HuiBadge>
<HuiBadge :value="12" shape="rounded" size="small">
<HuiButton>small</HuiButton>
</HuiBadge>
<HuiBadge value="new" shape="rounded" size="large">
<HuiButton>Text</HuiButton>
</HuiBadge>
</HuiSpace>
<HuiSpace wrap size="large">
<span class="row-label">square</span>
<HuiBadge :value="12" shape="square" size="large">
<HuiButton>large</HuiButton>
</HuiBadge>
<HuiBadge :value="12" shape="square">
<HuiButton>default</HuiButton>
</HuiBadge>
<HuiBadge :value="12" shape="square" size="small">
<HuiButton>small</HuiButton>
</HuiBadge>
<HuiBadge value="new" shape="square" size="large">
<HuiButton>Text</HuiButton>
</HuiBadge>
</HuiSpace>
<HuiSpace wrap size="large">
<span class="row-label">dot</span>
<HuiBadge dot size="large">
<HuiButton>large</HuiButton>
</HuiBadge>
<HuiBadge dot>
<HuiButton>default</HuiButton>
</HuiBadge>
<HuiBadge dot size="small">
<HuiButton>small</HuiButton>
</HuiBadge>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 64px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Numeric Change Animation
With animated=true, a changed number slides upward into view as the old number slides out. Dot mode has no animation. Compare the animated badge on the left with the regular badge on the right:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiBadge, HuiButton, HuiSpace } from '@nscode/handy-ui'
const count = ref(5)
</script>
<template>
<HuiSpace wrap size="large">
<HuiBadge :value="count" :max="99" animated>
<HuiButton>Animated</HuiButton>
</HuiBadge>
<HuiBadge :value="count" :max="99">
<HuiButton>Not animated</HuiButton>
</HuiBadge>
<HuiButton icon="plus" @click="count++">Add 1</HuiButton>
<HuiButton icon="minus" :disabled="count <= 0" @click="count--">Subtract 1</HuiButton>
</HuiSpace>
</template>
Hiding
hidden=true fades the badge out. Setting it back to false scales and fades it in. This applies to both numeric badges and dots.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiBadge, HuiButton, HuiSpace } from '@nscode/handy-ui'
const hidden = ref(false)
</script>
<template>
<HuiSpace wrap size="large">
<HuiBadge :value="3" :hidden="hidden">
<HuiButton>Messages</HuiButton>
</HuiBadge>
<HuiBadge dot :hidden="hidden">
<HuiButton>Activity</HuiButton>
</HuiBadge>
<HuiButton @click="hidden = !hidden">{{ hidden ? 'Show' : 'Hide' }}</HuiButton>
</HuiSpace>
</template>
Standalone Badges
Without a default slot, the badge is standalone, vertically centered inline with text. position has no effect on standalone badges; other props work normally.
<script setup lang="ts">
import { HuiBadge, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace size="large">
<span>Tasks <HuiBadge :value="5" /></span>
<span>New <HuiBadge dot /></span>
<span>Comments <HuiBadge :value="120" :max="99" type="primary" /></span>
<span>Status <HuiBadge value="Live" type="success" shape="rounded" /></span>
</HuiSpace>
<HuiSpace size="large" align="center">
<HuiBadge :value="1" type="primary" size="small" />
<HuiBadge :value="2" type="success" />
<HuiBadge :value="3" type="warning" size="large" />
<HuiBadge value="beta" type="help" shape="square" />
<HuiBadge dot type="info" size="large" />
</HuiSpace>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| value | string | number | - | Displayed value. Without a value or dot mode, no badge is shown |
| max | number | - | Maximum numeric value, displaying max+ above it; affects numbers only |
| type | 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'help' | - | Type color; red when omitted |
| color | string | - | Custom background color, overriding type |
| dot | boolean | false | Whether to display a dot instead of a value |
| hidden | boolean | false | Whether hidden |
| offset | [number, number] | - | Offset from position in [x, y] pixels; positive x moves right and positive y moves down |
| shape | 'circle' | 'square' | 'rounded' | 'circle' | Shape |
| position | 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left' | 'top-right' | Corner when wrapping content; ignored for standalone badges |
| size | 'large' | 'default' | 'small' | 'medium' | - | Size. Follows HuiConfigProvider.size when omitted, otherwise falls back to 'default'. Deprecated 'medium' is equivalent to 'default' |
| animated | boolean | false | Whether numeric changes use a sliding animation |
Slots
| Slot | Scope arguments | Description |
|---|---|---|
| default | - | Content to mark; omission creates a standalone badge |
Types
import type { BadgeProps, BadgeType, BadgeShape, BadgePosition, BadgeSize } from '@nscode/handy-ui'
type BadgeType = 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'help'
type BadgeShape = 'circle' | 'square' | 'rounded'
type BadgePosition = 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'
/** @deprecated Use 'default'. 'medium' remains only for compatibility and behaves like 'default' */
type BadgeSizeMedium = 'medium'
type BadgeSize = 'large' | 'default' | 'small' | BadgeSizeMedium
interface BadgeProps {
value?: string | number
max?: number
type?: BadgeType
color?: string
dot?: boolean
hidden?: boolean
offset?: [number, number]
shape?: BadgeShape
position?: BadgePosition
size?: BadgeSize
animated?: boolean
}