Tag
Tag component is used for items that need to be labeled, categorized, or organized using keywords that describe them.
Import#
import { Tag } from '@chakra-ui/react'
Usage#
<Tag.Root><Tag.Label>Sample Tag</Tag.Label></Tag.Root>
Sizes#
Set the size
prop to change the size of the tag.
<HStack gap={4}>{['sm', 'md', 'lg'].map((size) => (<Tag.Root size={size} key={size} variant='solid' colorPalette='teal'><Tag.Label>Teal</Tag.Label></Tag.Root>))}</HStack>
Rendering an icon#
<HStack gap={4}>{['sm', 'md', 'lg'].map((size) => (<Tag.Root size={size} key={size} variant='subtle' colorPalette='cyan'><FiPlus /><Tag.Label>Cyan</Tag.Label></Tag.Root>))}</HStack>
Rendering the close button#
<HStack gap={4}>{['sm', 'md', 'lg'].map((size) => (<Tag.Root size={size} key={size} colorPalette='green'><Tag.Label>Green</Tag.Label><Tag.CloseTrigger /></Tag.Root>))}</HStack>
Usage with avatar#
<Tag.Root variant='outline' borderRadius='full'><Avatar.Root size='xs'><Avatar.Image src='https://bit.ly/dan-abramov' /><Avatar.Fallback name='Dan Abrahmov' /></Avatar.Root><Tag.Label> Dan Abramov</Tag.Label><Tag.CloseTrigger /></Tag.Root>
Props#
Overview#
The Tag
component is a multipart component. The styling needs to be applied to
each part specifically.
To learn more about styling single part components, visit the Component Style page
Anantomy#
- A:
container
- B:
label
- C:
closeButton
Theming properties#
The properties that affect the theming of the Tag
component are:
variant
: The visual variant of the tag. Defaults tosubtle
.colorScheme
: The color scheme of the tag. Defaults togray
.size
: The size of the tag. Defaults tomd
.
Theming utilities#
createMultiStyleConfigHelpers
: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle
anddefineMultiStyleConfig
).definePartsStyle
: a function used to create multipart style objects.defineMultiStyleConfig
: a function used to define the style configuration for a multipart component.
import { tagAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(tagAnatomy.keys)
Customizing the default theme#
import { tagAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(tagAnatomy.keys)const baseStyle = definePartsStyle({// define the part you're going to stylecontainer: {bg: 'orange.400',color: 'blackAlpha.700',},})export const tagTheme = defineMultiStyleConfig({ baseStyle })
After customizing the tag theme, we can import it in our theme file and add it in the components property:
import { extendTheme } from '@chakra-ui/react'import { tagTheme } from './components/tag'export const theme = extendTheme({components: { Tag: tagTheme },})
This is a crucial step to make sure that any changes that we make to the tag theme are applied.
Adding a custom size#
Let's assume we want to include an extra large tag size. Here's how we can do that:
import { tagAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const ml = defineStyle({px: '3',py: '2',fontSize: '25',})const sizes = {ml: definePartsStyle({ container: ml, label: ml }),}export const tagTheme = defineMultiStyleConfig({ sizes })// Now we can use the new `xl` size;<Tag size='ml' />
Every time you're adding anything new to the theme, you'd need to run the cli command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a custom branded variant. Here's how we can do that:
import { tagAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(tagAnatomy.keys)const brandPrimary = definePartsStyle({container: {bg: 'orange.400',color: 'blackAlpha.700',},})const thick = definePartsStyle({container: {px: '4',py: '2',bg: 'blue.400',},})export const tagTheme = defineMultiStyleConfig({variants: {brand: brandPrimary,thick: thick,},})// Now we can use the new `brandPrimary` variant;<Tag variant='brand' />
Changing the default properties#
import { tagAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(tagAnatomy.keys,)export const tagTheme = defineMultiStyleConfig({defaultProps: {size: 'ml',variant: 'brand',},})// This saves you time, instead of manually setting the size and variant every time you use an input:<Input size="ml" variant="brand" ... />
Showcase#
import { Box, IconButton, Center, Tag, useColorMode } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box position="relative"> <Center h="100vh"> <Tag>Default</Tag> <Tag variant="brand">Branded</Tag> <Tag variant="thick">Thick</Tag> <Tag size="ml">XL Tag</Tag> </Center> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }