Skip to Content
文档

标签

用于对内容进行分类或标记

SourceStorybookRecipe
Plain Tag
Closable Tag

用法

¥Usage

import { Tag } from "@chakra-ui/react"
<Tag.Root>
  <Tag.Label>Tag here</Tag.Label>
</Tag.Root>
info

如果你更喜欢封闭的组件组合,请查看 以下代码片段

¥If you prefer a closed component composition, check out the snippet below.

示例

¥Examples

图标

¥Icon

使用 Tag.StartElementTag.EndElement 组件在标签的开头或结尾添加图标。

¥Use the Tag.StartElement and Tag.EndElement components to add an icon to the start or end of the tag

Tag 1
Top Rated
Tag 2

变量

¥Variants

使用 variant 属性更改标签的外观。

¥Use the variant prop to change the appearance of the tag.

Gray
Gray
Gray
Gray
Gray
Gray
Gray
Gray
Gray
Gray
Gray
Gray

尺寸

¥Sizes

使用 size 属性更改标签的大小。

¥Use the size prop to change the size of the tag.

Gray
Gray
Gray
Gray
Gray
Gray
Gray
Gray
Gray

颜色

¥Colors

使用 colorPalette 属性更改标签的颜色。

¥Use the colorPalette prop to change the color of the tag.

gray

Content
Content
Content

red

Content
Content
Content

green

Content
Content
Content

blue

Content
Content
Content

teal

Content
Content
Content

pink

Content
Content
Content

purple

Content
Content
Content

cyan

Content
Content
Content

orange

Content
Content
Content

yellow

Content
Content
Content

可关闭

¥Closable

Tag.EndElement 中使用 Tag.CloseTrigger 使标签可关闭。

¥Use the Tag.CloseTrigger within the Tag.EndElement to make the tag closable.

Tag 1
Tag 2

溢出

¥Overflow

使用 maxWidth 属性控制标签的最大宽度。当内容超过此宽度时,将会以省略号截断。

¥Use the maxWidth prop to control the maximum width of the tag. When the content exceeds this width, it will be truncated with an ellipsis.

当处理长度可能变化的动态或用户生成内容时,此功能尤其有用。

Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam molestias, laboriosam, quod, quia quidem quae voluptatem natus exercitationem autem quibusdam

头像

¥Avatar

标签组件的设计使其能够与 Avatar 组件完美兼容。

¥The tag component has been designed to work well with the Avatar component.

注意:将头像尺寸设置为 full,以确保其大小正确。

JD
Emily sm
JD
Emily md
JD
Emily lg
JD
Emily xl

渲染为按钮

¥Render as button

使用 asChild 属性将标签渲染为按钮。

¥Use the asChild prop to render the tag as a button.

已关闭组件

¥Closed Component

以下是如何设置封闭组件组合的标签。

¥Here's how to setup the Tag for a closed component composition.

import { Tag as ChakraTag } from "@chakra-ui/react"
import * as React from "react"

export interface TagProps extends ChakraTag.RootProps {
  startElement?: React.ReactNode
  endElement?: React.ReactNode
  onClose?: VoidFunction
  closable?: boolean
}

export const Tag = React.forwardRef<HTMLSpanElement, TagProps>(
  function Tag(props, ref) {
    const {
      startElement,
      endElement,
      onClose,
      closable = !!onClose,
      children,
      ...rest
    } = props

    return (
      <ChakraTag.Root ref={ref} {...rest}>
        {startElement && (
          <ChakraTag.StartElement>{startElement}</ChakraTag.StartElement>
        )}
        <ChakraTag.Label>{children}</ChakraTag.Label>
        {endElement && (
          <ChakraTag.EndElement>{endElement}</ChakraTag.EndElement>
        )}
        {closable && (
          <ChakraTag.EndElement>
            <ChakraTag.CloseTrigger onClick={onClose} />
          </ChakraTag.EndElement>
        )}
      </ChakraTag.Root>
    )
  },
)

如果你想自动将封闭式组件添加到项目中,请运行以下命令:

¥If you want to automatically add the closed component to your project, run the command:

npx @chakra-ui/cli snippet add tag

属性

¥Props

根元素

¥Root

PropDefaultType
colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

The color palette of the component

size 'md'
'sm' | 'md' | 'lg' | 'xl'

The size of the component

variant 'surface'
'subtle' | 'solid' | 'outline' | 'surface'

The variant of the component

as
React.ElementType

The underlying element to render.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior.

For more details, read our Composition guide.
unstyled
boolean

Whether to remove the component's style.

Previous

表格

Next

时间轴