The visual color appearance of the component
Tag
Tag component is used for items that need to be labeled, categorized, or organized using keywords that describe them.
Import#
Chakra UI exports 5 Tag related components:
- Tag: The wrapper for all the tag elements.
- TagLabel: The label for tag's text content.
- TagLeftIcon: The icon placed on the left side of the tag.
- TagRightIcon: The icon placed on the right side of the tag.
- TagCloseButton: The close button for the tag.
import {Tag,TagLabel,TagLeftIcon,TagRightIcon,TagCloseButton,} from '@chakra-ui/react'
Usage#
<Tag>Sample Tag</Tag>
<HStack spacing={4}>{['sm', 'md', 'lg'].map((size) => (<Tag size={size} key={size} variant='solid' colorScheme='teal'>Teal</Tag>))}</HStack>
With left icon#
<HStack spacing={4}>{['sm', 'md', 'lg'].map((size) => (<Tag size={size} key={size} variant='subtle' colorScheme='cyan'><TagLeftIcon boxSize='12px' as={AddIcon} /><TagLabel>Cyan</TagLabel></Tag>))}</HStack>
With right icon#
<HStack spacing={4}>{['sm', 'md', 'lg'].map((size) => (<Tag size={size} key={size} variant='outline' colorScheme='blue'><TagLabel>Blue</TagLabel><TagRightIcon as={MdSettings} /></Tag>))}</HStack>
With close button#
<HStack spacing={4}>{['sm', 'md', 'lg'].map((size) => (<Tagsize={size}key={size}borderRadius='full'variant='solid'colorScheme='green'><TagLabel>Green</TagLabel><TagCloseButton /></Tag>))}</HStack>
With custom element#
<Tag size='lg' colorScheme='red' borderRadius='full'><Avatarsrc='https://bit.ly/sage-adebayo'size='xs'name='Segun Adebayo'ml={-1}mr={2}/><TagLabel>Segun</TagLabel></Tag>
Props#
colorScheme
colorScheme
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"
gray
size
size
The size of the Tag
"sm" | "md" | "lg"
md
variant
variant
The variant of the Tag
"subtle" | "solid" | "outline"
subtle
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> ); }