概述
标签片表示人物、地点、标签或值。你可以单独使用它们,也可以在 Combobox 等组件中使用,在其中它们显示选中的值。它们与 徽章 类似,但有一些有意义的区别。
- 标签片支持激活状态和关闭按钮。
- 标签片只有一种尺寸——它们不会像徽章那样随父元素
font-size缩放。 - 标签片可以带有图标、头像和关闭按钮。
- 标签片可以单独或作为父容器上的组来设置主题。默认情况下,标签片使用
subtle和text主题 tokens,而激活状态使用bg和contrasttokens。 - 标签片可以处于激活或禁用状态。
在下面查看所有这些功能的实际示例。
标签片
使用 .chip 表示独立标签片。添加 .chip-icon 表示前置图标,.chip-img 表示头像等图像,.chip-dismiss 表示移除按钮。请注意,我们使用内联 SVG 作为关闭按钮图标——你可以根据需要修改它。
静态标签片没有悬停效果。关闭按钮有自己独立的悬停状态,而不改变整个标签片。
Basic chip
With avatar
With icon
Dismissible
<span class="chip">Basic chip</span>
<span class="chip">
<img src="https://github.com/mdo.png" class="chip-img" width="16" height="16" alt="">
With avatar
</span>
<span class="chip">
<span class="chip-icon">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
<path d="M3 14s-1 0-1-1 1-4 6-4 6 3 6 4-1 1-1 1zm5-6a3 3 0 1 0 0-6 3 3 0 0 0 0 6"/>
</svg>
</span>
With icon
</span>
<span class="chip">
Dismissible
<button type="button" class="chip-dismiss" aria-label="Remove">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><line x1="4" y1="4" x2="12" y2="12"/><line x1="12" y1="4" x2="4" y2="12"/></svg>
</button>
</span>交互式标签片
使用按钮表示操作,或使用链接表示导航。交互元素上或内部的标签片具有指针光标和悬停反馈。Combobox 也为其可选择标签片提供这些状态。
键盘焦点使用标签片的主题颜色,包括关闭按钮上的焦点。没有主题的标签片使用默认焦点颜色。
<button type="button" class="chip theme-success">Action chip</button>
<a class="chip theme-danger" href="#multiple-value-inputs">Link chip</a>带主题的标签片
应用 .theme-* 类为标签片着色。标签片默认是柔和的,因为这样可以实现清晰的、带主题的激活状态。
Default
Primary
Accent
Success
Danger
Warning
Info
Inverse
Secondary
<span class="chip">Default</span>
<span class="chip theme-primary">Primary</span>
<span class="chip theme-accent">Accent</span>
<span class="chip theme-success">Success</span>
<span class="chip theme-danger">Danger</span>
<span class="chip theme-warning">Warning</span>
<span class="chip theme-info">Info</span>
<span class="chip theme-inverse">Inverse</span>
<span class="chip theme-secondary">Secondary</span>激活状态
添加 .active 使标签片使用实心外观(bg/contrast)。这适用于切换式标签片选择。
Default
Active
Default
Active
<span class="chip theme-primary">Default</span>
<span class="chip theme-primary active">Active</span>
<span class="chip theme-success">Default</span>
<span class="chip theme-success active">Active</span>多值输入
使用 Combobox 实现可选择的选项和自由输入值。其多值模式会在可编辑输入框旁渲染标签片。本页的 .chip 类提供视觉呈现;Combobox 负责键盘行为、移除和表单提交。
CSS
标签片变量
Chip use local CSS variables on .chip for real-time customization. Values for the CSS variables are generated from Sass maps unique to each component and applied to the aforementioned class.
// stylelint-disable-next-line scss/dollar-variable-default
$chip-tokens: defaults(
(
--chip-height: 1.75rem,
--chip-padding-x: .625rem,
--chip-gap: .3125rem,
--chip-border-radius: var(--radius-pill),
--chip-img-size: 1.25rem,
--chip-icon-size: 1rem,
--chip-dismiss-size: 1rem,
--chip-dismiss-opacity: .65,
--chip-dismiss-hover-opacity: 1,
--chip-color: var(--theme-fg, var(--fg-body)),
--chip-bg: var(--theme-bg-subtle, var(--bg-2)),
--chip-border-color: transparent,
--chip-selected-color: var(--theme-contrast, var(--primary-contrast)),
--chip-selected-bg: var(--theme-bg, var(--primary-bg)),
--chip-selected-border-color: var(--theme-bg, var(--primary-bg)),
),
$chip-tokens
);