import { HStack, Tag } from "@chakra-ui/react"
const Demo = () => {
return (
<HStack>
<Tag.Root>
<Tag.Label>Plain Tag</Tag.Label>
</Tag.Root>
<Tag.Root>
<Tag.Label>Closable Tag</Tag.Label>
<Tag.EndElement>
<Tag.CloseTrigger />
</Tag.EndElement>
</Tag.Root>
</HStack>
)
}
用法
¥Usage
import { Tag } from "@chakra-ui/react"
<Tag.Root>
<Tag.Label>Tag here</Tag.Label>
</Tag.Root>
如果你更喜欢封闭的组件组合,请查看 以下代码片段。
¥If you prefer a closed component composition, check out the snippet below.
示例
¥Examples
图标
¥Icon
使用 Tag.StartElement
和 Tag.EndElement
组件在标签的开头或结尾添加图标。
¥Use the Tag.StartElement
and Tag.EndElement
components to add an icon to the
start or end of the tag
import { HStack, Tag } from "@chakra-ui/react"
import { LuCircleUser, LuFileBadge } from "react-icons/lu"
const Demo = () => {
return (
<HStack>
<Tag.Root>
<Tag.StartElement>
<LuCircleUser />
</Tag.StartElement>
<Tag.Label>Tag 1</Tag.Label>
</Tag.Root>
<Tag.Root>
<Tag.StartElement>
<LuFileBadge />
</Tag.StartElement>
<Tag.Label>Top Rated</Tag.Label>
</Tag.Root>
<Tag.Root>
<Tag.Label>Tag 2</Tag.Label>
<Tag.EndElement>
<LuCircleUser />
</Tag.EndElement>
</Tag.Root>
</HStack>
)
}
变量
¥Variants
使用 variant
属性更改标签的外观。
¥Use the variant
prop to change the appearance of the tag.
import { For, HStack, Stack, Tag } from "@chakra-ui/react"
import { HiCheck } from "react-icons/hi"
const Demo = () => {
return (
<Stack gap="8">
<For each={["subtle", "solid", "outline", "surface"]}>
{(variant) => (
<HStack key={variant}>
<Tag.Root variant={variant}>
<Tag.Label>Gray</Tag.Label>
</Tag.Root>
<Tag.Root variant={variant}>
<Tag.Label>Gray</Tag.Label>
<Tag.EndElement>
<Tag.CloseTrigger />
</Tag.EndElement>
</Tag.Root>
<Tag.Root variant={variant}>
<Tag.Label>Gray</Tag.Label>
<Tag.EndElement>
<HiCheck />
</Tag.EndElement>
</Tag.Root>
</HStack>
)}
</For>
</Stack>
)
}
尺寸
¥Sizes
使用 size
属性更改标签的大小。
¥Use the size
prop to change the size of the tag.
import { For, HStack, Stack, Tag } from "@chakra-ui/react"
import { HiCheck } from "react-icons/hi"
const Demo = () => {
return (
<Stack gap="8">
<For each={["sm", "md", "lg"]}>
{(size) => (
<HStack key={size}>
<Tag.Root size={size}>
<Tag.Label>Gray</Tag.Label>
</Tag.Root>
<Tag.Root size={size}>
<Tag.Label>Gray</Tag.Label>
<Tag.EndElement>
<Tag.CloseTrigger />
</Tag.EndElement>
</Tag.Root>
<Tag.Root size={size}>
<Tag.Label>Gray</Tag.Label>
<Tag.EndElement>
<HiCheck />
</Tag.EndElement>
</Tag.Root>
</HStack>
)}
</For>
</Stack>
)
}
颜色
¥Colors
使用 colorPalette
属性更改标签的颜色。
¥Use the colorPalette
prop to change the color of the tag.
gray
red
green
blue
teal
pink
purple
cyan
orange
yellow
import { Stack, Tag, Text } from "@chakra-ui/react"
import { colorPalettes } from "compositions/lib/color-palettes"
import { HiPlus } from "react-icons/hi"
const Demo = () => {
return (
<Stack gap="2" align="flex-start">
{colorPalettes.map((colorPalette) => (
<Stack
align="center"
key={colorPalette}
direction="row"
gap="10"
px="4"
>
<Text minW="8ch">{colorPalette}</Text>
<Tag.Root size="sm" colorPalette={colorPalette}>
<Tag.Label>Content</Tag.Label>
</Tag.Root>
<Tag.Root size="sm" colorPalette={colorPalette}>
<Tag.StartElement>
<HiPlus />
</Tag.StartElement>
<Tag.Label>Content</Tag.Label>
</Tag.Root>
<Tag.Root colorPalette={colorPalette} variant="solid">
<Tag.Label>Content</Tag.Label>
<Tag.EndElement>
<Tag.CloseTrigger />
</Tag.EndElement>
</Tag.Root>
</Stack>
))}
</Stack>
)
}
可关闭
¥Closable
在 Tag.EndElement
中使用 Tag.CloseTrigger
使标签可关闭。
¥Use the Tag.CloseTrigger
within the Tag.EndElement
to make the tag closable.
import { HStack, Tag } from "@chakra-ui/react"
import { LuActivity } from "react-icons/lu"
const Demo = () => {
return (
<HStack>
<Tag.Root>
<Tag.StartElement>
<LuActivity />
</Tag.StartElement>
<Tag.Label>Tag 1</Tag.Label>
<Tag.EndElement>
<Tag.CloseTrigger />
</Tag.EndElement>
</Tag.Root>
<Tag.Root>
<Tag.Label>Tag 2</Tag.Label>
<Tag.EndElement>
<Tag.CloseTrigger />
</Tag.EndElement>
</Tag.Root>
</HStack>
)
}
溢出
¥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.
当处理长度可能变化的动态或用户生成内容时,此功能尤其有用。
import { Tag } from "@chakra-ui/react"
const Demo = () => {
return (
<Tag.Root size="sm" colorPalette="blue" maxW="200px">
<Tag.Label>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam
molestias, laboriosam, quod, quia quidem quae voluptatem natus
exercitationem autem quibusdam
</Tag.Label>
<Tag.EndElement>
<Tag.CloseTrigger />
</Tag.EndElement>
</Tag.Root>
)
}
头像
¥Avatar
标签组件的设计使其能够与 Avatar
组件完美兼容。
¥The tag component has been designed to work well with the Avatar
component.
注意:将头像尺寸设置为 full
,以确保其大小正确。
import { Avatar, For, HStack, Tag } from "@chakra-ui/react"
const Demo = () => {
return (
<HStack>
<For each={["sm", "md", "lg", "xl"]}>
{(size) => (
<Tag.Root key={size} size={size} rounded="full">
<Tag.StartElement>
<Avatar.Root size="full">
<Avatar.Image src="https://i.pravatar.cc/300?u=1" />
<Avatar.Fallback name="John Doe" />
</Avatar.Root>
</Tag.StartElement>
<Tag.Label>Emily {size}</Tag.Label>
</Tag.Root>
)}
</For>
</HStack>
)
}
渲染为按钮
¥Render as button
使用 asChild
属性将标签渲染为按钮。
¥Use the asChild
prop to render the tag as a button.
import { Tag } from "@chakra-ui/react"
import { LuCheck } from "react-icons/lu"
const Demo = () => {
return (
<Tag.Root asChild variant="solid">
<button type="submit">
<Tag.Label>Fish </Tag.Label>
<LuCheck />
</button>
</Tag.Root>
)
}
已关闭组件
¥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
Prop | Default | Type |
---|---|---|
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. |