JGuang

内容标签

Token · Astryx

交互预览
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

内容标签 · Token

内容标签用于内容展示场景。下面的官方示例展示原生用法与状态,属性名保持上游 API。

安装与前置条件

npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0

需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。

示例与使用方式

  • 内容标签 · 综合展示 1:首先体验这个示例,再查看代码理解组件的组合方式。
  • 内容标签 · 交互示例 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 内容标签 · 交互示例 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 内容标签 · 交互示例 4:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 内容标签 · 交互示例 5:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
  • 内容标签 · 交互示例 6:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。

API 与上游详细参考

优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。

Token

Token is a small, inline element for representing discrete pieces of associated data, like tags, categories, or selections. Use it to label content, show active filters, or represent removable items like selected recipients in a compose field.

属性类型默认值说明
label *string显示在标记内部的文本标签。
size'sm' | 'md' | 'lg''md'标记的大小。
color'default' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'cyan' | 'blue' | 'purple' | 'pink' | 'gray''default'标记的颜色变体。
iconReactNode在标签前渲染的可选图标。
isDisabledbooleanfalse标记是否被禁用;降低透明度并阻止交互。
onRemove(e: React.MouseEvent) => void点击移除按钮时触发的回调。提供时,标记内会渲染一个 X 按钮。
onClick(e: React.MouseEvent) => void点击处理函数。提供时,标记渲染为 <span> 容器,内部包含不可见的 <button> 以确保可访问性。
hrefstring链接 URL。提供时,标记渲染为 <a> 元素。
descriptionstring通过 aria-description 应用于根元素的无障碍描述。
endContentReactNode在标签之后、移除按钮之前渲染的内容。
isLabelHiddenbooleanfalse使用仅屏幕阅读器可见的裁剪技术视觉隐藏标签;标签仍然保持可访问性。
xstyleStyleXStyles用于布局自定义的 StyleX 样式(外边距、定位、尺寸)。必须是 stylex.create() 的值,不能是内联样式对象如 style={{}}。

使用建议

  • Use color to distinguish categories (for example, green for "Active", red for "Blocked", blue for "In Review") so users can scan status at a glance.
  • Provide an onRemove callback when tokens represent user selections that can be undone, like filters or multi-select values.
  • Add a leading icon when it helps identify the token type faster, like a person icon for user tokens or a tag icon for labels.
  • Keep labels short: one to three words. Tokens truncate with ellipsis when the text overflows.
  • 避免:Don't use tokens for primary actions or navigation; use Button or Link instead. Tokens are for displaying metadata, not triggering workflows.
  • 避免:Don't hide the label unless the icon alone is universally understood. A color dot without text is ambiguous.
  • 避免:Don't mix too many colors in one token group. Stick to two or three meaningful colors so the palette stays scannable.

主题 API

{
  "targets": [
    {
      "className": "astryx-token",
      "visualProps": [
        "color",
        "size"
      ]
    }
  ]
}

组件结构

  • Icon: A leading icon that identifies the token type, like a user avatar or category symbol.
  • Label: The visible text. Also used as the accessible name when isLabelHidden is true.
  • End content: Trailing content after the label, like a count badge or status dot.
  • Remove button: An X button that appears when onRemove is provided, letting users dismiss the token.

完整示例的额外依赖

部分示例使用独立图标包。示例清单中的每条安装命令包含该示例实际需要的依赖;若要运行本页全部示例,还需安装:

npm install @heroicons/react@2.2.0