Tag
A compact label for status, categories, or attributes, such as Published or Draft. Tags can have a close button. Use Chip when you need selection, icons, or avatars.
Import
import { HuiTag } from '@nscode/handy-ui'
Basic usage
Place text in the default slot.
<script setup lang="ts">
import { HuiTag } from '@nscode/handy-ui'
</script>
<template>
<HuiTag>Tag</HuiTag>
</template>
Types
type controls color and accepts primary, success, warning, danger, info, and help. The default is primary.
<script setup lang="ts">
import { HuiSpace, HuiTag } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiTag type="primary">primary</HuiTag>
<HuiTag type="success">success</HuiTag>
<HuiTag type="warning">warning</HuiTag>
<HuiTag type="danger">danger</HuiTag>
<HuiTag type="info">info</HuiTag>
<HuiTag type="help">help</HuiTag>
</HuiSpace>
</template>
Effects
effect accepts three values: light (default) uses a tinted background and the primary color for text; dark uses a primary-colored background and white text; plain uses the theme background (white in the light theme), primary-colored text, and a lighter primary-colored border.
Each row below shows one effect across all six types:
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">light</span>
<HuiTag type="primary" effect="light">Primary</HuiTag>
<HuiTag type="success" effect="light">Success</HuiTag>
<HuiTag type="warning" effect="light">Warning</HuiTag>
<HuiTag type="danger" effect="light">Danger</HuiTag>
<HuiTag type="info" effect="light">Info</HuiTag>
<HuiTag type="help" effect="light">Help</HuiTag>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">dark</span>
<HuiTag type="primary" effect="dark">Primary</HuiTag>
<HuiTag type="success" effect="dark">Success</HuiTag>
<HuiTag type="warning" effect="dark">Warning</HuiTag>
<HuiTag type="danger" effect="dark">Danger</HuiTag>
<HuiTag type="info" effect="dark">Info</HuiTag>
<HuiTag type="help" effect="dark">Help</HuiTag>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">plain</span>
<HuiTag type="primary" effect="plain">Primary</HuiTag>
<HuiTag type="success" effect="plain">Success</HuiTag>
<HuiTag type="warning" effect="plain">Warning</HuiTag>
<HuiTag type="danger" effect="plain">Danger</HuiTag>
<HuiTag type="info" effect="plain">Info</HuiTag>
<HuiTag type="help" effect="plain">Help</HuiTag>
</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>
Sizes
size accepts large, default, and small, with heights of 32, 24, and 18px respectively. When omitted, it follows HuiConfigProvider's size, falling back to default. The close button and corner radius scale with size.
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap align="center">
<HuiTag size="large">Large</HuiTag>
<HuiTag>Default</HuiTag>
<HuiTag size="small">Small</HuiTag>
</HuiSpace>
<HuiSpace wrap align="center">
<HuiTag type="success" size="large" closable>Closable</HuiTag>
<HuiTag type="success" closable>Closable</HuiTag>
<HuiTag type="success" size="small" closable>Closable</HuiTag>
</HuiSpace>
<HuiSpace wrap align="center">
<HuiTag type="warning" effect="dark" size="large" round>Rounded</HuiTag>
<HuiTag type="warning" effect="dark" round>Rounded</HuiTag>
<HuiTag type="warning" effect="dark" size="small" round>Rounded</HuiTag>
</HuiSpace>
</HuiSpace>
</template>
Closable tags
closable adds a close button after the text and works with every effect. The button follows the text color and gains a translucent background on hover.
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">light</span>
<HuiTag type="primary" closable>Primary</HuiTag>
<HuiTag type="success" closable>Success</HuiTag>
<HuiTag type="warning" closable>Warning</HuiTag>
<HuiTag type="danger" closable>Danger</HuiTag>
<HuiTag type="info" closable>Info</HuiTag>
<HuiTag type="help" closable>Help</HuiTag>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">dark</span>
<HuiTag type="primary" effect="dark" closable>Primary</HuiTag>
<HuiTag type="success" effect="dark" closable>Success</HuiTag>
<HuiTag type="warning" effect="dark" closable>Warning</HuiTag>
<HuiTag type="danger" effect="dark" closable>Danger</HuiTag>
<HuiTag type="info" effect="dark" closable>Info</HuiTag>
<HuiTag type="help" effect="dark" closable>Help</HuiTag>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">plain</span>
<HuiTag type="primary" effect="plain" closable>Primary</HuiTag>
<HuiTag type="success" effect="plain" closable>Success</HuiTag>
<HuiTag type="warning" effect="plain" closable>Warning</HuiTag>
<HuiTag type="danger" effect="plain" closable>Danger</HuiTag>
<HuiTag type="info" effect="plain" closable>Info</HuiTag>
<HuiTag type="help" effect="plain" closable>Help</HuiTag>
</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>
Clicking the close button emits close; the tag does not remove itself. This example confirms the target before updating the list and the Last closed feedback. Canceling, closing, or pressing Esc leaves both unchanged. Reset restores initial tags while preserving additions, so it does not silently delete data. Adding, resetting, renaming, changing read-only state, or unmounting while confirmation is pending invalidates it.
<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from 'vue'
import { HuiButton, HuiMessageBox, HuiSpace, HuiTag } from '@nscode/handy-ui'
// Deletion confirmation: remove a Tag
const all = ['Vue', 'React', 'Angular', 'Svelte']
const tags = ref([...all])
const lastClosed = ref('')
const readonly = ref(false)
const pending = ref(false)
let alive = true
let version = 0
let nextId = 1
watch([tags, readonly, lastClosed], () => version++, { deep: true, flush: 'sync' })
onBeforeUnmount(() => {
alive = false
version++
})
const handleClose = async (name: string) => {
if (!alive || readonly.value || pending.value || !tags.value.includes(name)) return
const collection = tags.value
const snapshot = collection.slice()
const request = version
pending.value = true
try {
await HuiMessageBox.confirm(
`Remove tag “${name}” (1 item)? The other ${snapshot.length - 1} tags will remain. Last closed feedback is updated after confirmation; other examples are unaffected.`,
'Confirm tag removal',
{ type: 'warning' }
)
if (
!alive ||
readonly.value ||
version !== request ||
tags.value !== collection ||
collection.length !== snapshot.length ||
collection.some((item, index) => item !== snapshot[index])
)
return
tags.value = collection.filter((item) => item !== name)
lastClosed.value = name
} catch {
// Canceling, closing, or pressing Esc preserves the list and Last closed feedback.
} finally {
if (alive) pending.value = false
}
}
const reset = () => {
if (!alive || readonly.value) return
tags.value = [...all, ...tags.value.filter((item) => !all.includes(item))]
lastClosed.value = ''
}
const add = () => {
if (!alive || readonly.value) return
tags.value = [...tags.value, `新标签${nextId++}`]
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap align="center">
<label><input v-model="readonly" type="checkbox" /> Read-only</label>
<HuiTag
v-for="tag in tags"
:key="tag"
:closable="!readonly && !pending"
@close="handleClose(tag)"
>
{{ tag }}
</HuiTag>
<HuiButton size="small" :disabled="readonly" @click="add">Add tag</HuiButton>
<HuiButton size="small" icon="refresh" :disabled="readonly" @click="reset">Reset</HuiButton>
</HuiSpace>
<span>{{ lastClosed ? `Last closed: ${lastClosed}` : 'Try the × button on a tag' }}</span>
<span v-if="pending">Waiting for confirmation; tags and close feedback are unchanged</span>
</HuiSpace>
</template>
Rounded ends
round makes both ends semicircular. It works with every effect and with close buttons.
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">light</span>
<HuiTag type="primary" round>Primary</HuiTag>
<HuiTag type="success" round>Success</HuiTag>
<HuiTag type="warning" round>Warning</HuiTag>
<HuiTag type="danger" round>Danger</HuiTag>
<HuiTag type="info" round>Info</HuiTag>
<HuiTag type="help" round>Help</HuiTag>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">dark</span>
<HuiTag type="primary" effect="dark" round>Primary</HuiTag>
<HuiTag type="success" effect="dark" round>Success</HuiTag>
<HuiTag type="warning" effect="dark" round>Warning</HuiTag>
<HuiTag type="danger" effect="dark" round>Danger</HuiTag>
<HuiTag type="info" effect="dark" round>Info</HuiTag>
<HuiTag type="help" effect="dark" round>Help</HuiTag>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">plain</span>
<HuiTag type="primary" effect="plain" round>Primary</HuiTag>
<HuiTag type="success" effect="plain" round>Success</HuiTag>
<HuiTag type="warning" effect="plain" round>Warning</HuiTag>
<HuiTag type="danger" effect="plain" round>Danger</HuiTag>
<HuiTag type="info" effect="plain" round>Info</HuiTag>
<HuiTag type="help" effect="plain" round>Help</HuiTag>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">closable</span>
<HuiTag round closable>Closable</HuiTag>
<HuiTag type="success" effect="dark" round closable>Closable</HuiTag>
<HuiTag type="danger" effect="plain" round closable>Closable</HuiTag>
</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>
Stronger borders
hit uses the type's primary color for the border, making it stronger than the default light border in light and plain. The dark border already uses that color, so hit makes no difference.
The first row shows light without hit for comparison; the next three rows show each effect with hit:
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Without hit</span>
<HuiTag type="primary">Primary</HuiTag>
<HuiTag type="success">Success</HuiTag>
<HuiTag type="warning">Warning</HuiTag>
<HuiTag type="danger">Danger</HuiTag>
<HuiTag type="info">Info</HuiTag>
<HuiTag type="help">Help</HuiTag>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">light</span>
<HuiTag type="primary" hit>Primary</HuiTag>
<HuiTag type="success" hit>Success</HuiTag>
<HuiTag type="warning" hit>Warning</HuiTag>
<HuiTag type="danger" hit>Danger</HuiTag>
<HuiTag type="info" hit>Info</HuiTag>
<HuiTag type="help" hit>Help</HuiTag>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">plain</span>
<HuiTag type="primary" effect="plain" hit>Primary</HuiTag>
<HuiTag type="success" effect="plain" hit>Success</HuiTag>
<HuiTag type="warning" effect="plain" hit>Warning</HuiTag>
<HuiTag type="danger" effect="plain" hit>Danger</HuiTag>
<HuiTag type="info" effect="plain" hit>Info</HuiTag>
<HuiTag type="help" effect="plain" hit>Help</HuiTag>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">dark</span>
<HuiTag type="primary" effect="dark" hit>Primary</HuiTag>
<HuiTag type="success" effect="dark" hit>Success</HuiTag>
<HuiTag type="warning" effect="dark" hit>Warning</HuiTag>
<HuiTag type="danger" effect="dark" hit>Danger</HuiTag>
<HuiTag type="info" effect="dark" hit>Info</HuiTag>
<HuiTag type="help" effect="dark" hit>Help</HuiTag>
</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>
Click events
Clicking a tag emits click. Tags have no built-in selection state. This example combines click and effect for simple multiple selection; use HuiChipGroup for a ready-made selection component.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTag } from '@nscode/handy-ui'
const options = ['前端', '后端', '测试', '运维']
const selected = ref<string[]>(['前端'])
const toggle = (name: string) => {
selected.value = selected.value.includes(name)
? selected.value.filter((item) => item !== name)
: [...selected.value, name]
}
</script>
<template>
<HuiSpace wrap>
<HuiTag
v-for="name in options"
:key="name"
:effect="selected.includes(name) ? 'dark' : 'plain'"
style="cursor: pointer"
@click="toggle(name)"
>
{{ name }}
</HuiTag>
</HuiSpace>
<p style="margin-top: 12px">Selected: {{ selected.length ? selected.join('、') : 'None' }}</p>
</template>
When listening to both click and close, the close button emits only close:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTag } from '@nscode/handy-ui'
const log = ref('')
</script>
<template>
<HuiTag
type="help"
closable
style="cursor: pointer"
@click="log = 'click emitted'"
@close="log = 'close emitted'"
>
Click the text or ×
</HuiTag>
<p style="margin-top: 12px">{{ log || 'No event yet' }}</p>
</template>
CSS variables on ancestors
Public CSS variables are read-only inputs. Without overrides, default colors follow type and effect. Set overrides on the component or any ancestor to share them across tags. hit changes only the private default border and cannot override public configuration. Nested tags inherit the outer public configuration while calculating their own private defaults for type and effect.
| Variable | Default | Description |
|---|---|---|
--hui-tag-color | Type color; white for dark effect | Text and close button color |
--hui-tag-bg | Depends on type/effect | Background color |
--hui-tag-border-color | Depends on type/effect/hit | Border color |
The border variable was renamed from --hui-tag-border to --hui-tag-border-color; no fallback for the old name is retained.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTag, HuiSpace } from '@nscode/handy-ui'
const variableCloseCount = ref(0)
</script>
<template>
<HuiSpace
class="tag-variables-demo"
direction="vertical"
fill
style="--hui-tag-color: var(--hui-color-primary); --hui-tag-bg: var(--hui-color-primary-light-9); --hui-tag-border-color: var(--hui-color-primary)"
>
<span
>These three effects share colors from their ancestor; default type and effect colors remain
independently available.</span
>
<HuiSpace wrap>
<HuiTag type="success" effect="light" closable @close="variableCloseCount++">Light</HuiTag>
<HuiTag type="danger" effect="dark" closable @close="variableCloseCount++">Dark</HuiTag>
<HuiTag type="warning" effect="plain" hit closable @close="variableCloseCount++"
>Plain outline</HuiTag
>
</HuiSpace>
<span class="tag-variables-events">Close notifications: {{ variableCloseCount }}</span>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| type | 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'help' | 'primary' | Tag type and color |
| size | 'large' | 'default' | 'small' | 'default' | Size; follows HuiConfigProvider when omitted, falling back to 'default' |
| closable | boolean | false | Whether to show the close button |
| effect | 'dark' | 'light' | 'plain' | 'light' | Effect: light uses a tinted background, dark uses the primary background with white text, and plain uses the theme background |
| round | boolean | false | Whether both ends are semicircular |
| hit | boolean | false | Use the type color for the border |
Events
| Event | Parameters | Description |
|---|---|---|
| click | (event: MouseEvent) | Emitted when the tag is clicked; not emitted by the close button |
| close | (event: MouseEvent) | Emitted when the close button is clicked |
Slots
| Slot | Scope | Description |
|---|---|---|
| default | - | Tag content |
Types
import type { TagType, TagSize, TagEffect, TagProps } from '@nscode/handy-ui'
type TagType = 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'help'
type TagSize = 'large' | 'default' | 'small'
type TagEffect = 'dark' | 'light' | 'plain'
interface TagProps {
type?: TagType
size?: TagSize
closable?: boolean
effect?: TagEffect
round?: boolean
hit?: boolean
}
TagEmits is also exported.