Tag

Tag component is used for items that need to be labeled, categorized, or organized using keywords that describe them.

    SourceTheme source@chakra-ui/tag

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 to subtle.
  • colorScheme: The color scheme of the tag. Defaults to gray.
  • size: The size of the tag. Defaults to md.

Theming utilities#

  • createMultiStyleConfigHelpers: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle and defineMultiStyleConfig).
  • 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 style
container: {
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>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <Tag variant="brand">Branded</Tag>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <Tag variant="thick">Thick</Tag>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <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>
  );
}
Edit this page on GitHub

Proudly made inNigeria by Segun Adebayo

Deployed by â–² Vercel