# Tag

> 分类与简短状态标签，沿用现有语义颜色。

网页来源：/components/tag

版本：@quirekit/ui 0.3.1；源码修订：d79a6b48d00e+worktree。此文档对应当前源码构建，接入方式以开始使用为准。

接入前置：[开始使用](/raw/components/getting-started.md)（Vue、样式、主题与图标 Provider）。

## 颜色与外观

默认插槽放置分类或状态文字，`color` 选择语义色。`soft` 为浅底，`subtle` 在浅底上增加描边，`outline` 为透明底描边，`solid` 为实色。
需要选择的筛选项使用 [ToggleGroup](/raw/components/toggle-group.md)，数量提醒使用 [Badge](/raw/components/badge.md)。

### 示例：tag/basic

文件：`basic.vue`

```vue
<script setup lang="ts">
import { QuireTag } from '@quirekit/ui/vue';
const families = ['neutral', 'primary', 'info', 'success', 'warning', 'danger'] as const;
const labels = ['草稿', '进行中', '提示', '已完成', '待确认', '未通过'];
</script>
<template>
  <div class="grid gap-qk-group p-qk-surface" data-tag-demo>
    <div v-for="variant in (['soft', 'subtle', 'outline', 'solid'] as const)" :key="variant" class="flex flex-wrap items-center gap-qk-surface">
      <QuireTag v-for="(color, index) in families" :key="color" :color="color" :variant="variant">{{ labels[index] }}</QuireTag>
    </div>
    <div class="flex flex-wrap items-center gap-qk-surface"><QuireTag size="xs">紧凑标签</QuireTag><QuireTag size="sm">常规标签</QuireTag><QuireTag size="md">宽松标签</QuireTag></div>
  </div>
</template>
```

## 尺寸

`size` 支持 `xs`、`sm`、`md`，默认 `sm`，调整标签文字与留白。

### 示例：tag/sizes

文件：`sizes.vue`

```vue
<script setup lang="ts">
import { QuireTag } from '@quirekit/ui/vue';
</script>
<template>
  <div class="grid gap-qk-surface p-qk-surface" data-tag-sizes>
    <div v-for="size in (['xs', 'sm', 'md'] as const)" :key="size" class="flex flex-wrap items-center gap-qk-surface"><span>{{ size }}</span><QuireTag :size="size">设计规范</QuireTag><QuireTag :size="size" color="success">已完成</QuireTag><QuireTag :size="size" color="warning" variant="outline">待确认</QuireTag></div>
  </div>
</template>
```

## 图标与内容

`icon` 设置前置图标；`leading` 插槽可替换图标，`trailing` 放置末尾标记。两侧插槽用于装饰内容，状态含义仍通过文字说明；缩写可用 `label` 提供完整名称。

### 示例：tag/content

文件：`content.vue`

```vue
<script setup lang="ts">
import { QuireTag, QuireIcon, QuireAvatar } from '@quirekit/ui/vue';
</script>
<template>
  <div class="grid gap-qk-group p-qk-surface" data-tag-content>
    <div class="flex flex-wrap items-center gap-qk-surface">
      <QuireTag color="success" icon="circleCheck">审核通过</QuireTag>
      <QuireTag color="warning" icon="triangleAlert" variant="subtle">待确认</QuireTag>
      <QuireTag color="danger" icon="circleX" variant="solid">未通过</QuireTag>
    </div>
    <div class="flex flex-wrap items-center gap-qk-surface">
      <QuireTag><template #leading><QuireAvatar name="林舒" size="xs" /></template>林舒</QuireTag>
      <QuireTag color="primary">设计规范<template #trailing><QuireIcon name="check" :size="12" /></template></QuireTag>
    </div>
  </div>
</template>
```

## 关闭与禁用

`closable` 显示移除按钮，按钮名称跟随 [Locale Provider](/raw/components/locale-provider.md) 的语言并由标签内容补充说明。`close` 只发出请求，由页面更新列表；`disabled` 只禁用移除。
删除当前标签后，把焦点交给相邻操作或恢复入口。可关闭标签会为按钮保留足够高度。

### 示例：tag/closable

文件：`closable.vue`

```vue
<script setup lang="ts">
import { nextTick, ref } from 'vue';
import { QuireTag, QuireButton } from '@quirekit/ui/vue';
const original = ['设计规范', '交互评审', '团队共享'];
const tags = ref([...original]);
const root = ref<HTMLDivElement | null>(null);
const result = ref('选择移除按钮删除一个标签。');
/**
 * The host removes data and moves focus to a surviving action after the DOM updates.
 * @param tag - Label to remove from this local demonstration.
 */
async function remove(tag: string): Promise<void> {
  const index = tags.value.indexOf(tag);
  tags.value = tags.value.filter(value => value !== tag);
  result.value = `已移除：${tag}`;
  await nextTick();
  const buttons = root.value?.querySelectorAll<HTMLButtonElement>('[data-removable-tags] button:not(:disabled)');
  const next = buttons?.[Math.min(index, buttons.length - 1)];
  (next ?? root.value?.querySelector<HTMLButtonElement>('[data-reset-tags]'))?.focus();
}
</script>
<template>
  <div ref="root" class="grid gap-qk-group p-qk-surface" data-tag-closable>
    <div class="flex flex-wrap items-center gap-qk-surface" data-removable-tags>
      <QuireTag v-for="tag in tags" :key="tag" color="primary" closable @close="remove(tag)">{{ tag }}</QuireTag>
    </div>
    <div class="flex flex-wrap items-center gap-qk-surface">
      <QuireTag closable disabled>组织策略（不可移除）</QuireTag>
      <QuireTag closable variant="solid" color="success" @close="result = '已完成标签发出了关闭请求'">已完成</QuireTag>
      <QuireTag closable color="warning" @close="result = '宿主保留了受保护标签'">受保护标签</QuireTag>
    </div>
    <div class="flex flex-wrap items-center gap-qk-surface">
      <QuireTag v-for="size in (['xs', 'sm', 'md'] as const)" :key="size" :size="size" closable @close="result = `${size} 发出了关闭请求，宿主未删除`">{{ size }} 标签</QuireTag>
    </div>
    <div><QuireButton variant="outline" data-reset-tags @click="tags = [...original]; result = '已恢复全部标签'">恢复标签</QuireButton></div>
    <output role="status" class="text-qk-text-secondary">{{ result }}</output>
  </div>
</template>
```

## 长文本

`maxWidth` 限制宽度，默认允许换行；`truncate` 开启单行省略，图标和关闭按钮保持完整。需要查看全文时组合 Tooltip，同时提供键盘聚焦入口。

### 示例：tag/overflow

文件：`overflow.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireTag, QuireTooltipRoot, QuireTooltipTrigger, QuireTooltipContent } from '@quirekit/ui/vue';
const fullText = '跨团队协作与文档发布流程设计规范';
const count = ref(0);
</script>
<template>
  <div class="grid gap-qk-group p-qk-surface" data-tag-overflow>
    <div class="grid gap-qk-inline justify-items-start"><span>默认换行</span><QuireTag max-width="12rem">{{ fullText }}</QuireTag></div>
    <div class="grid gap-qk-inline justify-items-start">
      <span>单行截断，可悬停或聚焦查看全文</span>
      <QuireTooltipRoot>
        <QuireTooltipTrigger as-child><QuireTag max-width="12rem" truncate tabindex="0">{{ fullText }}</QuireTag></QuireTooltipTrigger>
        <QuireTooltipContent :content="fullText" />
      </QuireTooltipRoot>
    </div>
    <div class="grid gap-qk-inline justify-items-start"><span>图标和关闭按钮保持完整</span><QuireTag max-width="12rem" truncate icon="check" closable @close="count++">{{ fullText }}</QuireTag><output>关闭请求：{{ count }}</output></div>
  </div>
</template>
```

## 键盘操作

静态标签不进入 Tab 顺序。移除按钮支持 Enter 和空格；全文提示可用 Escape 关闭。
输入并管理标签使用 [TagsInput](/raw/components/tags-input.md)。

## API

### QuireTag

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| color | 否 | "neutral" | Existing semantic color family. |
| variant | 否 | "soft" | Quiet fill, tinted outline, outline or strong fill. |
| size | 否 | "sm" | Typography and padding density; removal retains the compact button target. |
| label | 否 | — | Accessible wording for abbreviated content. |
| icon | 否 | — | Optional decorative leading icon from the active registry. |
| closable | 否 | false | Show a removal button; emitting close never removes this component itself. |
| disabled | 否 | false | Disable only the removal action, without changing the meaning of the label. |
| truncate | 否 | false | Truncate text to one line while retaining its full accessible content. |
| maxWidth | 否 | — | Host layout constraint, expressed as a CSS width and capped by the container. |

**color**

```ts
QuireKitButtonColor | undefined
```

```ts
undefined | "info" | "neutral" | "primary" | "success" | "warning" | "danger"
```

**variant**

```ts
QuireKitTagVariant | undefined
```

```ts
undefined | "subtle" | "soft" | "outline" | "solid"
```

**size**

```ts
QuireKitTagSize | undefined
```

```ts
undefined | "xs" | "sm" | "md"
```

**label**

```ts
string | undefined
```

```ts
undefined | string
```

**icon**

```ts
string | undefined
```

```ts
undefined | string
```

**closable**

```ts
boolean | undefined
```

**disabled**

```ts
boolean | undefined
```

**truncate**

```ts
boolean | undefined
```

**maxWidth**

```ts
string | undefined
```

```ts
undefined | string
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| close | 否 | — |  |

**close**

```ts
[event: MouseEvent]
```

#### Slots

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| default | 否 | — | Category or status text. |
| leading | 否 | — | Decorative leading content, replacing the icon prop when provided. |
| trailing | 否 | — | Decorative trailing content, before the dedicated removal action. |

**default**

```ts
any
```

**leading**

```ts
any
```

**trailing**

```ts
any
```

#### 公开实例成员

无。
